C# CaliburnMicro:如何根据在数据网格中选择的项目自动在组合框中显示值?
C# CaliburnMicro: How can I automatically display a value in a combobox depending on which item is selected in a datagrid?
当用户从数据网格中选择一行时。我希望在组合框中自动选择该项目的颜色。目前,组合框对数据网格选择没有反应。
XAML:
<DataGrid x:Name="MyCollection" SelectedItem="{Binding MySelectedItem}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=ItemName}"/>
</DataGrid.Columns>
</DataGrid>
<ComboBox ItemsSource="{Binding ItemColours}" SelectedItem="{Binding MySelectedItem.Colour}"/>
<TextBox Text="{Binding MySelectedItem.Colour}" isEnabled="False"/>
视图模型:
public BindableCollection<Item> MyCollection { get; set; }
private Item_mySelectedItem;
public Item MySelectedItem
{
get { return _mySelectedItem; }
set
{
_mySelectedItem= value;
NotifyOfPropertyChange(() => MySelectedItem);
}
}
// Constructor
public Myclass()
{
MyCollection = GetData();
}
我检查过,组合框肯定连接到 SelectedItem.Colour,因为颜色也显示在别处的文本框中,并且当组合框选择被手动更改时更新。我希望 Combobox 具有与 TextBox 相同的功能和响应能力:
- 选择数据网格上的项目时,组合框显示项目的颜色;
- 手动选择不同的颜色会更改 SelectedItem.Colour 值。
- 理想情况下,即使在 isEnabled=False 时,ComboBox 选定项也会显示新 SelectedItem 的颜色,与 TextBox 值即使被禁用也会更新的方式相同。
我试过了SelectedItem="{Binding SelectedBall.Colour, Mode=TwoWay}"
,我看不出有什么不同。
感谢您的帮助。
104/5000
不知道你的ItemColours是什么数据,我觉得SelectedItem="{Binding myselecteditem.colour}"在ItemsSource里面找不到对应的值,所以没有反应?
问题是 ComboBox 正在比较两个 Color 对象以查看它是否应该显示颜色。例如,由于“Blue”的两个实例具有不同的哈希码,因此它总是 return false。解决方案是覆盖我的 Color class.
的 Equals() 和 GetHashCode() 方法
public partial class Colour
{
public override string ToString()
{
return Name;
}
public override bool Equals(object obj)
{
Colour otherColour;
try
{
otherColour = (Colour)obj;
}
catch (Exception)
{ return false; }
return (otherColour.Id == this.Id);
}
public override int GetHashCode()
{
return this.Id;
}
}
现在,当 Color class 的两个实例具有相同的 ColourID 时,它们将被视为相等。这意味着 ComboBox 将识别 SelectedItem.Colour 是其颜色之一。
当用户从数据网格中选择一行时。我希望在组合框中自动选择该项目的颜色。目前,组合框对数据网格选择没有反应。
XAML:
<DataGrid x:Name="MyCollection" SelectedItem="{Binding MySelectedItem}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=ItemName}"/>
</DataGrid.Columns>
</DataGrid>
<ComboBox ItemsSource="{Binding ItemColours}" SelectedItem="{Binding MySelectedItem.Colour}"/>
<TextBox Text="{Binding MySelectedItem.Colour}" isEnabled="False"/>
视图模型:
public BindableCollection<Item> MyCollection { get; set; }
private Item_mySelectedItem;
public Item MySelectedItem
{
get { return _mySelectedItem; }
set
{
_mySelectedItem= value;
NotifyOfPropertyChange(() => MySelectedItem);
}
}
// Constructor
public Myclass()
{
MyCollection = GetData();
}
我检查过,组合框肯定连接到 SelectedItem.Colour,因为颜色也显示在别处的文本框中,并且当组合框选择被手动更改时更新。我希望 Combobox 具有与 TextBox 相同的功能和响应能力:
- 选择数据网格上的项目时,组合框显示项目的颜色;
- 手动选择不同的颜色会更改 SelectedItem.Colour 值。
- 理想情况下,即使在 isEnabled=False 时,ComboBox 选定项也会显示新 SelectedItem 的颜色,与 TextBox 值即使被禁用也会更新的方式相同。
我试过了SelectedItem="{Binding SelectedBall.Colour, Mode=TwoWay}"
,我看不出有什么不同。
感谢您的帮助。
104/5000 不知道你的ItemColours是什么数据,我觉得SelectedItem="{Binding myselecteditem.colour}"在ItemsSource里面找不到对应的值,所以没有反应?
问题是 ComboBox 正在比较两个 Color 对象以查看它是否应该显示颜色。例如,由于“Blue”的两个实例具有不同的哈希码,因此它总是 return false。解决方案是覆盖我的 Color class.
的 Equals() 和 GetHashCode() 方法public partial class Colour
{
public override string ToString()
{
return Name;
}
public override bool Equals(object obj)
{
Colour otherColour;
try
{
otherColour = (Colour)obj;
}
catch (Exception)
{ return false; }
return (otherColour.Id == this.Id);
}
public override int GetHashCode()
{
return this.Id;
}
}
现在,当 Color class 的两个实例具有相同的 ColourID 时,它们将被视为相等。这意味着 ComboBox 将识别 SelectedItem.Colour 是其颜色之一。