我一直在寻找一种从我制作的datagrid组合框中获取数据的方法。特别是列中每个组合框的选定值。我是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项目具有string
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 values集合,或者如果您没有除集合之外的DataContext,则可以在XAML中定义它。
最后,在您的DataGrid中,您必须同时绑定Combos的ItemsSource和SelectedItem属性。它们将共享相同的ItemsSource,必须从放置集合的任何位置获取该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>