开发者

naughty button styles misbehaving

These two buttons; one is enabled and the one with the style is disabled. Can anyone figure out why?

<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>


Even if you set CanUserUpdateHosts to true, the Style.Setter cannot override the explicit value you provided in the Button declaration. See the Dependency Property Value Precedence page. Style setters in triggers are at #6, while the explicit value is #3.

You would need to reverse your logic, like so:

<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>

Or you could set the IsEnabled to false using a Setter of your Style, like so:

<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>


You have IsEnabled marked as false.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜