这是您的问题的基于 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) 用于组合仅显示选定值时的状态。因此当combo失去焦点时,会显示分数“Key”,而当您编辑combo选择值(选择值)时,会显示口头分数值。
此解决方案要求 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。 ScoreData 代码:
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>
就是这样,如果代码出现问题,我很乐意提供帮助。
请注意。