【问题标题】:WPF ComboBox in DataGrid databinding/update not workingDataGrid 数据绑定/更新中的 WPF 组合框不起作用
【发布时间】:2011-08-05 20:16:04
【问题描述】:

如果我在 Visual Studio 2010 中设置新的 WPF 应用程序并添加以下代码+XAML,则会打开一个包含组合框的数据网格。现在的问题是通过组合框更改值不会传播到绑定数据模型。换句话说:名为 MyValue 的属性永远不会被设置。我现在花了几个小时,我不知道为什么这不起作用。也有许多类似的主题和建议没有。

这里是 XAML。它只是一个包含 DataGrid 的简单窗口。 DataGrid 有一个模板列,其中设置了 CellTemplate 和 CellEditingTemplate。两者都包含一个 ComboBox,其中填充了资源部分中的列表。 ComboBox.SelectedItem 绑定到 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>

这里是代码。它包含仅设置 DataContext 的主 Window ctor。 MyItem 是支持 INotifyPropertyChanged 的​​行的数据模型。 MyItemList 是绑定到 ComboBox.ItemsSource 的选项列表。

public partial class MainWindow : Window
{
    public 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"); }
}

【问题讨论】:

    标签: .net wpf data-binding datagrid combobox


    【解决方案1】:

    我怀疑您需要让 SelectedItem 绑定更新 PropertyChanged 上的源才能使其正常工作。

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

    我会让 CellTemplate 和 CellEditingTemplate 在调试时都引用你的编辑模板,以消除另一个不相关的模板,直到你把它整理出来。

    【讨论】:

    • 我刚刚测试了它,它对我有用。我让 CellTemplate 和 CellEditingTemplate 都引用了编辑模板,并添加了 UpdateSourceTrigger=PropertyChanged。当我在组合框中选择一个项目时,会调用 MyValue 设置器。我正在针对安装了 VS 2010 SP1 的 .NET 4.0 进行测试。
    • 为什么这真的有效?如果我将相同的 ComboBox 移到 DataGrid 之外(只是一个列表),则相同的视图模型不需要。
    猜你喜欢
    • 2018-07-27
    • 2015-12-17
    • 2018-01-17
    • 2022-10-07
    • 1970-01-01
    • 2011-12-31
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多