WPF 数据触发器

发布于 2024-09-30 12:58:30 字数 2196 浏览 5 评论 0原文

我正在开发一款棋盘游戏来掌握 WPF,但在尝试了一整夜让这部分工作后我陷入了困境。

以下代码属于“House”用户控件,在设计器上运行良好。我可以选择“房屋”对象并更改属性网格上的 HouseType 属性,然后查看所需的效果。但在运行时,如果我更改相同的属性,则什么也不会发生!

<DrawingBrush x:Key="emptyHouseBrush">
  <DrawingBrush.Drawing>
    <GeometryDrawing Geometry="M0,0 100,100 M0,100 100,0">
      <GeometryDrawing.Pen>
        <Pen Brush="Black" />
      </GeometryDrawing.Pen>
    </GeometryDrawing>
  </DrawingBrush.Drawing>
</DrawingBrush>

<DataTemplate DataType="{x:Type m:House}">
  <Border x:Name="border" BorderThickness="2"  >
    <Grid x:Name="grid" >

    </Grid>
  </Border>      
  <DataTemplate.Triggers>
    <DataTrigger Binding="{Binding Path=HouseType}" Value="Neutral">
      <Setter TargetName="border" Property="BorderBrush" Value="Black"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding Path=HouseType}" Value="Forbidden">
      <Setter TargetName="border" Property="BorderBrush" Value="Black"/>
      <Setter TargetName="grid" Property="Background" Value="{StaticResource ResourceKey=emptyHouseBrush}"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding Path=HouseType}" Value="Borders">
      <Setter TargetName="border" Property="BorderBrush" Value="#FF7A6D34"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding Path=HouseType}" Value="Homeland">
      <Setter TargetName="border" Property="BorderBrush" Value="#FFFFD200"/>
      <Setter TargetName="border" Property="Background" Value="#54FBE681"/>
    </DataTrigger>

    <Trigger SourceName="border" Property="IsMouseOver" Value="True">
      <Setter TargetName="border" Property="Background" Value="#645CAEF9"/>
    </Trigger>

  </DataTemplate.Triggers>
</DataTemplate>


</Control.Resources>
  <ContentControl>
    <m:House x:Name="house" HouseType="Neutral"></m:House>
  </ContentControl>

</UserControl>

我尝试过使用默认属性、依赖属性、实现 IPropertyChanged 接口、尝试将属性移动到控件并重复上述所有操作...但仍然一无所获。

我缺少什么?更改此属性后,如何让控件更新边框(和背景)?

提前致谢。

马塞洛

I'm working on a board game to get a grip on WPF and I'm stuck after trying the whole night to get this part working.

The following code belongs to the 'House' user control and works just fine on the designer. I can select the 'house' object and change the HouseType property on the property grid and see the desired effect. But in runtime, if I change the same property, nothing happens!

<DrawingBrush x:Key="emptyHouseBrush">
  <DrawingBrush.Drawing>
    <GeometryDrawing Geometry="M0,0 100,100 M0,100 100,0">
      <GeometryDrawing.Pen>
        <Pen Brush="Black" />
      </GeometryDrawing.Pen>
    </GeometryDrawing>
  </DrawingBrush.Drawing>
</DrawingBrush>

<DataTemplate DataType="{x:Type m:House}">
  <Border x:Name="border" BorderThickness="2"  >
    <Grid x:Name="grid" >

    </Grid>
  </Border>      
  <DataTemplate.Triggers>
    <DataTrigger Binding="{Binding Path=HouseType}" Value="Neutral">
      <Setter TargetName="border" Property="BorderBrush" Value="Black"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding Path=HouseType}" Value="Forbidden">
      <Setter TargetName="border" Property="BorderBrush" Value="Black"/>
      <Setter TargetName="grid" Property="Background" Value="{StaticResource ResourceKey=emptyHouseBrush}"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding Path=HouseType}" Value="Borders">
      <Setter TargetName="border" Property="BorderBrush" Value="#FF7A6D34"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding Path=HouseType}" Value="Homeland">
      <Setter TargetName="border" Property="BorderBrush" Value="#FFFFD200"/>
      <Setter TargetName="border" Property="Background" Value="#54FBE681"/>
    </DataTrigger>

    <Trigger SourceName="border" Property="IsMouseOver" Value="True">
      <Setter TargetName="border" Property="Background" Value="#645CAEF9"/>
    </Trigger>

  </DataTemplate.Triggers>
</DataTemplate>


</Control.Resources>
  <ContentControl>
    <m:House x:Name="house" HouseType="Neutral"></m:House>
  </ContentControl>

</UserControl>

I have tried using default properties, dependency properties, implementing IPropertyChanged interface, tried moving the property to the control and repeating all of the above... and still nothing.

What am I missing? How can I have the control update the border (and background) once I change this property?

Thanks in advance.

Marcelo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月朦胧 2024-10-07 12:58:30

代码对我来说似乎不错。
查找这些错误的一个好方法是一个我觉得非常棒的小程序:Snoop。

http://snoopwpf.codeplex.com/Thread/View.aspx?ThreadId=207711

只要看一下它,也许你就能够追踪到你的错误。

{享受}

Code seems good to me.
A good way to look out for those errors is a little program I find really great : Snoop.

http://snoopwpf.codeplex.com/Thread/View.aspx?ThreadId=207711

Just take a look at it, and maybe you'll be able to track back your error.

{enjoy}

计㈡愣 2024-10-07 12:58:30

看起来您正在将 ControlTemplates 与 DataTemplates 混合在一起。
DataTemplate 用于指定如何显示某种数据对象类型,它通常包含一些绑定到该对象的值属性的控件。您想要的是更改控件的默认可视化效果,最好使用 ControlTemplate 来完成此操作。如果您想保持控件的可视化并仅添加边框颜色更改,那么您可以使用 Blend 等工具来获取 House 控件的当前 ControlTemplate 并自定义它添加触发器

Its seems like you are mixing up ControlTemplates with DataTemplates.
A DataTemplate is used to specify how a certain data object type is to be displayed, it usually will contain a few controls that are bound to value properties of the object. What you want is to change the default visualization of a control, this is best done using a ControlTemplate. If you want to keep the visualization of the control and just add the border color change then you can use a tool such as Blend to get the current ControlTemplate of the House control and customize it adding your triggers

霊感 2024-10-07 12:58:30

我觉得不错。确保 House 对象正在实现 INotifyPropertyChanged 并且您在更新属性时调用 PropertyChanged。如果仍然无法正常工作,请查看“输出调试”窗口。绑定错误通常会出现在那里。

It looks good to me. Make sure that the House object is implementing INotifyPropertyChanged and that you are calling PropertyChanged when updating the property. If it still isn't working look at your Output debug window. Binding errors typically show up there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文