WPF 触发器影响代码中设置的值,而不是 XAML

发布于 2024-07-30 13:55:21 字数 1152 浏览 2 评论 0原文

在我的应用程序中,我有具有三种可能状态的切换按钮; “未选中”、“已选中”和“以前使用过”。 当用户来到这个特定屏幕时,一些切换按钮将处于“先前使用”状态,以显示已完成的工作。 单击切换按钮(无论当前状态如何)都会将其置于“已选中”状态。 一次只能选中这些切换按钮之一。 不同的状态由不同颜色的外发光或根本不发光来指示。

为了设置“Checked”状态的外发光,我在 IsChecked euqals true 上使用触发器。

<ControlTemplate.Triggers>
   <Trigger Property="IsChecked" Value="True">                          
      <Setter Property="Effect">
         <Setter.Value>
            <DropShadowEffect Color="Salmon" BlurRadius="40" ShadowDepth="0" Opacity="1.0"></DropShadowEffect>
         </Setter.Value>
      </Setter>
   </Trigger>                        
</ControlTemplate.Triggers>

对于“以前使用的”状态,我在代码中应用外部发光,而不是标记。 我必须这样做,因为确定按钮是否应处于此状态是通过检查列表中的值来完成的。

if (mExistingViews.Contains(mViews[i].LocalizedName))
{
   DropShadowEffect dse = new DropShadowEffect();
   dse.ShadowDepth = 0;
   dse.BlurRadius = 20;
   dse.Opacity = 1.0;
   dse.Color = Colors.Yellow;
   mViewButtons[i].Effect = dse;
} 

但是,当处于“以前使用”状态时单击切换按钮时,触发器似乎没有任何效果。 外部发光不会改变。

我究竟做错了什么? 触发器不会影响 XAML 中未设置的内容吗?

In my app I have toggle buttons that have three possible states; "Unchecked", "Checked", and "Previously Used". When the user comes to this particular screen, some of the toggle buttons will be in the "Previously Used" state to show what work has been done. Clicking on a toggle button (no matter the current state) will place it into the "Checked" state. Only one of these toggle buttons can be checked at a time. The different states are indicated by different colored outer glow, or no glow at all.

To set the outer glow for the "Checked" state I use a trigger on IsChecked euqals true.

<ControlTemplate.Triggers>
   <Trigger Property="IsChecked" Value="True">                          
      <Setter Property="Effect">
         <Setter.Value>
            <DropShadowEffect Color="Salmon" BlurRadius="40" ShadowDepth="0" Opacity="1.0"></DropShadowEffect>
         </Setter.Value>
      </Setter>
   </Trigger>                        
</ControlTemplate.Triggers>

For the "Previously Used" state I apply the outer glow in code, not markup. I have to do this because determining if the button should be in this state is done by checking values in a List.

if (mExistingViews.Contains(mViews[i].LocalizedName))
{
   DropShadowEffect dse = new DropShadowEffect();
   dse.ShadowDepth = 0;
   dse.BlurRadius = 20;
   dse.Opacity = 1.0;
   dse.Color = Colors.Yellow;
   mViewButtons[i].Effect = dse;
} 

However, when a toggle button is clicked when in the "Previously Used" state, the trigger doesn't seem to have any effect. The outer glow does not change.

What am I doing wrong? Will a trigger not effect something that has not been set in XAML?

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

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

发布评论

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

评论(1

随心而道 2024-08-06 13:55:22

Button 上设置 DropShadowEffect 后,您就设置了 ButtonEffect 属性的本地值,这是一个 DependencyProperty (Button.EffectProperty)。 本地值会覆盖任何其他可能的值,直到它被清除为止,如下所示:

button1.ClearValue( Button.EffectProperty );

但是,这只会使工作变得更加复杂,因为您还必须确保清除它(如果之前已设置且尚未清除)。

相反,您可以创建一个 AttachedProperty PreviouslyUsed 来放置 ToggleButton 并使用 Bindings,以便它们更自动地获取其值。 然后在触发器中引用此 AttachedProperty 值,您将看到一个用于 PreviouslyUsed 的投影,另一个用于 IsChecked 的投影。

您必须连接的部分是绑定,您可能必须在某处使用 IValueConvertermViews[i].LocalizedName 转换为 truefalse 表示 PreviouslyUsed

不幸的是,我对 mViews 的设置了解不够,因此无法提供更多建议。 我不知道您的 ToggleButtons 是否是数据绑定的一部分。 我怀疑它们不是,因为您似乎正在迭代它们的数组。 例如,如果您将 mViews 对象绑定到 ListBoxItemsSource,您可以创建一个 DataTemplate生成 ToggleButtons ,其中 AttachedProperty 已就位。 这还可以通过将其绑定到是否在 ListBox 中选择该项目来简化您的 IsChecked 情况,然后 ListBox 负责处理确保只选择一个。

Once you set the DropShadowEffect on the Button, you are setting the local value of the Button's Effect property, which is a DependencyProperty (Button.EffectProperty). The local value overrides any other possible value until it is cleared like this:

button1.ClearValue( Button.EffectProperty );

However, that just makes the workings a bit more convoluted, because you also have to make sure you clear it if was set before and hasn't been cleared yet.

Instead, you could create an AttachedProperty PreviouslyUsed to put on the ToggleButtons and use Bindings so they get their value a bit more automagically. Then refer to this AttachedProperty value in your triggers and you'll have one drop shadow for PreviouslyUsed, and another for IsChecked.

The part you'll have to wire up is the binding, and you'll probably have to use an IValueConverter somewhere to turn mViews[i].LocalizedName into a true or false for PreviouslyUsed.

Unfortunately, I don't know enough about your setup with mViews and such to offer more advice about that. I don't know whether your ToggleButtons are part of databinding or not. I suspect that they are not, as you seem to be iterating an array of them. If you bound your mViews objects to the ItemsSource of a ListBox, for example, you could create a DataTemplate that generates the ToggleButtons with the AttachedProperty already in place. That would also simplify your IsChecked situation by binding it to whether or not that item is selected in the ListBox, and then the ListBox takes care of making sure that only one is selected.

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