【问题标题】:DataGridTextColumn Binding to IDictionaryDataGridTextColumn 绑定到 IDictionary
【发布时间】:2012-04-29 22:22:32
【问题描述】:

我有一个包含用户集合的 ObservableCollection

class User
{
  public int Id{ get; set; }
  public UserType UserType{ get; set; }
}

还有一个枚举

enum UserType
{
  Admin,
  Moderator,
  User,
  Guest
}

然后我有一个名为 UserTypeMap 的枚举映射器,可以从 ViewModel 访问

public class UserListViewModel : INotifyPropertyChanged
{
  ......
  public ObservableCollection<User> Users
  {
    get{ return this.users; }
    set{ this.users = value; this.InvokePropertyChanged( "Users" );
  }
  public IDictionary<UserType, string> UserTypeMap{ get; set; }
  {
    get{ return this.userTypeMap; }
    set{ this.userTypeMap= value; this.InvokePropertyChanged( "UserTypeMap" ); 
  }
  ....
}

在客户端,我创建了一个包含用户列表的 DataGrid

<sdk:DataGrid ItemsSource="{Binding Users}">
  <sdk:DataGridTextColumn Binding="{Binding Id}" Header="ID"/>
  <sdk:DataGridTextColumn Binding="{**??????????**}" Header="Type"/>
</sdk>

我不知道如何将字典值绑定到DataGridTextColumn, 我之前尝试使用组合框,使用 SelectedValue 和 ItemsSource 并且它有效, 但我想在文本框/标签上显示它,而不是在组合框上...

有没有一种纯粹的 xaml 方式(不是页面背后的代码)来做到这一点?

谢谢, 会

【问题讨论】:

    标签: wpf silverlight xaml data-binding datagrid


    【解决方案1】:

    不是您问题的答案,但我认为只显示您不需要 UserTypeMap 字典的枚举。

    <sdk:DataGridTextColumn Binding="{Binding UserType, Mode=OneWay, StringFormat={}{0}}" Header="Type"/>
    

    【讨论】:

    • 我不想显示键(枚举),我想显示字符串的值。例如,如果我有密钥管理员并且我想显示“管理员”,但无论如何谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    相关资源
    最近更新 更多