XAML触发器不触发

时间:2016-09-26 14:38:51

标签: c# xaml triggers

你认为这个触发器有什么问题吗?

<Border Margin="2" BorderBrush="#6593CF" BorderThickness="1.5" >
                        <Border.Style>
                            <Style>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsCreating, UpdateSourceTrigger=PropertyChanged}" Value="true">
                                        <Setter Property="Border.BorderBrush" Value="Lime"/>
                                        <Setter Property="Border.BorderThickness" Value="3" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Border.Style>

                    </Border>

它属于这个属性:

private bool _IsCreating = false;
public bool IsCreating
        {
            get
            {
                return _IsCreating;
            }
            set
            {
                if (_IsCreating == value)
                {
                    return;
                }
                _IsCreating = value;            
                RaisePropertyChanged("IsCreating");
            }
        }

当我设置IsCreating = true时,样式不会更新。

1 个答案:

答案 0 :(得分:2)

您已在边框上设置了BorderBrush和BorderThinkness内联。内联声明有quite hight precedence并覆盖您的触发器。从边框中删除内联声明并将其移至触发器:

<Border Margin="2">
    <Border.Style>
        <Style TargetType="{x:Type Border}">
            <Style.Triggers>
                <DataTrigger
                    Binding="{Binding Path=DataContext.IsCreating, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                    Value="true">
                    <Setter Property="BorderBrush" Value="Lime" />
                    <Setter Property="BorderThickness" Value="3" />
                </DataTrigger>
                <DataTrigger
                    Binding="{Binding Path=DataContext.IsCreating, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                    Value="false">
                    <Setter Property="BorderBrush" Value="#6593CF" />
                    <Setter Property="BorderThickness" Value="1.5" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
    <CheckBox IsChecked="{Binding IsCreating}" />
</Border>