WPF DataTrigger
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}" V开发者_StackOverflowalue="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
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}
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
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.
精彩评论