C# WPF绑定内部绑定

C# WPF绑定内部绑定,c#,wpf,xaml,C#,Wpf,Xaml,我有一个与我的viewModel.CurrentProduct.ProductAddOns绑定的列表 <ListView ItemsSource="{Binding CurrentProduct.ProductAddOns}" MaxHeight="200"> <ListView.ItemTemplate> <DataTemplate> &l

我有一个与我的
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>

视图模型代码

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());

            }));
        }
    }
类AddOrUpdateProductDialogViewModel:BaseViewModel { ProductRepository ProductRepository=新的ProductRepository(); TaxRepository TaxRepository=新的TaxRepository(); CategoryRepository CategoryRepository=新的CategoryRepository(); AddOnRepository AddOnRepository=新的AddOnRepository(); public AddOrUpdateProductDialogViewModel() { 当前产品=新产品 { Id=0, ImageSource=新位图图像(新Uri(@“.\..\..\..\assets\Icon.png”,UriKind.Relative)) }; FillDefaultData(); } public AddOrUpdateProductDialogViewModel(int-ProductId) { CurrentProduct=productRepository.GetById(ProductId); FillDefaultData(); foreach(所有类别中的var项目) { var pr=currentProduct.Categories.FirstOrDefault(p=>p.Id==item.Model.Id); 如果(pr!=null) { item.IsSelected=true; } } 选择的税=当前产品税; } 私人产品; 公共产品 { 获取{return currentProduct;} 设置 { 当前产品=价值; OnPropertyChanged(“当前产品”); } } 所有类别的清单模型; 公共清单模型所有类别 { 获取{返回所有类别;} 设置 { 所有类别=价值; 不动产变更(“所有类别”); } } 可观察到的所有税收; 公开征收所有税款 { 获取{returnalltaxes;} 设置 { 所有税收=价值; 不动产变更(“所有税费”); } } 税收选择税; 公共税收选择税 { 收到 { 返回所选的税; } 设置 { selectedTax=值; CurrentProduct.Tax=价值; 关于财产变更(“选择税”); } } 可观测的集合; 公共可观测集合Alladons { 获取{返回allAddOns;} 设置 { allAddOns=价值; 房地产变更(“Alladons”); } } void FillDefaultData() { ICollection addons=CurrentProduct.ProductAddOns; CurrentProduct.ProductAddOns=新的可观察集合(addons); AllAddOns=newobserveCollection(addOnRepository.GetAll()); AllCategories=新的CheckListModel(); AllTaxes=新的ObservableCollection(taxRepository.GetAll()); foreach(categoryRepository.GetAll()中的变量项) { 如果(!item.IsDeleted) { Add(新的CheckListItemModel{Model=item}); } } } RelayCommand addor updatecommand; public relay命令addor更新命令 { 收到 { 返回addOrUpdateCommand?? (addOrUpdateCommand=newrelayCommand(o=> { currentProduct.Categories=新列表(); currentProduct.Categories=AllCategories.GetChekedItemModels(); productRepository.AddOrUpdate(当前产品); MessageBox.Show(“成功更新”,“更新”,MessageBoxButton.OK,MessageBoxImage.Information); })); } } 专用中继命令AddOnCommand; 公共中继命令添加命令 { 收到 { 返回addonCommand???(addonCommand=newrelayCommand((o)=> { currentProduct.ProductAddOns.Add(新的ProductAddOn()); })); } } 在dataTemplate的ListView中,我有一个组合框,我想绑定viewModel.AllAddons。 但是绑定
AllAdOns
尝试在
ProductAddOn类中查找
AllAdOns
属性

如何从dataContext绑定AllAddOns(viewModel?

您需要的是

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

好的,谢谢你的评论,我现在就更新