Obtinerea de date în Listă în DataGrid ComboBox

0

Problema

Am probleme cu obtinerea de elemente dintr-o listă la un combobox coloană într-un datagrid în WPF. Acest lucru este nou pentru mine asa ca orice ajutor ar fi apreciat foarte mult. Se pare că există multe moduri de a face asta, dar nu am fost capabil de a obține oricare dintre ele pentru a lucra.

"'

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

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

Lista cu Date în codul din spatele

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

UPDATE: Am sfârșit prin a adăuga o încărcare eveniment pentru a trage lista ca itemsource. Acum întrebarea este cum să obțineți valoarea selectată din combobox înapoi în bloc de text?

C# Adăugate pentru a obține combo încărcate.

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

    }
c# combobox datagrid wpf
2021-11-19 03:06:28
2

Cel mai bun răspuns

0

Acum întrebarea este cum să obțineți valoarea selectată din combobox înapoi în bloc de text?

Lega psnme proprietatea Positions obiect la Positionname proprietatea de date obiect:

<ComboBox x:Name="poscombo" Loaded="comboposloaded"
          SelectedValue="{Binding Positionname, UpdateSourceTrigger=PropertyChanged}"/>
2021-11-19 15:00:41
0

Datele cu caracter obligatoriu pentru DataGridComboBoxColumn pare un pic mai complicat decât ai putea crede.

  1. Utilizarea ObservableCollection în loc de Lista. Acest lucru va actualiza automat conținutul DataGrid.
  2. Puteți încărca datele într-un combo box utilizând ObjectDataProvide. Acest lucru va asigura, de asemenea, că datele de la sursă este actualizat automat. Aici este un exemplu de lucru.
    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>

enter image description here

Cred că ai nevoie pentru a vă familiariza cu MVVM model și de a folosi ca o baza pentru construirea unor astfel de aplicații.

2021-11-19 12:22:47

În alte limbi

Această pagină este în alte limbi

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................