开发者

WPF ComboBox in DataGrid databinding/update not working

开发者 https://www.devze.com 2023-02-25 03:01 出处:网络
If I set up a new WPF application in Visual Studio 2010 and add the following code+XAML, a data grid opens with comboboxes inside. The problem now is that changeing a value through a combobox does not

If I set up a new WPF application in Visual Studio 2010 and add the following code+XAML, a data grid opens with comboboxes inside. The problem now is that changeing a value through a combobox does not get propagated to the bound data model. In other words: the property named MyValue never gets set. It took me hours now and I have no clue why this doesn't work. Also many similar threads and suggestions didn't.

Here the XAML. It is just a simple Window with a DataGrid contained. The DataGrid has a template column where CellTemplate and CellEditingTemplate are set. Both contain a ComboBox that is filled with the list from the resource section. The ComboBox.SelectedItem is bound to MyItem.MyValue:

<Window x:Class="DataGridComboBoxExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"    xmlns:local="clr-namespace:DataGridComboBoxExample">

    <Window.Resources>

        <local:MyItemList x:Key="ItemList"/>

        <DataTemplate x:Key="NotificationModeDataTemplate">
            <ComboBox
                ItemsSource="{StaticResource ItemList}"
                SelectedItem="{Binding Path=MyValue, Mode=OneWay}" />
        </DataTemplate>
        <DataTemplate x:Key="NotificationModeEditTemplate">
            <ComboBox
                ItemsSource="{StaticResource ItemList}"
                SelectedItem="{Binding Path=MyValue, Mode=TwoWay}" />
        </DataTemplate>

    </Window.Resources>

    <Grid>
        <DataGrid x:Name="myDataGrid" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn
                    Header="Test" Width="100"
                    CellTemplate="{StaticResource NotificationModeDataTemplate}"
                    CellEditingTemplate="{StaticResource NotificationModeEditTemplate}" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

Here the code. It contains the main Window ctor which just sets up a DataContext. MyItem is the row's datamodel supporting INotifyPropertyChanged. MyItemList is the list of choices bound to ComboBox.ItemsSource.

public partial class MainWindow : Window
{
    pub开发者_Python百科lic MainWindow()
    {
        InitializeComponent();

        myDataGrid.ItemsSource = new List<MyItem> 
        {
            new MyItem { MyValue = "i0" },
            new MyItem { MyValue = "i1" },
            new MyItem { MyValue = "i0" },
        };
    }
}

public class MyItem : INotifyPropertyChanged
{
    public string MyValue
    {
        get { return myValue; }
        set
        {
            myValue = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("MyValue"));
            }
        }
    }
    private string myValue;

    public event PropertyChangedEventHandler PropertyChanged;
}

public class MyItemList : List<string>
{
    public MyItemList() { Add("i0"); Add("i1"); Add("i2"); }
}


I suspect you'll need to make the SelectedItem binding update the source on PropertyChanged for this to work.

SelectedItem="{Binding Path=MyValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

I'd make the CellTemplate and CellEditingTemplate both reference your edit template while debugging this, to eliminate the other, irrelevant, template until you get it sorted out.

0

精彩评论

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