WPF 触发器影响代码中设置的值,而不是 XAML
在我的应用程序中,我有具有三种可能状态的切换按钮; “未选中”、“已选中”和“以前使用过”。 当用户来到这个特定屏幕时,一些切换按钮将处于“先前使用”状态,以显示已完成的工作。 单击切换按钮(无论当前状态如何)都会将其置于“已选中”状态。 一次只能选中这些切换按钮之一。 不同的状态由不同颜色的外发光或根本不发光来指示。
为了设置“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
Button
上设置DropShadowEffect
后,您就设置了Button
的Effect
属性的本地值,这是一个DependencyProperty
(Button.EffectProperty
)。 本地值会覆盖任何其他可能的值,直到它被清除为止,如下所示:但是,这只会使工作变得更加复杂,因为您还必须确保清除它(如果之前已设置且尚未清除)。
相反,您可以创建一个 AttachedProperty
PreviouslyUsed
来放置ToggleButton
并使用Bindings
,以便它们更自动地获取其值。 然后在触发器中引用此 AttachedProperty 值,您将看到一个用于PreviouslyUsed
的投影,另一个用于IsChecked
的投影。您必须连接的部分是绑定,您可能必须在某处使用
IValueConverter
将mViews[i].LocalizedName
转换为true
或false
表示PreviouslyUsed
。不幸的是,我对
mViews
的设置了解不够,因此无法提供更多建议。 我不知道您的ToggleButtons
是否是数据绑定的一部分。 我怀疑它们不是,因为您似乎正在迭代它们的数组。 例如,如果您将mViews
对象绑定到ListBox
的ItemsSource
,您可以创建一个DataTemplate
生成ToggleButtons
,其中 AttachedProperty 已就位。 这还可以通过将其绑定到是否在ListBox
中选择该项目来简化您的IsChecked
情况,然后ListBox
负责处理确保只选择一个。Once you set the
DropShadowEffect
on theButton
, you are setting the local value of theButton
'sEffect
property, which is aDependencyProperty
(Button.EffectProperty
). The local value overrides any other possible value until it is cleared like this: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 theToggleButton
s and useBindings
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 forPreviouslyUsed
, and another forIsChecked
.The part you'll have to wire up is the binding, and you'll probably have to use an
IValueConverter
somewhere to turnmViews[i].LocalizedName
into atrue
orfalse
forPreviouslyUsed
.Unfortunately, I don't know enough about your setup with
mViews
and such to offer more advice about that. I don't know whether yourToggleButtons
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 yourmViews
objects to theItemsSource
of aListBox
, for example, you could create aDataTemplate
that generates theToggleButtons
with the AttachedProperty already in place. That would also simplify yourIsChecked
situation by binding it to whether or not that item is selected in theListBox
, and then theListBox
takes care of making sure that only one is selected.