WPF-数据网格对话框-从下拉列表中选择项目后更改显示值
本文关键字:项目 选择 显示 下拉列表 数据网 数据 网格 对话框 WPF- | 更新日期: 2023-09-27 18:21:59
我在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";
}
我真正想要的是,当下拉列表展开时,它包含字典中的值。但在用户选择任何项目后,键都应该显示在一个单元格中。屏幕
你能帮我做这件事吗?
这是一个基于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数组实际上是映射对象的数组时。您可以注意到,该解决方案使用了两种样式;一个用于选择值时的组合编辑状态(EditingElementStyle),另一个用于组合仅显示选定值时的状态(ElementStyle)。因此,当组合失去焦点时,将显示分数"Key",而当您编辑组合选择值(选择值)时,会显示口头分数值。此解决方案要求DataGridComboBoxColumn ItemsSource是键/值对的集合,如以下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.列项目源代码(只需在代码后面设置):
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>
仅此而已,如果代码出现问题,我将很乐意提供帮助。看待