【问题标题】:Getting data in List into DataGrid ComboBox将 List 中的数据放入 DataGrid ComboBox
【发布时间】:2021-12-29 21:44:06
【问题描述】:

我在 WPF 的数据网格中将项目从列表中获取到组合框列时遇到问题。这对我来说是新的,所以任何帮助将不胜感激。似乎有很多方法可以做到这一点,但我无法让其中任何一种工作。

'''

                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Positionname}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>

                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox x:Name="poscombo Loaded="comboposloaded"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            
            </DataGridTemplateColumn>

在后面的代码中列出数据

 List<Positions> PositionList = new List<Positions>();

更新: 我最终添加了一个加载事件以将列表作为 itemsource 拉取。现在的问题是如何将组合框中的选定值返回到文本块中?

添加 C# 以加载组合。

    private void comboposloaded(object sender, RoutedEventArgs e)
    {
        ComboBox cmb = (ComboBox)sender;
        cmb.ItemsSource = PositionList;
        cmb.DisplayMemberPath = "info";
        cmb.SelectedValuePath = "psnme";

    }

【问题讨论】:

  • 使用数据绑定。看我的回答。

标签: c# wpf combobox datagrid


【解决方案1】:

现在的问题是如何将组合框中的选定值返回到文本块中?

Positions对象的psnme属性绑定到数据对象的Positionname属性:

<ComboBox x:Name="poscombo" Loaded="comboposloaded"
          SelectedValue="{Binding Positionname, UpdateSourceTrigger=PropertyChanged}"/>

【讨论】:

    【解决方案2】:

    DataGridComboBoxColumn 的数据绑定看起来比您想象的要复杂一些。

    1. 使用 ObservableCollection 而不是 List。这将自动更新 DataGrid 的内容。
    2. 您可以使用 ObjectDataProvide 将数据加载到组合框中。这也将确保源中的数据自动更新。 这是一个工作示例。
        public partial class MainWindow : Window
        {
            public EmployeeViewModel EmployeeVM;
            public MainWindow()
            {
                InitializeComponent();
                EmployeeVM = new EmployeeViewModel();
                MyDataGrid.ItemsSource = EmployeeVM.EmployeeList;
            }
        }
    
        public class Employee
        {
            public string Name { get; set; }
            public PositionEnum Position { get; set; }
        }
    
        public enum PositionEnum { Marketeer, Mechanic, Accountant };
        public class EmployeeViewModel 
        {
            public ObservableCollection<Employee> EmployeeList =
              new ObservableCollection<Employee>();
            public EmployeeViewModel()
            { 
                EmployeeList.Add(new Employee()
                 { Name = "James Smith", Position = PositionEnum.Accountant });
                EmployeeList.Add(new Employee()
                 { Name = "Robert Johnson", Position = PositionEnum.Marketeer });
                EmployeeList.Add(new Employee() 
                 { Name = "David Williams", Position = PositionEnum.Mechanic });
            }
        }
    

    XAML

    <Window x:Class="WpfApp6.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:core="clr-namespace:System;assembly=mscorlib"
            xmlns:local="clr-namespace:WpfApp6"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800" >
        <Window.Resources>
            <!--Create list of enumeration values-->
            <ObjectDataProvider x:Key="myEnum" MethodName="GetValues" ObjectType="{x:Type core:Enum}">
                <ObjectDataProvider.MethodParameters>
                    <x:Type Type="local:PositionEnum"/>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
        </Window.Resources>
        <Grid>
            <DataGrid x:Name="MyDataGrid"
                AutoGenerateColumns="False"      
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                RowHeaderWidth="0" >
                <DataGrid.Columns>
                    <DataGridTextColumn
                       Binding="{Binding Name}"
                       Header="Name" />
                    <DataGridComboBoxColumn
                        Header="Order Status"
                        SelectedItemBinding="{Binding Position}"
                        ItemsSource="{Binding Source={StaticResource myEnum}}" />
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>
    

    认为您需要熟悉 MVVM 模式并将其用作构建此类应用程序的基础。

    【讨论】:

      猜你喜欢
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 2014-05-16
      • 2011-05-06
      • 2016-04-30
      相关资源
      最近更新 更多