【问题标题】:WPF - datagridcombobox - change displayed value after selection of item from dropdownWPF - datagridcombobox - 从下拉列表中选择项目后更改显示值
【发布时间】:2023-03-04 15:54:01
【问题描述】:

我在 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";                 
    }

我真正想要的是,当下拉菜单展开时,它包含字典中的值。但是在用户选择任何项目后,密钥应显示在单元格中。 screen

您能帮我解决这个问题吗?

【问题讨论】:

  • 可能类似于this?

标签: c# wpf datagridcomboboxcolumn


【解决方案1】:

这是您的问题的基于 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; }
}
  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>
    

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

【讨论】:

  • 感谢提供的解决方案,但我真正需要的是背后的代码。如果您能帮助我,将不胜感激。
  • @Irina 看看这个,一定有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-10
  • 1970-01-01
  • 2012-11-15
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多