Search code examples
c#wpfxamldatatrigger

WPF Data Trigger


Hi I just wondered if I could get some help, I'm having alot of trouble getting the data trigger to work, if i remove the data trigger and place the drop shadow just as a setter it works. But i want to be able to give the user the option of turning on and off the drop shadow so I thought the data trigger would be the answer.

Basically I want to add the data trigger all over my control styles and just be able to change the fancyGraphics BOOL to TRUE or FALSE and have every controls drop shadow adjust.

Thanks in advance.

Here is my XAML

<sys:Boolean x:Key="fancyGraphics" >True</sys:Boolean>

<Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="#EEE"></Setter>
    <Setter Property="Foreground" Value="#555"></Setter>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">

                <Border Background="{TemplateBinding Background}" BorderThickness="1" BorderBrush="#DDD">
                    <Grid>
                        <ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0" />
                    </Grid>
                </Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="#333"></Setter>
                        <Setter Property="BorderBrush" Value="#888"></Setter>
                    </Trigger>

                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=fancyGraphics}" Value="True">
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect BlurRadius="20"
                              Opacity="0.5"
                              ShadowDepth="0"
                              Color="#111" />
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

Solution

  • The binding in DataTrigger looks for a property named fancyGraphics. But, fancyGraphics is a static resource. You should define the binding as such:

    <DataTrigger Binding="{Binding Source={StaticResource fancyGraphics}}" Value="True">
        <Setter Property="Effect">
            <Setter.Value>
                <DropShadowEffect BlurRadius="20"
                                  Opacity="0.5"
                                  ShadowDepth="0"
                                  Color="#111" />
            </Setter.Value>
        </Setter>
    </DataTrigger>