【问题标题】:WPF Binding inside bindingWPF 绑定内部绑定
【发布时间】:2018-05-25 07:50:30
【问题描述】:

我有一个与我的viewModel.CurrentProduct.ProductAddOns 绑定的列表

<ListView ItemsSource="{Binding CurrentProduct.ProductAddOns}" MaxHeight="200">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <WrapPanel>
                            <ComboBox ItemsSource="{Binding AllAddOns}" SelectedItem="{Binding AddOn}">
                                <ComboBox.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Name}"/>
                                    </DataTemplate>
                                </ComboBox.ItemTemplate>
                            </ComboBox>

                            <TextBox Text="{Binding Quantity}" Width="100"
                                     Margin="8" VerticalAlignment="Center"/>
                        </WrapPanel>
                    </DataTemplate>

ViewModel 代码

class AddOrUpdateProductDialogViewModel : BaseViewModel
{
    ProductRepository productRepository = new ProductRepository();
    TaxRepository taxRepository = new TaxRepository();
    CategoryRepository categoryRepository = new CategoryRepository();
    AddOnRepository addOnRepository = new AddOnRepository();

    public AddOrUpdateProductDialogViewModel()
    {
        CurrentProduct = new Product
        {
            Id = 0,
            ImageSource = new BitmapImage(new Uri(@"..\..\..\..\assets\Icon.png", UriKind.Relative))
        };
        FillDefaultData();
    }
    public AddOrUpdateProductDialogViewModel(int ProductId)
    {
        CurrentProduct = productRepository.GetById(ProductId);
        FillDefaultData();
        foreach (var item in AllCategories)
        {
            var pr = currentProduct.Categories.FirstOrDefault(p => p.Id == item.Model.Id);
            if (pr != null)
            {
                item.IsSelected = true;
            }
        }
        SelectedTax = currentProduct.Tax;
    }

    private Product currentProduct;
    public Product CurrentProduct
    {
        get { return currentProduct; }
        set
        {
            currentProduct = value;
            OnPropertyChanged("CurrentProduct");
        }
    }

    CheckListModel<Category> allCategories;
    public CheckListModel<Category> AllCategories
    {
        get { return allCategories; }
        set
        {
            allCategories = value;
            OnPropertyChanged("AllCategories");
        }
    }


    ObservableCollection<Tax> allTaxes;
    public ObservableCollection<Tax> AllTaxes
    {
        get { return allTaxes; }
        set
        {
            allTaxes = value;
            OnPropertyChanged("allTaxes");
        }
    }

    Tax selectedTax;
    public Tax SelectedTax
    {
        get
        {
            return selectedTax;
        }
        set
        {
            selectedTax = value;
            CurrentProduct.Tax = value;
            OnPropertyChanged("SelectedTax");
        }
    }


    ObservableCollection<AddOn> allAddOns;
    public ObservableCollection<AddOn> AllAddOns
    {
        get { return allAddOns; }
        set
        {
            allAddOns = value;
            OnPropertyChanged("AllAddOns");
        }
    }

    void FillDefaultData()
    {
        ICollection<ProductAddOn> addons = CurrentProduct.ProductAddOns;
        CurrentProduct.ProductAddOns = new ObservableCollection<ProductAddOn>(addons);
        AllAddOns = new ObservableCollection<AddOn>(addOnRepository.GetAll());

        AllCategories = new CheckListModel<Category>();
        AllTaxes = new ObservableCollection<Tax>(taxRepository.GetAll());
        foreach (var item in categoryRepository.GetAll())
        {
            if (!item.IsDeleted)
            {
                AllCategories.Add(new CheckListItemModel<Category> { Model = item });
            }
        }
    }

    RelayCommand addOrUpdateCommand;
    public RelayCommand AddOrUpdateCommand
    {
        get
        {
            return addOrUpdateCommand ??
                (addOrUpdateCommand = new RelayCommand(o =>
                {
                    currentProduct.Categories = new List<Category>();

                    currentProduct.Categories = AllCategories.GetChekedItemsModels();
                    productRepository.AddOrUpdate(CurrentProduct);

                    MessageBox.Show("Sucessfully Updated", "Updated", MessageBoxButton.OK, MessageBoxImage.Information);

                }));
        }
    }

    private RelayCommand addAddOnCommand;
    public RelayCommand AddAddOnCommand
    {
        get
        {
            return addAddOnCommand ?? (addAddOnCommand = new RelayCommand((o) =>
            {
                currentProduct.ProductAddOns.Add(new ProductAddOn());

            }));
        }
    }

在 dataTemplate 的 ListView 中,我有想要绑定 viewModel.AllAddons 的 ComboBox。 但是绑定AllAdOns 试图在ProductAddOn class 中找到AllAddOns 属性。

如何从 dataContext(viewModel? 中绑定 AllAddOns?

【问题讨论】:

  • 好的,谢谢你的评论,我马上更新

标签: c# wpf xaml


【解决方案1】:

你需要的是

<ComboBox 
    ItemsSource="{Binding DataContext.AllAddOns,RelativeSource={RelativeSource FindAncestor,AncestorType=ListView}" 
    SelectedItem="{Binding AddOn}"
    >

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 2011-03-30
    相关资源
    最近更新 更多