WPF触发和绑定问题

发布于 2024-10-03 11:05:08 字数 1106 浏览 6 评论 0原文

我正在创建一个带有简单折线的按钮,我想在禁用按钮时更改颜色(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 技术交流群。

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

发布评论

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

评论(1

深海少女心 2024-10-10 11:05:08

我自己找到了答案。
我将 Foreground="Green" 移至一个单独的 setter,< ;Style>,然后就成功了。
我不知道为什么。

这是完整的解决方案:

<RepeatButton x:Name="btnUp">
  <RepeatButton.Style>
    <Style TargetType="{x:Type RepeatButton}">
      <Setter Property="Foreground" Value="Green"/>
      <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>

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:

<RepeatButton x:Name="btnUp">
  <RepeatButton.Style>
    <Style TargetType="{x:Type RepeatButton}">
      <Setter Property="Foreground" Value="Green"/>
      <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文