绑定值更改时,WPF DataTrigger未设置

时间:2015-01-29 18:18:15

标签: c# wpf

我有一个数据模板,它定义了一个显示由画布ItemsPanel内部绘制的每个NodePairViewModel的行。当表示的NodePairViewModel的IsChecked属性按样式中的定义更改时(见下文),线颜色应该更改,但是当设置此属性时,颜色不会更改。 INotifyPropertyChanged已实现,我已验证此事件正在触发。我看不出任何明显错误的东西,同样的设置适用于NodeViewModels,它们也在同一个itemspanel中绘制。

    <DataTemplate DataType="{x:Type vm:NodePairViewModel}">
        <Line Canvas.ZIndex="2" Name="Arc" Stroke="Blue" StrokeThickness="3" 
              X1="{Binding Path=NodeA.PositionX, Mode=OneWay, Converter={StaticResource NodeLinkXConverter}}" 
              X2="{Binding Path=NodeB.PositionX, Mode=OneWay, Converter={StaticResource NodeLinkXConverter}}" 
              Y1="{Binding Path=NodeA.PositionY, Mode=OneWay, Converter={StaticResource NodeLinkYConverter}}" 
              Y2="{Binding Path=NodeB.PositionY, Mode=OneWay, Converter={StaticResource NodeLinkYConverter}}" 
              MouseLeftButtonDown="Arc_MouseLeftButtonDown" MouseLeftButtonUp="Arc_MouseLeftButtonUp">
            <Line.Style>
                <Style TargetType="Line">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsChecked}" Value="True">
                            <Setter Property="Stroke" Value="Orange"></Setter>
                            </DataTrigger>

<DataTrigger Binding="{Binding Path=IsSelected, Mode=OneWay}" Value="True">
                                <Setter Property="Stroke" Value="Red"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path=IsRejected, Mode=OneWay}" Value="True">
                                <Setter Property="Stroke" Value="LightGray"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path=IsVisited}" Value="True">
                                <Setter Property="Stroke" Value="LightGreen"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Line.Style>
            </Line>
        </DataTemplate>


<ItemsControl Name="NodeSpace" ItemsSource="{Binding GraphItems}" Margin="5" MouseMove="NodeSpace_MouseMove" MouseDown="NodeSpace_MouseDown" MouseLeftButtonUp="NodeSpace_MouseLeftButtonUp" MouseLeftButtonDown="NodeSpace_MouseLeftButtonDown" MouseRightButtonDown="NodeSpace_MouseRightButtonDown" RenderTransformOrigin="0.5,0.5" Grid.Column="1" Grid.Row="1">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <Canvas Name="GraphSurface" Background="#FFFFFE"></Canvas>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemContainerStyle>
                        <Style TargetType="ContentPresenter">
                            <Setter Property="Canvas.Left">
                                <Setter.Value>
                                    <Binding Mode="OneWay" Path="PositionX" Converter="{StaticResource XInverseConverter}"/>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Canvas.Top">
                                <Setter.Value>
                                    <Binding Mode="OneWay" Path="PositionY" Converter="{StaticResource YInverseConverter}"/>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ItemsControl.ItemContainerStyle>
                </ItemsControl>

IsChecked属性本身

private bool ArcIsChecked = false;
public bool IsChecked 
{
    get
    {
        return ArcIsChecked;
    }
    set
    {
        ArcIsChecked = value;
        RaisePropertyChanged("IsChecked");
    }
}

1 个答案:

答案 0 :(得分:3)

您需要从Line

移动默认值
<Line ... Stroke="Blue">

Style作为Setter

<Line 
   Canvas.ZIndex="2" 
   Name="Arc" 
   StrokeThickness="3" 
   X1="{Binding Path=NodeA.PositionX, Mode=OneWay, Converter={StaticResource NodeLinkXConverter}}" 
   X2="{Binding Path=NodeB.PositionX, Mode=OneWay, Converter={StaticResource NodeLinkXConverter}}" 
   Y1="{Binding Path=NodeA.PositionY, Mode=OneWay, Converter={StaticResource NodeLinkYConverter}}" 
   Y2="{Binding Path=NodeB.PositionY, Mode=OneWay, Converter={StaticResource NodeLinkYConverter}}" 
   MouseLeftButtonDown="Arc_MouseLeftButtonDown" 
   MouseLeftButtonUp="Arc_MouseLeftButtonUp">
   <Line.Style>
      <Style TargetType="{x:Type Line}">
         <Setter Property="Stroke" Value="Blue"/>
         <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsChecked}" Value="True">
               <Setter Property="Stroke" Value="Orange"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=IsSelected, Mode=OneWay}" Value="True">
               <Setter Property="Stroke" Value="Red"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=IsRejected, Mode=OneWay}" Value="True">
               <Setter Property="Stroke" Value="LightGray"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=IsVisited}" Value="True">
               <Setter Property="Stroke" Value="LightGreen"/>
            </DataTrigger>
         </Style.Triggers>
      </Style>
   </Line.Style>
</Line>

根据Dependency Property Setting Precedence List样式触发器的层次结构低于本地值,因此它无法覆盖您的默认(本地)值