【发布时间】:2015-07-15 16:52:14
【问题描述】:
我目前有一个包含 2 列的 DataGrid,其中一列是 DataGridTextColumn,它将在字符串的 ObservableCollection 中显示项目,第二列是 DataGridTemplateColumn,它包含 DataGridCellTemplate,其中包含显示 Combobox 的 DataTemplate。组合框还绑定到字符串的 ObservableCollection。一切都按应有的方式绑定和显示。
我想要实现的是,由于需求的变化和功能的增加,我需要更改 Combobox 中显示的默认条目的索引(当前索引为 0 )。我有一个新字典,其中包含第一列中的条目作为键,值是第二列(组合框)中存在的字符串。
即:如何更改第二列中某些组合框的 selectedIndex 以“预选”从字典中获取的已知条目的索引?
一个最小的、完整的、可验证的例子:
我目前拥有的:
+--------------+-------------------+
| CustomerName | Location |
+--------------+-------------------+
| Bob | <Select Location> | <-- these are comboboxes
| John | <Select Location> |
| Katy | <Select Location> |
+--------------+-------------------+
XAML:
<DataGrid x:Name="CustomerLocationGrid" Height="265" VerticalAlignment="Center" Margin="-7,8,-2,-6" ItemsSource="{Binding CustomerNameList}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="customerDisplayList" Header="CustomerName" MinWidth="500" IsReadOnly="True" Binding="{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayIndex="0"/>
<DataGridTemplateColumn x:Name="networkTypeTemplateColumn" Header="Location" MinWidth ="200" CanUserResize="True" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="locationCombobox" ItemsSource="{Binding locationList, RelativeSource={RelativeSource AncestorType=UserControl}}" SelectedIndex="0" SelectionChanged="locationCombobox_SelectionChanged"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
地点:
public ObservableCollection<String> CustomerNameList { get; set; } //has a collection of Bob, John, Katy
public ObservableCollection<string> locationList { get; set;} //has locations like Minnesota, Iowa, New York
(列表来自不同的来源)
请注意,上面的 DataTemplate-Combobox XAML 是我从 this tutorial on WPF comboboxes 学到的(命名为“有史以来最好的 WPF Combobox 教程”)
以及我如何拥有第三个Dictionary<string,string>,它来自另一个具有Bob -> Minnesota、John -> Iowa 映射的来源,但 Katy 不存在,因为它无法预先确定,用户需要手动选择位置。
我想自动选择组合框内的索引,使表格看起来像这样,或者更改 Bob 和 Johns 的位置:
+--------------+-------------------+
| CustomerName | Location |
+--------------+-------------------+
| Bob | Minnesota | <-- set as the displayedIndex
| John | Iowa |
| Katy | <Select Location> |
+--------------+-------------------+
我的尝试: 我的方法是遍历每个 CustomerName 条目,在字典中查找,如果从字典返回值,则设置位置。字符串都是相同的,因此不需要匹配。
以编程方式循环遍历 DataGridTemplateColumn 以获取每个组合框并设置 selectedIndex,但未能获取每个客户组合框的索引以及客户名称
-
尝试利用
李>Initialized事件处理程序(这样做的灵感来自this answer)来使用对象(使用sender as ComboBox进行转换,但无法获取索引。
我也尝试过使用相关解决方案,例如 getting the index with VisualTreeHelper,但觉得太复杂(也无法获取父级)以及其他一些基于 VisualTreeHelper 方法的解决方案。
【问题讨论】:
标签: c# wpf xaml datagrid combobox