【问题标题】:WPF Binding and DataTemplateWPF 绑定和数据模板
【发布时间】:2012-05-30 19:07:22
【问题描述】:

我在我的应用程序中遇到了问题,我无法解决。我创建了以下简单的 WPF 应用程序来说明这种情况。

MainWindow.xaml:

<Window x:Class="GlobalDataTemplate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:GlobalDataTemplate"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate DataType="{x:Type my:MyData}">
            <StackPanel Background="{Binding BgColor}">
                <TextBlock Text="{Binding Text}"/>
                <TextBlock Text="{Binding Number}"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <ItemsControl>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Columns="3" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <my:MyData x:Name="NW" Text="NW" Number="1" BgColor="#FFFF0000" />
        <my:MyData x:Name="N" Text="N"  Number="2" BgColor="#FF63FF00" />
        <my:MyData x:Name="NE" Text="NE" Number="3" BgColor="#FFFFCA00" />
        <my:MyData x:Name="W" Text="W" Number="4" BgColor="#FF0037FF" />
        <my:MyData x:Name="C" Text="C" Number="5" BgColor="#FF9E00FF" />
        <my:MyData x:Name="E" Text="E" Number="6" BgColor="#FF838383" />
        <my:MyData x:Name="SW" Text="SW" Number="7"
                   BgColor="{Binding ElementName=NW, Path=BgColor}" />
        <my:MyData x:Name="S" Text="S" Number="8"
                   BgColor="{Binding ElementName=N, Path=BgColor}" />
        <my:MyData x:Name="SE" Text="SE" Number="9"
                   BgColor="{Binding ElementName=NE, Path=BgColor}" />
    </ItemsControl>
</Window>

MyData.cs:

using System.Windows;
using System.Windows.Media;

namespace GlobalDataTemplate
{
    class MyData : DependencyObject
    {
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(MyData), new UIPropertyMetadata(null));

        public int Number
        {
            get { return (int)GetValue(NumberProperty); }
            set { SetValue(NumberProperty, value); }
        }
        public static readonly DependencyProperty NumberProperty =
            DependencyProperty.Register("Number", typeof(int), typeof(MyData), new UIPropertyMetadata(0));

        public Brush BgColor
        {
            get { return (Brush)GetValue(BgColorProperty); }
            set { SetValue(BgColorProperty, value); }
        }
        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BgColorProperty =
            DependencyProperty.Register("BgColor", typeof(Brush), typeof(MyData), new UIPropertyMetadata(null));
    }
}

在 XAML 中,您会看到一个 3x3 网格,其底部颜色与顶部显示的颜色相同。但底行的颜色根本不显示(您会看到窗口的白色背景)。如何让底部的颜色正确绑定到顶部的颜色?

我还尝试添加属性更改处理程序并设置断点。断点永远不会被击中。

提前致谢。

【问题讨论】:

  • 我假设上来没有颜色?
  • 您可能还需要在 UniformGrid 上设置 Rows 属性:
  • @CodingGorilla,本质上,是的,它们是无色的。正如我所提到的,您会看到窗口的白色背景。
  • @Ross,问题在于绑定。这些项目显示在适当的位置。
  • 您是否尝试过在 BgColor UIPropertyMetadata 中放置一个属性更改处理程序并放置一个断点以查看它是否被调用并检查新值?

标签: wpf binding datatemplate


【解决方案1】:

当我运行您的代码时,我在调试输出中收到以下错误消息:

System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。绑定表达式:路径=BgColor;数据项=空;目标元素是“MyData”(HashCode=65325907);目标属性是“BgColor”(类型“画笔”)

即这意味着 WPF 不会将 MyData 项视为逻辑树的一部分。 因此,从Freezable 派生MyData,例如

class MyData : Freezable 
{
    protected override Freezable CreateInstanceCore()
    {
        throw new System.NotImplementedException();
    }

    ... put the dependency properties here ...

}

“找不到管理 FrameworkElement...”问题与“继承上下文”有关;请在此处找到详细信息:http://blogs.msdn.com/b/nickkramer/archive/2006/08/18/705116.aspx

【讨论】:

  • 谢谢你。我今天学到了一些东西。像魅力一样工作!
猜你喜欢
  • 1970-01-01
  • 2013-05-23
  • 2021-12-25
  • 2014-08-23
  • 1970-01-01
  • 2012-01-22
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
相关资源
最近更新 更多