【问题标题】:WPF ComboBox not updating sourceWPF ComboBox 不更新源
【发布时间】:2010-10-10 05:02:28
【问题描述】:

我正在尝试使 WPF 组合框正常工作(在 WPFToolkit 数据网格内),但我无法正确对齐所有部分。我正在使用 Linq to Entities,并将整个数据上下文设置为 Linq 查询的结果:


private void LoadDonationGrid()
{
    donationGrid.ItemsSource = from donations in entities.Donation
                   .Include("Family")
                   .Include("PledgeYear")
                   .Include("DonationPurpose")
               from donationPurposes in entities.DonationPurpose
               select new { donations, donationPurposes };
}

我的代码隐藏中还有一个页面属性,它为组合框提供 ItemsSource:


private IEnumerable donationPurposeList;
public IEnumerable DonationPurposeList
{
    get
    {
        if (donationPurposeList == null)
        {
            donationPurposeList = from dp in entities.DonationPurpose
                                  select dp;
        }
        return donationPurposeList.ToList();
    }
}

组合框的 XAML 如下所示:

<tk:DataGridTemplateColumn Header="Purpose">
    <tk:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=donations.DonationPurpose.Description, Mode=TwoWay}" />
        </DataTemplate>
    </tk:DataGridTemplateColumn.CellTemplate>
    <tk:DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox Name="cboDonationPurpose"
                SelectedValue="{Binding Path=donations.DonationPurposeID, Mode=TwoWay}"
                ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Page},Mode=FindAncestor},Path=DonationPurposeList}"                                 
                DisplayMemberPath="Description"
                SelectedValuePath="DonationPurposeID"
                      />
        </DataTemplate>
    </tk:DataGridTemplateColumn.CellEditingTemplate>
</tk:DataGridTemplateColumn>

一切似乎都正常工作,即适当的值显示在 ComboBox 中,直到焦点离开 ComboBox 的位置。此时,显示的值将恢复为原始值,而不是新选择的值。我尝试使用 SelectedItem 而不是 SelectedValue,但最终选择的值未显示在 ComboBox 中。我有点迷惑:这部分似乎应该起作用了。

2009 年 3 月 2 日编辑:我仍然对此感到困惑。我应该注意到,在我的数据网格中,任何简单的数据列(例如,“DataGridTextColumn”)都会按照您的预期更新基础数据源。但是对我的任何模板列(“DataGridTemplateColumn”)或 ComboBox 列(“DataGridComboBoxColumn”)的任何更新都不起作用:基础数据源永远不会更新。当然其他人已经尝试使用 WPF.Toolkit 数据网格并让这个场景工作 - 但我已经查看了那里的所有示例代码,我基本上按照它说的去做(在我的约束范围内)解决方案),所以我在摸索为什么这不起作用。

有什么想法吗?

【问题讨论】:

    标签: wpf data-binding combobox wpftoolkit


    【解决方案1】:

    我遇到了类似的问题(为此我花了好几天的时间),我发现将 SelectedValue 绑定上的 UpdateSourceTrigger 更改为 PropertyChanged 解决了这个问题。我不知道为什么,但对我来说,数据源在我进行此更改之前没有更新。

    <ComboBox 
        ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UpdateTypesManager:MainWindow}}, Path=CardinalityTypes}" 
        DisplayMemberPath="CardinalityType"
        SelectedValue="{Binding CardinalityTypeId, UpdateSourceTrigger=PropertyChanged}"
        SelectedValuePath="Id" />
    

    【讨论】:

    • 我遇到了完全相同的问题。已经搞砸了几个小时。我将绑定更改为 TwoWay,尝试更改一大堆其他内容。MSDN 似乎表明在 Text 等情况下默认设置为 LostFocus。
    • 实际上,当我使用文本框时,它并没有默认为我的 LostFocus。将其设置为 PropertyChanged 确实可以解决此问题。
    • 我的 .NET 4.1 应用程序有许多组合框,不需要明确告诉它们更新源。只有在 DataGrid 中,它们似乎需要特别注意。
    • 嗨@RogerWilliamson,我的在ListBoxItemTemplate 内,所以可能与中继器类型的控件有关。
    【解决方案2】:

    我能够得到这个工作。但我的设置有点不同。

    1. 我创建了一个 ViewModel 作为与 View 的合同。
    2. 我绑定到 ComboBox.SelectedItem 属性而不是 ComboBox.SelectedValue 属性

    由于我不知道您的数据源是什么,所以我自己编写了一个来模拟基本问题:在 WPF DataGrid 中正确绑定组合框。

    这是我的视图模型的组成:

    public class RootViewModel
    {
        public List<State> USStates { get; set; }
        public List<Customer> Customers { get; set; }
    
        public ViewModel()
        {
            Customers = new List<Customer>();
            Customers.Add(new Customer() { FirstName = "John", LastName = "Smith", State = new State() { ShortName = "IL" } });
            Customers.Add(new Customer() { FirstName = "John", LastName = "Doe", State = new State() { ShortName = "OH" } });
            Customers.Add(new Customer() { FirstName = "Sally", LastName = "Smith", State = new State() { ShortName = "IN" } });
    
            USStates = new List<State>();
            USStates.Add(new State() { ShortName = "OH" });
            USStates.Add(new State() { ShortName = "IL" });
            USStates.Add(new State() { ShortName = "IN" });
        }
    }
    
    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public State State { get; set; }
    }
    
    public class State
    {
        public string ShortName { get; set; }
    
        public State() 
        {
            ShortName = string.Empty;
        }
    
        public override bool Equals(object obj)
        {
            if (obj is State)
            {
                State otherState = obj as State;
                return ShortName.Equals(otherState.ShortName);
            }
            else
            {
                return false;
            }
        }
    }
    

    在开始之前,我将 Window 的 DataContext 设置为我正确构造的 RootViewModel 的一个实例。

    <tk:DataGrid ItemsSource="{Binding Customers}" AutoGenerateColumns="False">
                <tk:DataGrid.Columns>
                    <tk:DataGridTemplateColumn Header="State">
                        <tk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=State.ShortName, Mode=TwoWay}" />
                            </DataTemplate>
                        </tk:DataGridTemplateColumn.CellTemplate>
                        <tk:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <ComboBox 
                                    x:Name="cboDonationPurpose" 
                                    SelectedItem="{Binding Path=State, Mode=TwoWay}" 
                                    ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window},Mode=FindAncestor}, Path=DataContext.USStates}" 
                                    DisplayMemberPath="ShortName" 
                                    SelectedValuePath="ShortName" />
                            </DataTemplate>
                        </tk:DataGridTemplateColumn.CellEditingTemplate>
                    </tk:DataGridTemplateColumn>
                </tk:DataGrid.Columns>
            </tk:DataGrid>
    

    为了使 SelectedItem 正确绑定,我需要确保我已经覆盖了我的实体上的 Equals 方法,因为在后台,WPF 正在使用此方法来确定谁是 SelectedItem。我认为这从根本上是您从一开始就有的问题,导致您尝试绑定到 SelectedValue 而不是 SelectedItem。

    【讨论】:

      【解决方案3】:

      你需要添加

      IsSynchronizedWithCurrentItem = "True"
      

      在您的 Xaml 中。

      就这么简单……

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-04
        • 1970-01-01
        • 2019-09-11
        • 1970-01-01
        • 2015-03-20
        • 2012-05-12
        相关资源
        最近更新 更多