使用DataTriggers的ItemsControl [英] ItemsControl with DataTriggers
问题描述
有人可以提供一个简单的示例,如何在ItemsControl上使用DataTriggers?
Can someone provide a simple example how could you use DataTriggers on an ItemsControl?
例如,如果我说这样的话:
For example if i say something like this:
<ItemsControl.Triggers>
<DataTrigger Binding="{Binding Items.Count}" Value="2">
<Setter TargetName="DocHost" Property="UniformGrid.Rows" Value="2"/>
</DataTrigger>
</ItemsControl.Triggers>
给我一个错误,说ItemsControl需要一个事件触发器.可悲的是,我必须在内部使用DataTriggers和ItemsControl.我该怎么办?
It gives me an error saying that ItemsControl expects an event trigger. Sadly i must use DataTriggers inside and ItemsControl. How can i do that?
推荐答案
您不能在TriggerCollection
中使用DataTrigger
...是的,是的,我知道...这很疯狂.但是,您可以 将一个放在Style
的TriggerCollection
中:
You cannot use a DataTrigger
in a TriggerCollection
... yes, yes, I know... it's madness. However, you can put one in the TriggerCollection
of a Style
:
<ItemsControl.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Items.Count}" Value="2">
<Setter TargetName="DocHost" Property="UniformGrid.Rows" Value="2"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ItemsControl.Style>
更新>>>
UPDATE >>>
对不起,我没有在那看到TargetName
.答案是将DataTrigger
移到UnifrmGrid.Style
并删除TargetName
属性,但随后可能在Binding
的Items
属性上遇到麻烦...让我知道您是否还有其他问题.
Sorry, I didn't see that TargetName
in there. The answer is to move this DataTrigger
into the UnifrmGrid.Style
instead and remove the TargetName
property, but then you might have some trouble Binding
to the Items
property... let me know if you have any more problems.
这篇关于使用DataTriggers的ItemsControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!