简体   繁体   中英

DataTrigger Border in a ListBoxItem

I'm trying to change the backgroundcolor and borderbrush color of a ListBoxItem.

<Window x:Class="Application1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:PE2_WPF_Jelle_Vandekerckhove"
    Title="MainResolutions" Height="500" Width="300" ResizeMode="NoResize" Closed="Window_Closed">
<Window.Resources>

    <Style TargetType="Border">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=Resafgehandeld}" Value="True">
                <Setter Property="Background" Value="LightGreen" />
                <Setter Property="BorderBrush" Value="DarkGreen" />
            </DataTrigger>
        </Style.Triggers>
    </Style>



</Window.Resources>
<StackPanel Orientation="Vertical" Background="Aquamarine">
    <TextBlock Text="Mijn Resolutions voor het jaar 2015" Margin="5" Background="White" />
    <ListBox Name="lstResolutions" Height="160">
        <ListBox.ItemTemplate>
            <DataTemplate>

                <Border Name="Rand" BorderBrush="Black" BorderThickness="2" Padding="5" Margin="5" CornerRadius="10" Width="240">

                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Row="0" Grid.Column="0" Text="Resolutie : "/>
                        <TextBlock Grid.Column="1" Text="{Binding Resitem}"/>
                        <TextBlock Grid.Row="1" Grid.Column="0" Text="Deadline : "/>
                        <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Resdeadline}" />
                        <TextBlock Grid.Row="2" Grid.Column="0" Text="Reden :" />
                        <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Resopmerking}" />
                    </Grid>

                </Border>
            </DataTemplate>

        </ListBox.ItemTemplate>

    </ListBox>

I tried using <Style TargetType="ListBoxItem"> instead of <Style TargetType="Border"> which worked but it's not what i want, i want the Border background of that listboxitem changed but i can't seem to access it. Any help would be appreciated.

It's not working because you have the BorderBrush hard-coded in the attribute, which takes precedence. Update so that the style is not a resource, and add the Trigger to the Border itself.

<Window x:Class="Application1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:PE2_WPF_Jelle_Vandekerckhove"
    Title="MainResolutions" Height="500" Width="300" ResizeMode="NoResize" Closed="Window_Closed">
<StackPanel Orientation="Vertical" Background="Aquamarine">
    <TextBlock Text="Mijn Resolutions voor het jaar 2015" Margin="5" Background="White" />
    <ListBox Name="lstResolutions" Height="160">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border Name="Rand" BorderThickness="2" Padding="5" Margin="5" CornerRadius="10" Width="240">
                    <Border.Style>
                        <Style TargetType="Border">
                            <Setter Property="BorderBrush" Value="Black" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=Resafgehandeld}" Value="True">
                                    <Setter Property="Background" Value="LightGreen" />
                                    <Setter Property="BorderBrush" Value="DarkGreen" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Row="0" Grid.Column="0" Text="Resolutie : "/>
                        <TextBlock Grid.Column="1" Text="{Binding Resitem}"/>
                        <TextBlock Grid.Row="1" Grid.Column="0" Text="Deadline : "/>
                        <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Resdeadline}" />
                        <TextBlock Grid.Row="2" Grid.Column="0" Text="Reden :" />
                        <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Resopmerking}" />
                    </Grid>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM