2

这两个按钮;一个是启用的,一个是禁用的。谁能弄清楚为什么?

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

2 回答 2

3

即使您将 CanUserUpdateHosts 设置为 true,Style.Setter 也无法覆盖您在 Button 声明中提供的显式值。请参阅依赖属性值优先级页面。触发器中的样式设置器在#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>

或者您可以使用您的样式的 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>
于 2011-03-10T20:07:25.273 回答
2

您已IsEnabled标记为假。

<Button VerticalAlignment="Center" IsEnabled="False"...>
于 2011-03-10T20:04:09.127 回答