WPF - datagridcombobox - 从下拉列表中选择项目后更改显示的值
WPF - datagridcombobox - change displayed value after selection of item from dropdown
我在 XAML 中指定了一个数据网格作为
<DataGrid Name="grAssessment">
</DataGrid>
列数根据用户的选择动态设置。
//define number of alternatives
int num = Alternatives.Children.Count - 1;
列是带有字典选项的组合框
Dictionary<int, string> scores = new Dictionary<int, string>();
scores.Add(1, "the same");
scores.Add(3, "moderate superiority");
scores.Add(5, "strong superiority");
scores.Add(7, "very strong superiority");
scores.Add(9, "extremely superiority");
我按以下方式添加新列
for (int i = 0; i < num; i++)
{
DataGridComboBoxColumn col = new DataGridComboBoxColumn();
grAssessment.Columns.Add(col);
col.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
col.ItemsSource = scores;
col.DisplayMemberPath = "Value";
col.SelectedValuePath = "Key";
}
我真正想要的是,当下拉菜单展开时,它包含字典中的值。但是在用户选择任何项目后,Key 应该显示在一个单元格中。
screen
你能帮我解决这个问题吗?
这是基于 xaml 的问题解决方案;
1. XAML代码:
<DataGridComboBoxColumn Header="Scores"
SelectedValueBinding="{Binding ScoreData, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{StaticResource ScoresAndDescription}"/>
<Setter Property="DisplayMemberPath" Value="Score"></Setter>
<Setter Property="IsReadOnly" Value="True"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{StaticResource ScoresAndDescription}"/>
<Setter Property="DisplayMemberPath" Value="ScoreVerbal"></Setter>
<Setter Property="IsReadOnly" Value="True"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle></DataGridComboBoxColumn>
当simpleDataGrid:ScoreData数组实际上是你映射对象的数组。您会注意到该解决方案使用了两种样式;当您 select 一个值时,一个用于组合编辑状态 (EditingElementStyle),当组合仅显示 selected 值时,第二个 (ElementStyle) 用于状态。因此,当组合失去对它的关注时,将显示得分 "Key",而当您编辑组合 selected 值(select 值)时,将显示口头得分值。
此解决方案要求 DataGridComboBoxColumn ItemsSource 是 key/value 对的集合,如下所示 xaml 代码:
<x:Array Type="simpleDataGrid:ScoreData" x:key>
<simpleDataGrid:ScoreData Score="1" ScoreVerbal="the same"/>
<simpleDataGrid:ScoreData Score="3" ScoreVerbal="moderate superiority"/>
<simpleDataGrid:ScoreData Score="5" ScoreVerbal="strong superiority"/>
<simpleDataGrid:ScoreData Score="7" ScoreVerbal="the samvery strong superioritye"/>
<simpleDataGrid:ScoreData Score="9" ScoreVerbal="extremely superiority"/>
</x:Array>
告诉我您是否需要版本背后的代码而不是 XAML 代码。
希望对您有所帮助,问候。
更新:
1. Column items源码(在后面的代码中设置即可):
private ObservableCollection<ScoreData> _scores = new ObservableCollection<ScoreData>(
new List<ScoreData>
{
new ScoreData{Score = 1, ScoreVerbal = "the same"},
new ScoreData{Score = 3, ScoreVerbal = "moderate superiority"},
new ScoreData{Score = 5, ScoreVerbal = "strong superiority"},
new ScoreData{Score = 7, ScoreVerbal = "the samvery strong superioritye"},
new ScoreData{Score = 9, ScoreVerbal = "extremely superiority"},
} );
2。分数数据代码:
public class ScoreData
{
public string ScoreVerbal { get; set; }
public int Score { get; set; }
}
列创建代码(将其设置为列创建方法的一部分):
var col = new DataGridComboBoxColumn();
dataGrid.Columns.Add(col);
col.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
col.ItemsSource = _scores;
col.Header = "Added In Code";
col.SelectedValueBinding = new Binding("ScoreData");
//bring the ElementStyle from the xaml code by its key
var elementStyle = this.FindResource("ElementStyle") as Style;
col.ElementStyle = elementStyle;
//bring the EditingElementStyle from the xaml code by its key
var editingElementStyle = this.FindResource("EditingElementStyle") as Style;
col.EditingElementStyle = editingElementStyle;
Xaml 代码(将其设置为 window.xaml 或 app.xaml 资源的一部分):
<Style x:Key="ElementStyle" TargetType="ComboBox">
<Setter Property="DisplayMemberPath" Value="Score"></Setter>
<Setter Property="IsReadOnly" Value="True"/>
</Style>
<Style x:Key="EditingElementStyle" TargetType="ComboBox">
<Setter Property="DisplayMemberPath" Value="ScoreVerbal"></Setter>
<Setter Property="IsReadOnly" Value="True"/>
</Style>
就是这样,如果代码有问题,我很乐意提供帮助。
关注
我在 XAML 中指定了一个数据网格作为
<DataGrid Name="grAssessment">
</DataGrid>
列数根据用户的选择动态设置。
//define number of alternatives
int num = Alternatives.Children.Count - 1;
列是带有字典选项的组合框
Dictionary<int, string> scores = new Dictionary<int, string>();
scores.Add(1, "the same");
scores.Add(3, "moderate superiority");
scores.Add(5, "strong superiority");
scores.Add(7, "very strong superiority");
scores.Add(9, "extremely superiority");
我按以下方式添加新列
for (int i = 0; i < num; i++)
{
DataGridComboBoxColumn col = new DataGridComboBoxColumn();
grAssessment.Columns.Add(col);
col.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
col.ItemsSource = scores;
col.DisplayMemberPath = "Value";
col.SelectedValuePath = "Key";
}
我真正想要的是,当下拉菜单展开时,它包含字典中的值。但是在用户选择任何项目后,Key 应该显示在一个单元格中。 screen
你能帮我解决这个问题吗?
这是基于 xaml 的问题解决方案; 1. XAML代码:
<DataGridComboBoxColumn Header="Scores"
SelectedValueBinding="{Binding ScoreData, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{StaticResource ScoresAndDescription}"/>
<Setter Property="DisplayMemberPath" Value="Score"></Setter>
<Setter Property="IsReadOnly" Value="True"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{StaticResource ScoresAndDescription}"/>
<Setter Property="DisplayMemberPath" Value="ScoreVerbal"></Setter>
<Setter Property="IsReadOnly" Value="True"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle></DataGridComboBoxColumn>
当simpleDataGrid:ScoreData数组实际上是你映射对象的数组。您会注意到该解决方案使用了两种样式;当您 select 一个值时,一个用于组合编辑状态 (EditingElementStyle),当组合仅显示 selected 值时,第二个 (ElementStyle) 用于状态。因此,当组合失去对它的关注时,将显示得分 "Key",而当您编辑组合 selected 值(select 值)时,将显示口头得分值。 此解决方案要求 DataGridComboBoxColumn ItemsSource 是 key/value 对的集合,如下所示 xaml 代码:
<x:Array Type="simpleDataGrid:ScoreData" x:key>
<simpleDataGrid:ScoreData Score="1" ScoreVerbal="the same"/>
<simpleDataGrid:ScoreData Score="3" ScoreVerbal="moderate superiority"/>
<simpleDataGrid:ScoreData Score="5" ScoreVerbal="strong superiority"/>
<simpleDataGrid:ScoreData Score="7" ScoreVerbal="the samvery strong superioritye"/>
<simpleDataGrid:ScoreData Score="9" ScoreVerbal="extremely superiority"/>
</x:Array>
告诉我您是否需要版本背后的代码而不是 XAML 代码。 希望对您有所帮助,问候。
更新: 1. Column items源码(在后面的代码中设置即可):
private ObservableCollection<ScoreData> _scores = new ObservableCollection<ScoreData>(
new List<ScoreData>
{
new ScoreData{Score = 1, ScoreVerbal = "the same"},
new ScoreData{Score = 3, ScoreVerbal = "moderate superiority"},
new ScoreData{Score = 5, ScoreVerbal = "strong superiority"},
new ScoreData{Score = 7, ScoreVerbal = "the samvery strong superioritye"},
new ScoreData{Score = 9, ScoreVerbal = "extremely superiority"},
} );
2。分数数据代码:
public class ScoreData
{
public string ScoreVerbal { get; set; }
public int Score { get; set; }
}
列创建代码(将其设置为列创建方法的一部分):
var col = new DataGridComboBoxColumn(); dataGrid.Columns.Add(col); col.Width = new DataGridLength(1, DataGridLengthUnitType.Star); col.ItemsSource = _scores; col.Header = "Added In Code"; col.SelectedValueBinding = new Binding("ScoreData"); //bring the ElementStyle from the xaml code by its key var elementStyle = this.FindResource("ElementStyle") as Style; col.ElementStyle = elementStyle; //bring the EditingElementStyle from the xaml code by its key var editingElementStyle = this.FindResource("EditingElementStyle") as Style; col.EditingElementStyle = editingElementStyle;
Xaml 代码(将其设置为 window.xaml 或 app.xaml 资源的一部分):
<Style x:Key="ElementStyle" TargetType="ComboBox"> <Setter Property="DisplayMemberPath" Value="Score"></Setter> <Setter Property="IsReadOnly" Value="True"/> </Style> <Style x:Key="EditingElementStyle" TargetType="ComboBox"> <Setter Property="DisplayMemberPath" Value="ScoreVerbal"></Setter> <Setter Property="IsReadOnly" Value="True"/> </Style>
就是这样,如果代码有问题,我很乐意提供帮助。 关注