Home > Enterprise >  Is it possible to set a different polygon in a wpf data template trigger
Is it possible to set a different polygon in a wpf data template trigger

Time:11-21

I want to display a different polygon based on a datatemplate trigger.

(PS: don't look at the points collection for Circle, I know that doesn't look like a circle at all ;-))

<ContentControl.Resources>
    <Polygon 
        x:Key="Circle"  
        Grid.Column="0" 
        Stretch="Fill" Stroke="Black" StrokeThickness="1" Fill="DarkGray"
        Width="25" Height="25" Points=" 0,50 25,100 75,100" />        
    <Polygon 
        x:Key="Hexagon" 
        Grid.Column="0" 
        Stretch="Fill" Stroke="Black" StrokeThickness="1" Fill="DarkGray"
        Width="25" Height="25" Points=" 0,50 25,100 75,100 100,50 75,0 25,0 0,50" />

    <Polygon Name="path" Grid.Column="0" Stretch="Fill" Stroke="Black" StrokeThickness="1" Fill="DarkGray"
        Width="25" Height="25" />

Then I want to change the polygon on a data template trigger.

        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding SimulationType}" Value="{x:Static vm:SimulationType.ServerSimulated}">
                <DataTrigger.Setters>
                    <Setter TargetName="path" Property="Points" Value="{StaticResource Hexagon}"/>
                </DataTrigger.Setters>
            </DataTrigger>
            <DataTrigger Binding="{Binding SimulationType}" Value="{x:Static vm:SimulationType.ServerInSimulation}">
                <DataTrigger.Setters>
                    <Setter TargetName="path" Property="Points" Value="{StaticResource Circle}"/>
                </DataTrigger.Setters>
            </DataTrigger>
            
        </DataTemplate.Triggers>

This will throw a runtime exception because I am trying to put an entire Polygon in a PointsCollection which is not possible. But how on earth do I achieve what I am looking for?

I can't seem to figure out if, and how, to define a PointsCollection in xaml. Neither is it possible to create a data template trigger on an entire Polygon.

How do I replace the points of a polygon in a data template trigger?

CodePudding user response:

Assuming the target is a Polygon, you can define a PointCollections as a collection of Points and set it to Points Property.

<Window.Resources>
    <PointCollection x:Key="HexagonPoints">
        <Point X="0" Y="50"/>
        <Point X="25" Y="100"/>
        <Point X="75" Y="100"/>
        <Point X="100" Y="50"/>
        <Point X="75" Y="0"/>
        <Point X="25" Y="0"/>
        <Point X="0" Y="50"/>
    </PointCollection>
</Window.Resources>
  •  Tags:  
  • wpf
  • Related