Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在选项卡的属性上绑定ListView中的组合框_C#_Wpf_Xaml_Mvvm_Binding - Fatal编程技术网

C# 在选项卡的属性上绑定ListView中的组合框

C# 在选项卡的属性上绑定ListView中的组合框,c#,wpf,xaml,mvvm,binding,C#,Wpf,Xaml,Mvvm,Binding,我在XAML中再次遇到绑定问题,我自己无法解决。我有一个列表视图和一个组合框。ListView的ItemsSource位于TabControl的视图模型上,ComboBox.ItemsSource也是如此。如何在此集合上绑定组合框 以下是我目前的代码: <ListView ItemsSource="{Binding ListViewSource}" SelectedItem="{Binding ListViewSelection}" ItemTemplate="{Dyn

我在XAML中再次遇到绑定问题,我自己无法解决。我有一个列表视图和一个组合框。ListView的ItemsSource位于TabControl的视图模型上,ComboBox.ItemsSource也是如此。如何在此集合上绑定组合框

以下是我目前的代码:

<ListView ItemsSource="{Binding ListViewSource}" SelectedItem="{Binding ListViewSelection}"
          ItemTemplate="{DynamicResource ListViewTemplate}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="...">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding ComboBoxSource}"
                                  SelectedValue="{Binding ...}"
                                  DisplayMemberPath="DisplayName"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn> // ...
是否可以在此属性上绑定CombBox.ItemsSource?

仅举一个例子。 您有一个类似这样的TabViewModel

public class TabViewModel
{
    public ObservableCollection<CostViewModel> ListViewSource { get; set; }
    public ObservableCollection<TypeViewModel> ComboBoxSource { get; set; }

    public TabViewModel()
    {
        ListViewSource = new ObservableCollection<CostViewModel>();
        ListViewSource.Add(new CostViewModel() { CostA = "A", CostB = "B" });
        ListViewSource.Add(new CostViewModel() { CostA = "1", CostB = "2" });            

        ComboBoxSource = new ObservableCollection<TypeViewModel>();
        ComboBoxSource.Add(new TypeViewModel() { TypeA = "A1", TypeB = "B1" });
        ComboBoxSource.Add(new TypeViewModel() { TypeA = "A2", TypeB = "B2" });
    }
}
并为窗口指定一个名称,并与ElementName绑定


在CellTemplate中,猜测:ItemsSource={Binding DataContext.ComboxSource,RelativeSource={RelativeSource AncestorType=ListView}}。根据前面的注释,Relative source可以执行您想要的操作。不幸的是,您的问题没有明确说明为什么ListView中的每个项目都有相同的下拉列表,但是如果出于某种原因,相对源方法不适合您的需要,对于每个TypeViewModel来说,公开其自己的ComboBoxSource属性并不是不合理的,它只是委托给父视图模型,例如….ComboBoxSource{get{return}parentViewModel.ComboxSource;}}。或者取决于这些值的来源,甚至可能是一个单态。有很多可能的答案。谢谢你们的回答。不幸的是,这对我不起作用,但我现在解决了它,正如KMCho在回答部分{Binding ElementName=ControlName,Path=DataContext.ComboBoxSource}中解释的那样,非常感谢您提供了这个伟大的示例!这解决了我的问题。
public class TabViewModel
{
    public ObservableCollection<CostViewModel> ListViewSource { get; set; }
    public ObservableCollection<TypeViewModel> ComboBoxSource { get; set; }

    public TabViewModel()
    {
        ListViewSource = new ObservableCollection<CostViewModel>();
        ListViewSource.Add(new CostViewModel() { CostA = "A", CostB = "B" });
        ListViewSource.Add(new CostViewModel() { CostA = "1", CostB = "2" });            

        ComboBoxSource = new ObservableCollection<TypeViewModel>();
        ComboBoxSource.Add(new TypeViewModel() { TypeA = "A1", TypeB = "B1" });
        ComboBoxSource.Add(new TypeViewModel() { TypeA = "A2", TypeB = "B2" });
    }
}
TabViewModel vm { get; set; }        
public Window1()
{
    vm = new TabViewModel();
    this.DataContext = vm;            
    InitializeComponent();
}
<Window x:Class="WpfApplication5.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300" Name="mainWnd">    
    <Grid>        
        <ListView ItemsSource="{Binding ListViewSource}" >
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="...">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding ElementName=mainWnd, Path=DataContext.ComboBoxSource}" DisplayMemberPath="TypeA"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>        
    </Grid>
</Window>