ListBoxItem中的DataTrigger边框

时间:2016-01-02 23:00:29

标签: c# xaml

我正在尝试更改ListBoxItem的backgroundcolor和borderbrush颜色。

<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>

我尝试使用<Style TargetType="ListBoxItem">而不是<Style TargetType="Border">但这不是我想要的,我想要更改该listboxitem的边框背景,但我似乎无法访问它。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

它不起作用,因为你在属性中有BorderBrush硬编码,它优先。更新以便样式不是资源,并将Trigger添加到Border本身。

<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>