【问题标题】:MVVM populating a comboboxMVVM 填充组合框
【发布时间】:2013-11-18 06:49:34
【问题描述】:

我还是 MVVM(和 C#)的新手,所以如果我的问题不清楚,请告诉我,我会更新/澄清。

我的xaml 中有一个ListView,代码如下:

<ListView.Resources>
    <local:IndexConverter x:Key="IndexConverter" />
    <DataTemplate x:Key="OrdinalColumnDataTemplate">
        <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListViewItem},
        Converter={StaticResource ResourceKey=IndexConverter}}" HorizontalAlignment="Right" />
    </DataTemplate>
</ListView.Resources>

<ListView.View>
    <GridView x:Name="gridView3">
        <GridViewColumn Header="#" CellTemplate="{StaticResource ResourceKey=OrdinalColumnDataTemplate}" />
        <GridViewColumn DisplayMemberBinding="{Binding Name, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" Header="Name" />
        <GridViewColumn DisplayMemberBinding="{Binding License, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" Header="License" />
    </GridView>
</ListView.View>

它按预期工作,它填充了每个元素的NameLicense 属性。 现在我想添加一个ComboBox 并用ListView 中显示的相同元素的唯一Names 填充它。

问题 1:我已经测试了下面的代码,但它不起作用,xml 应该是什么样子?

<ComboBox Name="filterUserComboBox" ItemsSource="{Binding Path=licenseListView, ElementName=Name}"></ComboBox>

问题 2:当上述工作正常时,如何将“全部”(或单个空元素)元素添加到 ComboBox?

【问题讨论】:

  • 为什么不在 ResourceList 的索引 0 处插入一个新条目来表示“全部”选项?
  • 也许这个问题就是你想做的:stackoverflow.com/questions/1426050/…
  • @roqz,不,这完全是另一个问题。
  • 可以肯定的是,您希望组合框中的空元素像空值一样吗?
  • @dennisschütz,是的。后缀为空元素。

标签: c# data-binding mvvm combobox


【解决方案1】:

好的,我自己解决了这两个问题。希望它对未来的人们有所帮助。

答案 1:原来 xml 只是有点不对劲。

<ComboBox Name="filterUserComboBox" ItemsSource="{Binding Path=ResourceList}" DisplayMemberPath="Name"></ComboBox>

回答 2: 我对此进行了很长时间的研究,解决方案是实际更改填充在 ComboBox 中的 MVVM,因此源实际上应该包含一个空/全部选项以显示在组合框。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 2022-01-06
    • 2013-06-22
    • 2011-11-20
    • 1970-01-01
    相关资源
    最近更新 更多