【问题标题】:Binding a Dictionary with its Key and the members of its Value将字典与其键及其值的成员绑定
【发布时间】:2015-07-07 07:36:07
【问题描述】:

我想使用DataTemplateDictionary<DateTime, MyClass> 绑定为StackPanel 的ItemSource。它适用于Collection<DateTime>,但似乎无法掌握字典的语法。 如果我只绑定DateTime 类型的字典键,则下面的 xaml 文件有效。但我也想访问价值。值是 MaClass 类型,我还需要将各种成员(FromDateToDate)放入网格中。提前谢谢你们!

.xaml 文件

<DataTemplate x:Key="Mytemplate" DataType="x:Type local:MyClass">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Label Grid.Column="0" Content="{Binding Key}"
               ContentStringFormat="{}{0:dd.MM  ddd}"/> 
        <!--Label Grid.Column="1" Content="{Binding Value.FromDate}"
                  ContentStringFormat="{}{0:HH:mm}"/--> 
    </Grid>
</DataTemplate>

...

<ItemsControl ItemsSource="{Binding ElementName=thispage,Path=MyDictionary}"
              ItemTemplate="{StaticResource Mytemplate}" />

我的班级:

public class MyClass
{
     public DateTime FromDate { get; set; }
     public DateTime ToDate { get; set; }
     public string ThisString { get; set; }
     public MyClass()
     {
         ...
     }
}

【问题讨论】:

  • 你在使用 MVVM 吗?如果 MyDictionary 属性的类型为 Dictionary&lt;DataTime, MyClass&gt;,那么您的模板是错误的,因为您将 DataType 指定为 x:Type local:MyClass。还有为什么在ItemsSource的绑定中使用ElementName
  • 我是。是的,它的类型是Dictionary&lt;DateTime, MyClass&gt;。提供 Dictionary 作为 DataType 的语法如何?
  • 我认为这里不需要DataType。如果您删除它,您的绑定应该可以工作。
  • @tzippy 不知道您简化了多少代码,但 FromDateToDateThisString 看起来像公共字段,您无法绑定到字段。这不是有效的binding source。如果是这种情况,您需要通过添加 { get; set; } 转换为公共财产,例如
  • @tzippy dkozl 是对的,你必须使用属性来代替......你也可能想看看这个链接:stackoverflow.com/a/2495488/4668080

标签: .net wpf xaml


【解决方案1】:

首先您必须修复您的MyClass 以使用属性而不是字段,因为您不能将数据绑定到字段。

public class MyClass
{
    public DateTime FromDate { get; set; }
    public DateTime ToDate { get; set; }
    public string ThisString { get; set; }

    public MyClass()
    {
        ...
    }
}

然后只需将您的模板更改为:

<DataTemplate x:Key="Mytemplate">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Label Grid.Column="0"
                Content="{Binding Key}"
                ContentStringFormat="{}{0:dd.MM  ddd}" />
        <Label Grid.Column="1"
                Content="{Binding Value.FromDate}"
                ContentStringFormat="{}{0:HH:mm}" />
    </Grid>
</DataTemplate>

没有DataType 就没有 IntelliSense,但据我所知,无法在 XAML 中将通用的 Dictionary 定义为 DataType

但要克服这个问题,您可以空实现 Dictionary 类型。

public class MyDictionary : Dictionary<DateTime, MyClass>
{
}


public class Whatever
{
    public MyDictionary MyDictionary { get { ... } set { ... } }

    ...
}

并将您的 DataTemplate 与您的新 Dictionary 类型一起使用:

<DataTemplate x:Key="Mytemplate" DataType="{x:Type local:MyDictionary}">
    ...
</DataTemplate>

【讨论】:

  • 感谢您指出 MyClass 中的字段问题。这就是问题所在。 KeyValue.Property 的绑定现在按预期工作!
猜你喜欢
  • 2020-11-05
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
  • 1970-01-01
相关资源
最近更新 更多