【问题标题】:How to bind Dictionary object to checkedlistbox using WPF如何使用 WPF 将字典对象绑定到选中列表框
【发布时间】:2012-08-16 07:15:20
【问题描述】:

我有一个通用字典集合 Dictionary。我需要将 displaymember 路径键绑定到复选框的内容,并将复选框 Ischecked 属性绑定到 Dictionary 的值成员

 private Dictionary<string, bool> _columnHeaderList;
    public Dictionary<string, bool> ColumnHeaderList
    {
        get { return _columnHeaderList; }
        set { _columnHeaderList = value; RaisePropertyChanged("ColumnHeaderList"); }
    }

    private Dictionary<string, bool> GetColumnList()
    {
        Dictionary<string, bool> dictColumns = new Dictionary<string, bool>();
        Array columns = Enum.GetValues(typeof(ColumnHeaders));
        int arrayIndex=0;
        for(int i=0;i<columns.Length;i++)
        {
            dictColumns.Add(columns.GetValue(arrayIndex).ToString(), true);
        }
        return dictColumns;

    }

我的 XAML 看起来像

 <ListBox Grid.Column="0" Grid.Row="1" Height="200" 
             ItemsSource="{Binding ColumnHeaderList}"
             VerticalAlignment="Top">
        <ListBox.ItemTemplate>
            <HierarchicalDataTemplate>
                <CheckBox Content="{Binding key}" IsChecked="{Binding Path=Value}"></CheckBox>
            </HierarchicalDataTemplate>               
        </ListBox.ItemTemplate>           
    </ListBox>

【问题讨论】:

  • 您能否添加编译所需的代码? (RaisePropertyChanged 和 ColumnHeaders)?

标签: wpf checkedlistbox


【解决方案1】:

如果绑定到 Dictionary,则需要使用 OneWay 绑定,因为 KeyValuePair 具有只读属性。

<CheckBox Content="{Binding Key, Mode=OneWay}" IsChecked="{Binding Path=Value, Mode=OneWay}" Width="100" /></CheckBox>

确保您已设置 DataContext。请注意,当用户按下复选框时,这不会更新字典值。

【讨论】:

    【解决方案2】:

    由于 Value 属性是只读的,并且如果用户选中或取消选中复选框,OneWay 绑定将不允许您跟踪更改。建议将它们与数组新类 ListItem 绑定:

    class ListItem
    {
        public string Text { get; set; }
        public bool IsChecked { get; set; }
    }
    
    private ListItem[] GetColumnList()
    {
        return Enum.GetValues(typeof(ColumnHeaders))
                   .Select(h => new ListItem{ Text = h.ToString(),IsChecked = true})
                   .ToArray();
    
    }
    

    【讨论】:

      【解决方案3】:

      是的,它可能并且它也应该可以工作,尽管您需要将绑定模式的值绑定为OneWay,因为字典值自只读以来无法设置。如果你想改变这个值,你可以钩住Command(if following MVVVM)或者可以在Checked event后面的代码中处理。

      另外,Key 的绑定不正确,请将您的 key 替换为 Key。你最终的 xaml 应该是这样的 -

      <ListBox Grid.Column="0" Grid.Row="1" Height="200" 
                   ItemsSource="{Binding ColumnHeaderList}"
                   VerticalAlignment="Top">
         <ListBox.ItemTemplate>
             <DataTemplate>
                 <CheckBox Content="{Binding Key}"
                           IsChecked="{Binding Path=Value, Mode=OneWay}"/>
              </DataTemplate>               
         </ListBox.ItemTemplate>           
      </ListBox>
      

      请注意,我已将 HierarchicalDataTemplate 更改为 DataTemplate,因为我在模板中看不到任何层次结构。

      【讨论】:

        猜你喜欢
        • 2014-11-05
        • 1970-01-01
        • 2018-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多