WPF中GridViewColumn的DisplayMemberBinding如何使用字典键的值

3

我在Stack和Google上没有找到任何类似的内容,所以可能不可能实现,但希望有聪明人能提出想法。我对WPF/XAML是一位初学者。

我有一个自定义类,类似于这个样子。

public class LogEntry
{
   public Diciontary<string, string> Stuff;
   public string MyOtherProperty;   
}

我的GridView将有两列。一列是MyOtherProperty,另一列是Stuff["Stuff1"]。假设我不能将Dictionary更改为更容易绑定的内容。

我正在将我的ListView绑定到一个List<LogEntry>。在这种情况下,我该怎么做呢?

<ListView ItemsSource="{Binding}" DataContext="{Binding}">
   <ListView.View>
      <GridView>
         <GridView.Columns>
            <GridViewColumn DisplayMemberBinding="{Binding MyOtherProperty}"></GridViewColumn>
            <GridViewColumn DisplayMemberBinding="**{Binding Stuff[Stuff1]}**"></GridViewColumn>
         </GridView.Columns>
      </GridView>
   </ListView.View>
</ListView>

有什么想法吗?谢谢。

可能是 https://dev59.com/A0nSa4cB1Zd3GeqPP6uQ 的重复问题。 - Wonko the Sane
不,我认为这不是那个的复制。在这种情况下,ItemsSource是列表而不是字典。 - scottheckel
1个回答

4

WPF 支持绑定属性而不是字段。将 LogEntry 类更改为下面的形式,它就可以正常工作了。

public class LogEntry
{
    public Dictionary<string, string> Stuff { get; set; }
    public string MyOtherProperty { get; set; }
}

谢谢!那解决了问题。总是那些简单的问题让你感到困惑。 - scottheckel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接