【问题标题】:Problems with DataGridTemplateColumn with ComboBoxDataGridTemplateColumn 与 ComboBox 的问题
【发布时间】:2009-03-30 21:38:33
【问题描述】:

我有一个带有 ComboBox 的 DataGrid 模板列。当我选择一个值并按 Enter 时,绑定的数据不会更新(我看到的是空单元格)。

XAML:

<Window x:Class="WpfGrid2.Window2"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:sys="clr-namespace:System;assembly=mscorlib"
  xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
  >

    <Window.Resources>
        <x:Array x:Key="people" Type="sys:Object" />

        <x:Array x:Key="knownLastNames" Type="sys:String">
            <sys:String>Smith</sys:String>
            <sys:String>Johnson</sys:String>
            <sys:String>Williams</sys:String>
        </x:Array>
    </Window.Resources>

    <StackPanel>
        <dg:DataGrid x:Name="_grid" ItemsSource="{DynamicResource people}" CanUserAddRows="True" AutoGenerateColumns="False">
            <dg:DataGrid.Columns>

                <dg:DataGridTemplateColumn Header="LastName" MinWidth="100">
                    <dg:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{DynamicResource knownLastNames}" SelectedItem="{Binding LastName}"></ComboBox>
                        </DataTemplate>
                    </dg:DataGridTemplateColumn.CellEditingTemplate>
                    <dg:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding LastName}" />
                        </DataTemplate>
                    </dg:DataGridTemplateColumn.CellTemplate>
                </dg:DataGridTemplateColumn>

            </dg:DataGrid.Columns>
        </dg:DataGrid>

        <Button>test</Button>
    </StackPanel>
</Window>

代码隐藏:

namespace WpfGrid2
{
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();

            List<Person> people = new List<Person>();
            this.Resources["people"] = people;
        }
    }
}

如果我将 ComboBox 更改为 TextBox,它可以正常工作

<TextBox Text="{Binding LastName}" />

怎么了?

【问题讨论】:

    标签: wpf wpftoolkit


    【解决方案1】:

    我不知道这是否是解决您的问题的可行解决方案,但是如果您将 Combo-Box 的 ItemsSource 绑定更改为 StaticResource,则该绑定有效。

    ...
    
    <ComboBox ItemsSource="{StaticResource knownLastNames}" ... />
    
    ...
    

    我很确定发生的事情是当 ComboBox 被卸载时(当 EditTemplate 由于提交新记录而被卸载时),DynamicResource 尝试再次查找资源,但失败了(因为 ComboBox 不再在可视化树中,它不会在可视化树中找到它上面定义的资源)。这会将 ItemsSource 设置为 null,并将 SelectedItem 设置为 null,从而将 LastName 设置为 null。

    使用 StaticResource,集合仅在 ComboBox 显示之前搜索一次,因此不会重置为 null。

    【讨论】:

    • 您的解决方案有效,解释也很有意义。似乎我需要另一种方法来使 ItemsSource 可更新...
    【解决方案2】:

    另一种选择(遵循“Abe Heidebrecht”答案的逻辑)是将“knownLastNames”移动到 Application.Resources。见第 3 点。

    Dynamic resource lookup behavior

    1. 查找进程检查 资源中的请求键 由元素定义的字典 设置属性。

    2. 然后查找过程遍历 逻辑树向上,到 父元素及其资源 字典。这种情况一直持续到 到达根元素。

    3. 接下来,应用程序资源 检查。应用资源是 资源中的那些资源 定义的字典 WPF 的应用程序对象 应用。

    4. 主题资源字典是 选中,对于当前活动 主题。如果主题在 运行时,该值被重新评估。

    5. 检查系统资源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-14
      • 2011-10-28
      • 2013-06-30
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-13
      • 2016-01-21
      相关资源
      最近更新 更多