我一直在寻找一种从我创建的数据网格组合框中获取数据的方法.特别是来自列的每个组合框的选定值.我是
WPF的新手,如果有人愿意,我将非常感激.
谢谢
XAML:
<DataGrid x:Name="tb" Margin="5,51,5,5" ItemsSource="{Binding}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridCheckBoxColumn Header="Include" Binding="{Binding Include}"/>
<DataGridTemplateColumn Header="Measure" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Margin="2" ItemsSource="{Binding Measure}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
C#:
ObservableCollection<State> items = new ObservableCollection<State>();
foreach (string col in columns)
{
items.Add(new State()
{
Name = col,
Include = true,
Measure = new ObservableCollection<string>() { "Sum", "Average" }
});
}
DataContext = items;
列的图片:
最佳答案 我觉得你很困惑.我假设您希望您的State项具有字符串Measure属性,而不是它们的集合,但您希望能够从具有多个选项的Combo中选择Measure值…
如果是这种情况,那么您应该将Measure属性重新定义为单个字符串,而不是集合.
ObservableCollection<State> items = new ObservableCollection<State>();
foreach (string col in columns)
{
items.Add(new State()
{
Name = col,
Include = true,
Measure = string.Empty // Initialize it to whatever you want
});
}
DataContext = items;
然后在DataContext中的其他位置创建Measure值集合,或者如果除了集合之外没有DataContext,则可以在XAML中定义它.
最后,在DataGrid中,您必须绑定Combos的ItemsSource和SelectedItem属性.它们将共享相同的ItemsSource,它必须从放置集合的任何位置获取,而不是行的DataContext:
<DataGrid x:Name="tb" Margin="5,51,5,5" ItemsSource="{Binding}"
AutoGenerateColumns="False">
<DataGrid.Resources>
<!-- This is where the values are defined -->
<col:ArrayList x:Key="MeasureValues">
<sys:String>Sum</sys:String>
<sys:String>Average</sys:String>
</col:ArrayList>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridCheckBoxColumn Header="Include" Binding="{Binding Include}"/>
<DataGridTemplateColumn Header="Measure" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Margin="2"
SelectedItem="{Binding Measure}"
ItemsSource="{StaticResource MeasureValues}" />
<!-- And this is where you consume those values -->
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
如果你不想把值放在XAML中,你必须在另一个DataContext中定义集合并以某种方式创建一个绑定(使用RelativeSource,ElementName和类似的东西).
当然,这也适用于DataGridComboBoxColumn:
<DataGrid x:Name="tb" Margin="5,51,5,5" ItemsSource="{Binding}"
AutoGenerateColumns="False">
<DataGrid.Resources>
<!-- This is where the values are defined -->
<col:ArrayList x:Key="MeasureValues">
<sys:String>Sum</sys:String>
<sys:String>Average</sys:String>
</col:ArrayList>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridCheckBoxColumn Header="Include" Binding="{Binding Include}"/>
<DataGridComboBoxColumn Header="Measure" Width="*"
SelectedItemBinding="{Binding Measure}"
ItemsSource="{StaticResource MeasureValues}" />
<!-- And this is where you consume those values -->
</DataGrid.Columns>
</DataGrid>