使用控件模板覆盖ListBoxItem的Selected样式

时间:2016-10-09 07:46:02

标签: c# wpf

在我的WPF应用程序中,我正在使用ListBox控件。 我正在设置/使用ItemTemplate,所以我可以将自己的设计作为项目。 我希望在选择项目时具有默认突出显示样式。 所以,我创建了一个影响所有列表框的全局样式。 这不起作用。 这是我的观点:

<ListBox ItemsSource="{Binding SomeData}"
                 ItemTemplate="{StaticResource ListBoxItemTemplate}" >
</ListBox>

我的模板:

<Grid.Resources>
    <DataTemplate x:Key="ListBoxItemTemplate">
        <Border BorderThickness="2" BorderBrush="DarkGreen" CornerRadius="2" Padding="0"  Margin="0,0,14,0" >
            <Grid  Background="Transparent" HorizontalAlignment="Stretch" Width="250" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
                <Grid.ColumnDefinitions>
                <ColumnDefinition Width="70"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="17"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="22"></RowDefinition>
                </Grid.RowDefinitions>
                <Label FontWeight="DemiBold"  Grid.ColumnSpan="2" Background="{Binding PanelColor}" 
                       Content="{Binding ActionType}" HorizontalContentAlignment="Center"
                       HorizontalAlignment="Stretch"  Padding="0"
               VerticalAlignment="Center" Grid.Row="0" ></Label>
                <dc:AdvTextBox IsReadOnly="True"  Height="auto" BorderThickness="0" TextWrapping="Wrap"  Grid.ColumnSpan="2" Grid.Column="0" HorizontalAlignment="Left" Background="Transparent"  Width="253"     Grid.Row="1" Text="{Binding Description}" ></dc:AdvTextBox>
                <Label Margin="7,0,0,0" Padding="0" Foreground="{Binding ResponseColor}" Visibility="{Binding DisplayCustomerResponse}" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"
                    Content="{Binding FriendlyResponse, FallbackValue=''}" Style="{StaticResource LabelText}" VerticalContentAlignment="Center" VerticalAlignment="Stretch"></Label>
                <Label Margin="7,0,0,0" Padding="0" Visibility="{Binding DisplayErrorResponse}" Grid.ColumnSpan="2" VerticalContentAlignment="Center" VerticalAlignment="Stretch"
                    Grid.Row="3" Grid.Column="0" Content="{Binding ErrorResponse, FallbackValue=''}" Style="{StaticResource LabelText}" 
                    Foreground="Red"></Label>
                <StackPanel Margin="7,0,0,0" Orientation="Horizontal" Grid.ColumnSpan="2" Grid.Row="4" VerticalAlignment="Center" HorizontalAlignment="Left">
                <TextBlock Grid.Column="0" HorizontalAlignment="Center"><Hyperlink FontSize="12" >Remove</Hyperlink></TextBlock>
                <TextBlock Grid.Column="1" Margin="10,0,0,0" HorizontalAlignment="Center"><Hyperlink FontSize="12"
                    CommandParameter="{Binding ManifestRef}" 
                    Command="{Binding EditCommentAction}">Edit</Hyperlink></TextBlock>
            </StackPanel>
            </Grid>
        </Border>
    </DataTemplate>
</Grid.Resources>

我的全球风格:

<Style TargetType="ListBoxItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Border Name="_Border"
                        Padding="2"
                        SnapsToDevicePixels="true">
                    <ContentPresenter />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter TargetName="_Border" Property="Background" Value="White"/>
                        <Setter Property="Foreground" Value="Black"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我不理解的是什么?

0 个答案:

没有答案