简体   繁体   English

WPF Datagrid C#中的ComboBox

[英]ComboBox in WPF Datagrid c#

I have a DataGrid and fill it with a DataTable. 我有一个DataGrid并用DataTable填充它。

dgMitarbeiter.ItemsSource = mainController.loadDataTableMitarbeiter().DefaultView;

this is the function: 这是功能:

public DataTable loadDataTableMitarbeiter() 
{
    loadMitarbeiterList();
    dtMitarbeiter.Clear();
    foreach (Mitarbeiter mitarbeiter in mitarbeiterList)
    {
        drMitarbeiter = dtMitarbeiter.NewRow();
        drMitarbeiter["ID"] = mitarbeiter.ID;
        drMitarbeiter["Vorname"] = mitarbeiter.vorname;
        drMitarbeiter["Nachname"] = mitarbeiter.nachname;
        drMitarbeiter["Kostenstelle"] = mitarbeiter.kostenstelle.id;
        drMitarbeiter["Größe Hose"] = mitarbeiter.gr_hose;
        drMitarbeiter["Größe Oberteil"] = mitarbeiter.gr_oberteil;
        drMitarbeiter["Gröse Schuhe"] = mitarbeiter.gr_schuhe;
        drMitarbeiter["Ferial"] = mitarbeiter.ferial;
        drMitarbeiter["Werk"] = mitarbeiter.werk;
        drMitarbeiter["Datum"] = mitarbeiter.creationDate.ToString("dd.MM.yyyy");
        dtMitarbeiter.Rows.Add(drMitarbeiter);
    }

    return dtMitarbeiter;
}

The Xaml: Xaml:

<DataGrid x:Name="dgMitarbeiter" AlternatingRowBackground="Gainsboro"  AlternationCount="2" ColumnWidth="*" HorizontalAlignment="Left" SelectedItem="{Binding SelectedItem}" Margin="10,22,0,0" VerticalAlignment="Top" Height="357" Width="731" CanUserAddRows="False" CanUserDeleteRows="False" RowEditEnding="dgMitarbeiter_RowEditEnding"  Background="White" HeadersVisibility="Column"/>

I need a ComboBox for the column "Kostenstelle" but have no idea how to achieve this. 我需要为“ Kostenstelle”列使用ComboBox,但不知道如何实现此目的。 Any ideas? 有任何想法吗?

You need to define a DataGridComboBoxColumn in your DataGrid columns, you can then bind the ItemsSource to wherever your combo box's options are located. 您需要在DataGrid列中定义一个DataGridComboBoxColumn ,然后可以将ItemsSource绑定到组合框选项所在的任何位置。

See here. 这里

My answer implements an ObservableCollection . 我的答案实现了一个ObservableCollection And adds this as a databinding to the ComboBox 并将其作为数据绑定添加到ComboBox

You need a new class: 您需要一门新课:

public class Kostenstellen: ObservableCollection<Kostenstelle>
{
}

And a fill Method with the following lines of code: 以及具有以下代码行的fill方法:

var kostenstellen = new Kostenstellen();

foreach mitarbeiter in mitarbeiterList
{
    kostenstellen.Add(mitarbeiter.kostenstelle);
}

var cvsCombobox = new CollectionViewSource() { Source = this.operationList };

this.myCombobox.SetBinding(ItemsControl.ItemsSourceProperty, new Binding() { Source = cvsCombobox });

Now there will be only "(Kostenstelle)" as a string in the Combobox . 现在,在组合Combobox只有“(Kostenstelle)”作为字符串。 So you need to override the ToString() method of your Kostenstelle class 因此,您需要重写Kostenstelle类的ToString()方法

public partial class Kostenstelle
{
    public override string ToString()
    {
        return this.ID.ToString();
    }
}

HINT: Use english variable and class names next time :) 提示:下次使用英语变量和类名:)

You can do a lot in your xaml file :) For myself I used this one lately... In the xaml file you can now define the placement of the combobox on your page. 您可以在xaml文件中做很多事情:)最近,我自己使用了这个文件...在xaml文件中,您现在可以定义组合框在页面上的位置。 you can set the content by code later. 您可以稍后通过代码设置内容。

<GridView
        x:Name="itemGridView"
        AutomationProperties.AutomationId="ItemGridView"
        AutomationProperties.Name="Items In Group"
        TabIndex="1"
        Grid.RowSpan="2"
        Padding="120,126,120,50"
        ItemsSource="{Binding}"
        SelectionMode="None"
        IsSwipeEnabled="false"
        IsItemClickEnabled="True"
        ItemClick="ItemView_ItemClick"
        >
        <GridView.ItemTemplate >
            <DataTemplate >
                <Grid Height="150" Width="480"  Background="{StaticResource  ListViewItemPlaceholderBackgroundThemeBrush}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>


                    <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="10,10,0,0" >

                        <TextBlock Text="{Binding title}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap" FontSize="25"/>
                        <Line/>
                        <TextBlock Text="{Binding subtitle}" Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="NoWrap" FontSize="20" Margin="0,10,0,0" />
                        <Line/>
                        <TextBlock Text="{Binding description}" Style="{StaticResource BodyTextBlockStyle}" MaxHeight="60" FontSize="15" Margin="0,10,0,0"/>
                        <Button Tag="{Binding title}" Click="ItemButtonClicked" Content="Details" FontSize="15" Margin="0,10,0,0"/>
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </GridView.ItemTemplate>
        <GridView.ItemContainerStyle>
            <Style TargetType="FrameworkElement" >
                <Setter Property="Margin" Value="52,0,0,2"/>

            </Style>
        </GridView.ItemContainerStyle>
    </GridView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM