views:

44

answers:

1

Hi,

I have a DataGrid with data in my WPF 4 desktop-based application. When user click twice on row DataGrid switches to edit mode, where user can change values of cell. Now I want that on specific cell user could only choose values from combobox, e.g. sex — mail/female and not to type something else.

How can I show comboox on edit mode of DataGrid?

Thanks.

+1  A: 

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>
Meleak
I've tried your last way, but actually no binding is performed, I see a clean combobox. My window is called WinWorkers. I've created a list of values in WinWorkers.cs added getter and setter, as you mentioned. In WinWorkers.xaml I put DataGridComboBoxColumn and setuped ItemSource in differents way, but it doesn't work. e.g. ItemsSource="{Binding Path=ListName}" doesn't work. How can I connect to list «genderList», created in the same class? Thanks.
Toucki
Updated the second example with more details
Meleak