开发者

Show ComboBox on Datagrid row edit, WPF

开发者 https://www.devze.com 2023-01-21 11:47 出处:网络
I have a DataGrid with data in my WPF 4 desktop-based application. When a user clicks twice on row DataGrid switches to edit mode, where a user can change values of a cell. Now I want that on specif开

I have a DataGrid with data in my WPF 4 desktop-based application. When a user clicks twice on row DataGrid switches to edit mode, where a user can change values of a cell. Now I want that on specif开发者_如何转开发ic cell user can only choose values from the combo box, e.g. gender — male/female and not to type something else.

How can I show combo box in edit mode of DataGrid?


A number of different ways actually,

Binding to a enum

public enum ChoiseEnum
{
    Yes,
    No,
    Maybe
}

First you're gonna need an ObjectDataProvider

xmlns:sys="clr-namespace:System;assembly=mscorlib"

<ObjectDataProvider MethodName="GetValues"
                    ObjectType="{x:Type sys:Enum}"
                    x:Key="ChoiseEnumDataProvider">
    <ObjectDataProvider.MethodParameters>
        <x:Type TypeName="local:ChoiseEnum" />
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

Then we can bind a DataGridComboBoxColumn to a Property called Choise like this

<DataGrid Name="c_dataGrid"
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridComboBoxColumn Header="Choise"
                                SelectedItemBinding="{Binding Choise}"
                                ItemsSource="{Binding Source={StaticResource ChoiseEnumDataProvider}}"/>
    </DataGrid.Columns>
</DataGrid>

Only show values from a static List in the ComboBox

UPDATE
Added more details

namespace ComboBoxDataGrid
{
    public class TestClass
    {
        static TestClass()
        {
            ChoiseData = new List<string>();
            ChoiseData.Add("Yes");
            ChoiseData.Add("No");
            ChoiseData.Add("Maybe");
        }
        public static List<string> ChoiseData
        {
            get;
            set;
        }

        public TestClass()
        {
            SelectedChoise = string.Empty;
        }
        public TestClass(string selectedChoise)
        {
            SelectedChoise = selectedChoise;
        }
        public string SelectedChoise
        {
            get;
            set;
        }
    }
}

public partial class WinWorkers: Window
{
    public WinWorkers()
    {
        InitializeComponent();
        TestClasses = new ObservableCollection<TestClass>();
        TestClasses.Add(new TestClass("Yes1"));
        TestClasses.Add(new TestClass("No"));
        TestClasses.Add(new TestClass("Maybe"));
        c_dataGrid.ItemsSource = TestClasses;
    }

    public ObservableCollection<TestClass> TestClasses
    {
        get;
        set;
    }
}

<Window x:Class="ComboBoxDataGrid.WinWorkers"
        xmlns:local="clr-namespace:ComboBoxDataGrid"
        ...>
    <Window.Resources>
        <local:TestClass x:Key="TestClass" />
    </Window.Resources>
    <Grid>
        <DataGrid Name="c_dataGrid"
                  AutoGenerateColumns="False"
                  RowHeaderWidth="100">
            <DataGrid.Columns>
                <DataGridComboBoxColumn Header="Choise_StaticList"
                                        SelectedValueBinding="{Binding SelectedChoise}"
                                        ItemsSource="{Binding Source={StaticResource TestClass}, Path=ChoiseData}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>
0

精彩评论

暂无评论...
验证码 换一张
取 消