是否可以将重复的DataTrigger Binding设置为样式?

时间:2012-11-09 08:51:50

标签: wpf silverlight xaml styles

是否可以将DataTrigger绑定设置为重复绑定但绑定路径不同的样式?

<Button Grid.Row="2" Grid.Column="1"   VerticalAlignment="Center" Background="{Binding Dyes[4].DyeBrush}">
        <Button.Style>
            <Style TargetType="Button" BasedOn="{StaticResource Dye}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=Dyes[4].IsReferenceDye}" Value="True">
                        <Setter Property="Content" Value="R" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

    <Button Grid.Row="3" Grid.Column="1"  VerticalAlignment="Center" Background="{Binding  Dyes[5].DyeBrush}">
        <Button.Style>
            <Style TargetType="Button" BasedOn="{StaticResource Dye}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=Dyes[5].IsReferenceDye}" Value="True">
                        <Setter Property="Content" Value="R" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

1 个答案:

答案 0 :(得分:0)

让你的风格像

<Style x:Key="ButtonStyle" TargetType="Button" BasedOn="{StaticResource Dye}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsReferenceDye}" Value="True">
                <Setter Property="Content" Value="R" />
            </DataTrigger>
        </Style.Triggers>
    </Style> 

并将其用作

<Button Grid.Row="3" Grid.Column="1" 
            DataContext="{Binding Dyes[5]}" 
            Style="{StaticResource ButtonStyle}"
            VerticalAlignment="Center" Background="{Binding  Dyes[5].DyeBrush}"/>

希望这有帮助。