顽皮的按钮样式行为不端
这两个按钮;一种是启用的,一种是禁用的。谁能弄清楚为什么吗?
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使将 CanUserUpdateHosts 设置为 true,Style.Setter 也无法覆盖您在 Button 声明中提供的显式值。请参阅依赖属性值优先级页面。触发器中的样式设置器位于#6,而显式值是#3。
您需要反转您的逻辑,如下所示:
或者您可以使用您的 Style 的 Setter 将 IsEnabled 设置为 false,如下所示:
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:
Or you could set the IsEnabled to false using a Setter of your Style, like so:
您已将
IsEnabled
标记为 false。You have
IsEnabled
marked as false.