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>

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?

有帮助吗?

解决方案

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 >>>

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top