将 属性 从 ItemsSource 中的元素绑定到标签内容
Bind property from element in ItemsSource to Label Content
我已经创建了自己的用户控件。此控件有自己的 属性 ItemsSource
类型 Dictionary<string, object>
。键 - 它是我绑定到 ItemsSource 的 collection 中元素的标题。
我可以访问 ItemsSource
的任何 属性 而无需将它的值单独添加到 ItemsSource
(不要转换为 List<Tuple<string, string, object>>
)
public class Book
{
public int Id{get;set}
public string Title{get;set;}
public string Description{get;set;}
}
var list = new List<Book>(){//initializing};
userControl.ItemsSource = list.ToDictionary(i => i.Title, i => i);
所以我想访问 Description
如果我只有 ItemsSource
。可能吗?
我的UserControl和这里写的一样MultipleComboBox
我像这样绑定 ItemsSource :
<controls:MultiSelectComboBox SelectedItems="{Binding SelectedBooks, Mode=TwoWay}" x:Name="Books" DefaultText="Category" ItemsSource="{Binding Books}"/>
我能想到的解决方案- 将属性 添加到class 节点,它将使用ItemsSource 的值属性 进行初始化。绑定后像 Value.Description.
public class 节点:INotifyPropertyChanged
{
private string _title;
private object _value;
private bool _isSelected;
#region ctor
public Node(string title, object value)
{
Title = title;
Value = value;
}
#endregion
#region Properties
public string Title
{
get
{
return _title;
}
set
{
_title = value;
NotifyPropertyChanged("Title");
}
}
public object Value
{
get { return _value; }
set
{
_value = value;
NotifyPropertyChanged("Value");
}
}
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
NotifyPropertyChanged("IsSelected");
}
}
#endregion
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
但这是好的解决方案吗?从性能方面。谢谢
您必须设置 ItemTemplate
<ComboBox ItemsSource="{Binding Books}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Key}"/>
<TextBlock Text="{Binding Value.Description}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我已经创建了自己的用户控件。此控件有自己的 属性 ItemsSource
类型 Dictionary<string, object>
。键 - 它是我绑定到 ItemsSource 的 collection 中元素的标题。
我可以访问 ItemsSource
的任何 属性 而无需将它的值单独添加到 ItemsSource
(不要转换为 List<Tuple<string, string, object>>
)
public class Book
{
public int Id{get;set}
public string Title{get;set;}
public string Description{get;set;}
}
var list = new List<Book>(){//initializing};
userControl.ItemsSource = list.ToDictionary(i => i.Title, i => i);
所以我想访问 Description
如果我只有 ItemsSource
。可能吗?
我的UserControl和这里写的一样MultipleComboBox
我像这样绑定 ItemsSource :
<controls:MultiSelectComboBox SelectedItems="{Binding SelectedBooks, Mode=TwoWay}" x:Name="Books" DefaultText="Category" ItemsSource="{Binding Books}"/>
我能想到的解决方案- 将属性 添加到class 节点,它将使用ItemsSource 的值属性 进行初始化。绑定后像 Value.Description.
public class 节点:INotifyPropertyChanged {
private string _title;
private object _value;
private bool _isSelected;
#region ctor
public Node(string title, object value)
{
Title = title;
Value = value;
}
#endregion
#region Properties
public string Title
{
get
{
return _title;
}
set
{
_title = value;
NotifyPropertyChanged("Title");
}
}
public object Value
{
get { return _value; }
set
{
_value = value;
NotifyPropertyChanged("Value");
}
}
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
NotifyPropertyChanged("IsSelected");
}
}
#endregion
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
但这是好的解决方案吗?从性能方面。谢谢
您必须设置 ItemTemplate
<ComboBox ItemsSource="{Binding Books}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Key}"/>
<TextBlock Text="{Binding Value.Description}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>