在WPF控件中正确设置绑定 [英] Setting binding in WPF control correctly
问题描述
我想将一个控件数据源设置为可观察到的键值对集合,但仅显示该键值对的键部分.
I want to set a controls datasource to an observable collection of key value pairs, but to only display the key part of the pair.
我有一个第三方多选组合框.
I have a 3rd party multiselect combobox.
我修改了它附带的数据源类,以便它保存键值对集合.
I have modified the datasource class that came with it so that it holds key value pair collections.
这是课程:
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中,我有:
And in my xaml I have:
<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}"
有人可以帮助我吗?如果您需要更多信息,请告诉我.
Could anyone help me? Please let me know if you need anymore information.
推荐答案
对于常规组合框,您具有ValueMember
和DisplayMember
字段.
其工作方式类似于DisplayMemberPath.有关更好的说明,请参见此链接,但您的ComboBox应该看起来像这样:
For regular comboboxes you have the ValueMember
and the DisplayMember
field.
Which works similar to DisplayMemberPath. See this link for a better explanation, but your ComboBox should look like this:
<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" />
当然,我不确定这是否适用于您的第三方组合框.
Of course I'm not sure if this works with your third party combobox.
如果这不起作用,则可以在组合框的ItemTemplate
中使用值转换器.例如,将一个转换器添加到ItemTemplate的文本块中,在其中绑定整个对象,然后使用转换器返回要显示的键值.
If that will not work, you can use a value converter in the ItemTemplate
of the Combobox. For example add a converter to a textblock in the ItemTemplate, where you bind the whole object and use the converter to return the key value to display.
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}}"/>
这篇关于在WPF控件中正确设置绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!