如何在UserControl上为所有特定类型元素应用DataTrigger?

时间:2017-01-20 13:04:58

标签: c# wpf datatrigger

我有下一个UserControl:

<UserControl ...>
    <UserControl.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="..." />
                </ResourceDictionary.MergedDictionaries>
                <Style TargetType="{x:Type local:MyButton}" BasedOn="{DynamicResource MyButtonStyle}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=SmallButtons}" Value="True" >
                            <Setter Property="Margin" Value="4"/>
                            <Setter Property="FontSize" Value="8"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ResourceDictionary>
        </UserControl.Resources>
    <Grid>
      ...
      <local:MyButton ..Style="{DynamicResource MyButtonStyle}" />
      <local:MyButton ..Style="{DynamicResource MyButtonStyle}" />
      <local:MyButton ..Style="{DynamicResource MyButtonStyle}" />
      ...
      <local:MyButton ..Style="{DynamicResource MyButtonStyle}" />
    </Grid>
</UserControl>

问题是<DataTrigger Binding="{Binding Path=SmallButtons}" Value="True" >无效。有什么问题以及如何使其发挥作用?

我希望触发器应用于此网格上的所有MyButton

DataContext没关系,应用数据时有OnPropertyChanged事件,但我从来没有在SmallButtons get方法上获得断点。

1 个答案:

答案 0 :(得分:0)

您明确将按钮的样式设置为名为&#34; MyButtonStyle&#34;的样式。所以你的隐含风格基于&#34; MyButtonStyle&#34;不会申请。

尝试删除Button元素的Style属性:

<Grid>
    ...
    <local:MyButton />
    <local:MyButton />
    ...
</Grid>