希望您一切顺利,并以Guru编码。
这是Datagrid绑定问题,这让我痛苦了两天。我问了其他与此问题有关的问题并解决了,但这是另一个问题。
我有RawVal结构和信号类(具有ObservableCollection)。
public struct RawVal
{
public string name { get; set; }
public int value { get; set; }
}
public class Signal
{
public string Name { get; set; }
public Int32 Value { get; set; }
public ObservableCollection<RawVal> rawValue { get; set; }
}
现在XAML看起来像
<DataGrid ItemsSource="{Binding}" Name="grdSignal" Grid.Row="1" CanUserAddRows="False" AutoGenerateColumns="False" SelectionChanged="grdSignal_SelectionChanged_1">
<DataGrid.Columns>
<DataGridTextColumn Header=" Signal Name" Binding="{Binding Name}" Width="150"/>
<DataGridTemplateColumn Header=" Physical Value " Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding rawValue}" SelectedItem="Binding name" DisplayMemberPath="name" Name="cmbVal" SelectedIndex="0"
Visibility="{Binding Path=rawValue.Count, Converter={StaticResource ComboBoxItemCountToEnabledConverter}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header=" Value " Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding }" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
问题是,当用户从“物理值组合”框中选择任何项目时,与此项目相关的值需要显示在“值”文本框中。
例如,Signal类中的RawVal包含这些值。
4 - Search Key Trigger
3 - Tailgate Key Trigger
2 - Un-Lock Key Trigger
1 - Lock Key Trigger
0 - No Remote RQ Trigger
当用户选择“尾门按键触发器”时,文本框中将出现3。选择“无远程RQ触发器”时,为0。
欢迎任何建议和解决方案,谢谢。
您必须为INotifyPropertyChanged
实现了事件Signal
,并使用SelectedItem
您的属性Combobox
设置TextBox
column的值。
XAML代码:
<DataGrid ItemsSource="{Binding}" Name="grdSignal" Grid.Row="1" CanUserAddRows="False" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header=" Signal Name" Binding="{Binding Name}" Width="150"/>
<DataGridTemplateColumn Header=" Physical Value " Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding rawValue, Mode=OneWay}" SelectedItem="{Binding SelectedRaValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="name" Name="cmbVal"
Visibility="{Binding Path=rawValue.Count, Converter={StaticResource ComboBoxItemCountToEnabledConverter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header=" Value " Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding ComboValue}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
C#/代码落后
public class Signal: INotifyPropertyChanged
{
public string Name
{
get;
set;
}
public Int32 Value
{
get;
set;
}
private ObservableCollection < RawVal > rawValue1;
public ObservableCollection < RawVal > rawValue
{
get
{
return rawValue1;
}
set
{
rawValue1 = value;
OnPropertyChanged("rawValue");
if (value != null && value.Count > 0)
{
SelectedRaValue = value.First();
}
}
}
private RawVal selectedRaValue;
public RawVal SelectedRaValue
{
get
{
return selectedRaValue;
}
set
{
selectedRaValue = value;
OnPropertyChanged("SelectedRaValue");
ComboValue = value.name;
OnPropertyChanged("ComboValue");
}
}
public string ComboValue
{
get;
set;
}
#region Implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
现在您可以设置 DataContext
List < Signal > collection = new List < Signal > ();
collection.AddRange((new[]
{
new Signal
{
Name = "Hello",
Value = 1,
rawValue = new ObservableCollection < RawVal > ((new[] {
new RawVal { name = "A", value = 1},
new RawVal {name = "B", value = 1}
}).ToList()),
},
new Signal {
Name = "World",
Value = 2,
rawValue = new ObservableCollection < RawVal > ((new[] {
new RawVal {name = "A", value = 1},
new RawVal {name = "B", value = 1}
}).ToList()),
}
}).ToList());
this.DataContext = collection;
希望这可以帮助 !!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句