WPF触发和绑定问题
我正在创建一个带有简单折线的按钮,我想在禁用按钮时更改颜色(btnUp.IsEnabled = false
)。
我尝试了这个:
<RepeatButton x:Name="btnUp" Foreground="Green">
<RepeatButton.Style>
<Style TargetType="{x:Type RepeatButton}">
<Setter Property="Content">
<Setter.Value>
<Polyline x:Name="arrowUp" Points="0,2 3,0 6,2" Stroke="{Binding RelativeSource={RelativeSource AncestorType=RepeatButton}, Path=Foreground, Mode=TwoWay}" StrokeThickness="2"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="#FFFFFFFF"/>
</Trigger>
</Style.Triggers>
</Style>
</RepeatButton.Style>
</RepeatButton>
但是当按钮被禁用时,折线仍然具有相同的(绿色)颜色。尽管我确实希望它是白色的,因为按钮.前景和折线.笔画之间的数据绑定。
但是,如果我将触发器更改为此它就可以工作(按钮已折叠):
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
I am creating a button with a simple Polyline which I want to change color when the button is Disabled (btnUp.IsEnabled = false
).
I tried this:
<RepeatButton x:Name="btnUp" Foreground="Green">
<RepeatButton.Style>
<Style TargetType="{x:Type RepeatButton}">
<Setter Property="Content">
<Setter.Value>
<Polyline x:Name="arrowUp" Points="0,2 3,0 6,2" Stroke="{Binding RelativeSource={RelativeSource AncestorType=RepeatButton}, Path=Foreground, Mode=TwoWay}" StrokeThickness="2"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="#FFFFFFFF"/>
</Trigger>
</Style.Triggers>
</Style>
</RepeatButton.Style>
</RepeatButton>
But the Polyline still has the same (Green) color when the button is disabled. Even though I did expect it to be white because of the databinding between the button.foreground and polyline.stroke.
However if I change the trigger to this it works (the button is collapsed):
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己找到了答案。
我将
Foreground="Green"
移至一个单独的 setter,
内< ;Style>
,然后就成功了。我不知道为什么。
这是完整的解决方案:
I found the answer on my own.
I moved the
Foreground="Green"
to a separate setter,<Setter Property="Foreground" Value="#FF383838"/>
inside the<Style>
, then it worked.I'm not sure why though.
Here's the complete solution: