WPF - datagridcombobox - 从下拉列表中选择项目后更改显示的值

时间:2015-11-14 18:55:04

标签: c# wpf datagridcomboboxcolumn

我在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

你可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

以下是基于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)用于当组合仅显示选定值时的状态。因此,当梳状物丢失焦点时,将显示分数“键”,当您编辑组合选择值(选择值)时,将显示口头分数值。 此解决方案要求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; }
}
  1. 列创建代码(将其设置为列创建方法的一部分):

        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;
    
  2. 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>
    

    就是这样,如果代码出现问题,我将很乐意提供帮助。 方面。