【问题标题】:Binding a ComboBox in a ListView on a Property of the Tab在选项卡的属性上绑定 ListView 中的组合框
【发布时间】:2017-05-17 17:55:20
【问题描述】:

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

到目前为止,这是我的代码:

<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> // ...

这里只是TabViewModelItemsSource 属性的头部:

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

是否可以在此属性上绑定CombBox.ItemsSource

【问题讨论】:

  • 在 CellTemplate 中,猜测:ItemsSource="{Binding DataContext.ComboBoxSource, RelativeSource={RelativeSource AncestorType=ListView}}".
  • 根据前面的评论,相对源可以做你想做的事。不幸的是,您的问题并不清楚为什么您的 ListView 中的每个项目都会有相同的下拉菜单,但是如果由于某种原因相对源方法不适合您的需求,那么每个 @987654335 都不会不合理@ 公开其自己的 ComboBoxSource 属性,该属性仅代表父视图模型(例如 ...ComboBoxSource { get { return _parentViewModel.ComboBoxSource; } }。或者取决于这些值的来源,甚至可能是单例。有很多可能的答案。
  • 感谢两位的回答。不幸的是,这对我不起作用,但我现在按照 KMCho 在答案 Section { Binding ElementName=ControlName, Path=DataContext.ComboBoxSource } 中解释的方式解决了它

标签: c# wpf xaml mvvm binding


【解决方案1】:

只是一个例子。 你有一个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" });
    }
}

并将DataContext 放入您的window

TabViewModel vm { get; set; }        
public Window1()
{
    vm = new TabViewModel();
    this.DataContext = vm;            
    InitializeComponent();
}

window命名,绑定ElementName

<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>

【讨论】:

  • 非常感谢您提供的好例子!这解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-27
  • 2011-05-11
  • 1970-01-01
相关资源
最近更新 更多