如何使用WPF使用Style制作DataTrigger?

时间:2018-02-02 20:07:00

标签: c# wpf xaml

大家下午好,我试图让DataTrigger根据空间改变元素的颜色,但是当我把DataTrigger放在我的风格旁边时,DataTrigger就会停止工作。有人知道原因吗?

请遵循以下代码:

XAML:

<Button x:Name="BtMesa"
        Command="{Binding DataContext.ControleDeAcaoDeMesaCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
        CommandParameter="{Binding .}"
        Grid.Column="1" Grid.Row="0" Width="110" Height="87"
        HorizontalAlignment="Left" VerticalAlignment="Top" Cursor="Hand">
    <Button.Style>
        <Style BasedOn="{StaticResource menuMesa}" TargetType="Button">
            <Style.Triggers>
                <DataTrigger Binding="{Binding StatusMesa}" Value="2">
                    <Setter Property="Background" Value="#7a7a7a7a"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
    <StackPanel Orientation="Vertical">
        <TextBlock Text="{Binding Nome}" FontSize="20px" Foreground="#696969" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" FontFamily="Open Sans" Margin="0,20,0,0" />
        <TextBlock Text="{Binding TotalAPagar, StringFormat=C2}" TextAlignment="Center" FontSize="14px" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Top" FontWeight="Bold" FontFamily="Open Sans" Background="Transparent" Padding="0,0,0,0">
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Setter Property="Visibility" Value="Visible"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding StatusMesa}" Value="1">
                            <Setter Property="Visibility" Value="Hidden"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </StackPanel>
</Button>

样式:

<Style x:Key="menuMesa" TargetType="Button">
    <Setter Property="Foreground" Value="#898989" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border Name="border" BorderThickness="1" BorderBrush="#f2f2f2" Background="#cfcfcf">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="border" Property="BorderBrush" Value="#f2f2f2" />
                        <Setter TargetName="border" Property="Background" Value="#7a7a7a7a" />
                        <Setter TargetName="border" Property="BorderThickness" Value="1" />
                        <Setter Property="Foreground" Value="#fcaf17" />
                    </Trigger>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter TargetName="border" Property="BorderBrush" Value="#f2f2f2" />
                        <Setter TargetName="border" Property="Background" Value="#7a7a7a7a" />
                        <Setter TargetName="border" Property="BorderThickness" Value="1" />
                        <Setter Property="Foreground" Value="#fcaf17" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如果有人能帮助我,我会非常感激。谢谢。

0 个答案:

没有答案