我在WPF XAML中使用DataGrid内的Combobox进行数据绑定时遇到了一些问题。
我有Signal Class,它有ObservableCollection。
public struct RawVal
{
public string name; //Name of the Value
public int value; // Value
}
public class Signal
{
public string Name { get; set; }
public Int32 Value { get; set; }
public ObservableCollection<RawVal> rawValue { get; set; }
};
在Stettings窗口中有
public partial class Settings : Window
{
public ObservableCollection<Signal> tempSigList { get; set; }
public ObservableCollection<RawVal> tempRawVal { get; set; }
.........
.........
this.grdSignal.ItemsSource = tempSigList;
}
我的XAML看起来像这样:
<DataGrid ItemsSource="{Binding}" Name="grdSignal">
<DataGrid.Columns>
<DataGridTemplateColumn Header=" RAW Value ">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding tempRawVal }" SelectedItem="Binding name" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
但没有任何结果ㅠㅠㅠ 我在stackoverflow上尝试了很多例子,但没有任何结果。 欢迎任何建议或解决方案。 谢谢
答案 0 :(得分:1)
下面:
窗口:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
tempSigList = new ObservableCollection<Signal>();
tempSigList.Add(new Signal { Name = "Name 1", Value = 1, rawValue = new ObservableCollection<RawVal> { new RawVal { name = "combo 1", value = 1 }, new RawVal { name = "combo 2", value = 2 } } });
tempSigList.Add(new Signal { Name = "Name 2", Value = 2, rawValue = new ObservableCollection<RawVal> { new RawVal { name = "combo 3", value = 3 }, new RawVal { name = "combo 4", value = 4 } } });
tempSigList.Add(new Signal { Name = "Name 3", Value = 3, rawValue = new ObservableCollection<RawVal> { new RawVal { name = "combo 5", value = 5 }, new RawVal { name = "combo 6", value = 6 } } });
tempSigList.Add(new Signal { Name = "Name 4", Value = 4, rawValue = new ObservableCollection<RawVal> { new RawVal { name = "combo 7", value = 7 }, new RawVal { name = "combo 8", value = 8 } } });
tempSigList.Add(new Signal { Name = "Name 5", Value = 5, rawValue = new ObservableCollection<RawVal> { new RawVal { name = "combo 9", value = 9 }, new RawVal { name = "combo 10", value = 10 } } });
dataGrid1.ItemsSource = tempSigList;
}
XAML:
<DataGrid x:Name="dataGrid1" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="RAW Value">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding rawValue}" SelectedItem="{Binding name}" DisplayMemberPath="name" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我使用了AutoGenerateColumns=true
。您可以将其设为false
并仅显示您的ComboBox
列。
答案 1 :(得分:0)
首先,Binding使用属性而不是类RawVal中的字段。
使用3列显示数据:
public partial class Win32599087 : Window
{
public ObservableCollection<Signal> tempSigList { get; set; }
public ObservableCollection<RawVal> tempRawVal { get; set; }
public Win32599087()
{
InitializeComponent();
tempRawVal = new ObservableCollection<RawVal>() {
new RawVal(){ Name="RawName1", Value=1},
new RawVal(){ Name="RawName2", Value=2},
new RawVal(){ Name="RawName3", Value=3},
new RawVal(){ Name="RawName4", Value=4}
};
tempSigList = new ObservableCollection<Signal>() {
new Signal(){Name="signal1", Value = 1, RawValues = tempRawVal}
};
SignalGrid.DataContext = tempSigList;
}
}
//////////////
<DataGrid x:Name="SignalGrid" AutoGenerateColumns="False" SelectedIndex="0" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Value" Binding="{Binding Value}"/>
<DataGridComboBoxColumn Header="RawValues">
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=RawValues}" />
<Setter Property="DisplayMemberPath" Value="Name" />
<Setter Property="SelectedValuePath" Value="Value" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
/////// 使用templatecolumn:
<DataGrid x:Name="SignalGridWithTemplates" AutoGenerateColumns="False" SelectedIndex="0" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Value" Binding="{Binding Value}"/>
<DataGridTemplateColumn Header="RawValues">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<ComboBox ItemsSource="{Binding RawValues}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Background="AliceBlue"/>
<TextBlock Text=" | "/>
<TextBlock Text="{Binding Value}" Background="Azure"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>