顽皮的按钮样式行为不端

时间:2011-03-10 19:56:52

标签: wpf button styles isenabled

这两个按钮;启用一个,并禁用具有样式的一个。任何人都可以找出原因吗?

<Button Command="{Binding ResolveHostsCommand}" VerticalAlignment="Center" IsEnabled="{Binding CanUserUpdateHosts}" Grid.Column="0" Content="Resolve" />

<Button VerticalAlignment="Center" IsEnabled="False" Content="Cancel" Grid.Column="1">
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <DataTrigger Binding="{Binding CanUserUpdateHosts}" Value="True">
                    <Setter Property="IsEnabled" Value="True"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

2 个答案:

答案 0 :(得分:3)

即使将CanUserUpdateHosts设置为true,Style.Setter也不能覆盖您在Button声明中提供的显式值。请参阅Dependency Property Value Precedence页面。触发器中的样式设置器位于#6,而显式值为#3。

您需要反转逻辑,如下所示:

<Button Command="{Binding ResolveHostsCommand}" VerticalAlignment="Center" IsEnabled="{Binding CanUserUpdateHosts}" Grid.Column="0" Content="Resolve" />
<Button  VerticalAlignment="Center" Content="Cancel" Grid.Column="1">
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <DataTrigger Binding="{Binding CanUserUpdateHosts}" Value="False">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

或者你可以使用你的Style的Setter将IsEnabled设置为false,如下所示:

<Button Command="{Binding ResolveHostsCommand}" VerticalAlignment="Center" IsEnabled="{Binding CanUserUpdateHosts}" Grid.Column="0" Content="Resolve" />
<Button  VerticalAlignment="Center" Content="Cancel" Grid.Column="1">
    <Button.Style>
        <Style TargetType="Button">
            <Setter Property="IsEnabled" Value="False"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding CanUserUpdateHosts}" Value="True">
                    <Setter Property="IsEnabled" Value="True"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

答案 1 :(得分:2)

您将IsEnabled标记为false。

<Button VerticalAlignment="Center" IsEnabled="False"...>