顽皮的按钮样式行为不端

发布于 2024-10-21 14:45:56 字数 714 浏览 7 评论 0原文

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

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

影子是时光的心 2024-10-28 14:45:56

即使将 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>

或者您可以使用您的 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>

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>
终止放荡 2024-10-28 14:45:56

您已将 IsEnabled 标记为 false。

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

You have IsEnabled marked as false.

<Button VerticalAlignment="Center" IsEnabled="False"...>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文