正确设置WPF控件中的绑定

时间:2012-02-15 16:18:21

标签: c# wpf binding

我想将一个控件数据源设置为一个可观察的键值对集合,但只显示该对的关键部分。

我有第三方多选组合框。

我修改了随附的数据源类,以便它保存键值对集合。

这是班级:

public class DataSource : INotifyPropertyChanged
    {
        private ObservableCollection<KeyValuePair<string,string>> _items;

        public DataSource(ObservableCollection<KeyValuePair<string, string>> items)
        {
            _items = items;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion




        public ObservableCollection<KeyValuePair<string, string>> Items
        {
            get { return _items; }
        }

        private string _selectedItem = "";
        public string SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                OnPropertyChanged("SelectedItem");
            }
        }

        private ObservableCollection<KeyValuePair<string, string>> _selectedItems;
        public ObservableCollection<KeyValuePair<string, string>> SelectedItems
        {
            get
            {
                if (_selectedItems == null)
                {
                    _selectedItems = new ObservableCollection<KeyValuePair<string, string>> { new KeyValuePair<string,string>("ALL"," ") };
                    SelectedItemsText = WriteSelectedItemsString(_selectedItems);
                    _selectedItems.CollectionChanged +=
                        (s, e) =>
                        {
                            SelectedItemsText = WriteSelectedItemsString(_selectedItems);
                            OnPropertyChanged("SelectedItems");
                        };
                }
                return _selectedItems;
            }
            set
            {
                _selectedItems = value;
            }
        }

        public string SelectedItemsText
        {
            get { return _selectedItemsText; }
            set
            {
                _selectedItemsText = value;
                OnPropertyChanged("SelectedItemsText");
            }
        } string _selectedItemsText;


        private static string WriteSelectedItemsString(IList<KeyValuePair<string, string>> list)
        {
            if (list.Count == 0)
                return String.Empty;

            StringBuilder builder = new StringBuilder(list[0].Key);

            for (int i = 1; i < list.Count; i++)
            {
                builder.Append(", ");
                builder.Append(list[i].
);
            }

            return builder.ToString();
        }
    }

在我背后的代码中:

DataSource ds = new DataSource(materialNames);
            cmbLastEditors.DataContext = ds;

在我的xaml中我有:

<my1:MultiComboBox Name="cmbLastEditors"  Grid.Row="8" Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" SelectionMode="Multiple" DisplaySeparator=", " ItemsSource="{Binding Items}" SelectedItems="{Binding SelectedItems}"  />

我想做点什么

ItemsSource="{Binding Items.Key}"

有人能帮帮我吗?如果您需要更多信息,请告诉我。

2 个答案:

答案 0 :(得分:4)

您可以使用ItemTemplate渲染列表中的每个项目。像这样:

<my1:MultiComboBox ...>
    <my1:MultiComboBox.ItemTemplate>
        <DataTemplate>
             <TextBlock Text="{Binding Key}"/>
        </DataTemplate>
    <my1:MultiComboBox.ItemTemplate>
</my1:MultiComboBox>

也就是说,假设您的自定义MultiComboBox支持正常的WPF ItemTemplate语义。

答案 1 :(得分:3)

对于常规组合框,您有ValueMemberDisplayMember字段。 其工作方式与DisplayMemberPath类似。有关更好的说明,请参阅this链接,但您的ComboBox应如下所示:

<my1:MultiComboBox Name="cmbLastEditors"  
                   Grid.Row="8" Grid.Column="1" 
                   VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
                   SelectionMode="Multiple" DisplaySeparator=", " 
                   ItemsSource="{Binding Items}" 
                   SelectedItems="{Binding SelectedItems}" 
                   DisplayMemberPath="Key" />

当然我不确定这是否适用于你的第三方组合框。

如果这不起作用,您可以在Combobox的ItemTemplate中使用值转换器。例如,将转换器添加到ItemTemplate中的文本块,您可以在其中绑定整个对象并使用转换器返回要显示的键值。

class MyKeyValueConverter : IValueConverter
{
    public object Convert(object aValue)
    {
        var pair = aValue as KeyValuePair<string, string>;
        return pair.Key;
    }
}


<TextBlock Text="{Binding Path=., Converter={StaticResource myKeyValueConverter}}"/>