WPF ResourceDcitionary ItemContainerStyle

时间:2014-07-23 09:22:51

标签: c# wpf

我想在Click Handler添加ListViewItems。我找到了很多关于如何做到这一点的教程,但是我在ResourceDictionary内重新创建了这个问题。我的问题是如何在那里定义ItemContainerStyle。它没有这样的工作:

<Style TargetType="{x:Type ListViewItem}">        
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border BorderBrush="#5076A7" BorderThickness="1">
                    <Border.Background>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                            <GradientStop Color="#FFFFFF" Offset="0.0"/>
                            <GradientStop Color="#C0D3EA" Offset="1.0"/>
                        </LinearGradientBrush>
                    </Border.Background>
                    <StackPanel TextElement.FontFamily="Segoe UI" TextElement.FontSize="12">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="15"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <TextBlock Padding="3,0,0,0" Text="{Binding Betreff}" TextTrimming="CharacterEllipsis" Grid.Column="0" Grid.Row="0"/>
                            <Button FontSize="7" Content="X" Grid.Column="1" Grid.Row="0" 
                                    Command="{Binding DataContext.DeleteButtonCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
                                    CommandParameter="{Binding ItemId}"/>
                        </Grid>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Padding="3,0,0,0" Text="{Binding Kunde}"/>
                            <TextBlock Padding="3,0,0,0" Text="|"/>
                            <TextBlock Padding="3,0,0,0" Text="{Binding IncidentId}"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock FontWeight="Bold" Padding="3,0,0,0" Text="{Binding Ort}"/>
                            <TextBlock Padding="3,0,0,0" Text="("/>
                            <TextBlock Text="{Binding Alternative}"/>
                            <TextBlock Text=")"/>
                        </StackPanel>
                    </StackPanel>
                </Border>
            </ControlTemplate>                
        </Setter.Value>
    </Setter>
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" />
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <DataTrigger Binding="{Binding Betreff}" Value="Blocked">
            <Setter Property="Template" Value="{StaticResource BlockedListViewItem}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

1 个答案:

答案 0 :(得分:0)

将Style移动到MainWindow的Resources并将EventSetter直接添加到ListViewItem样式:

<Style TargetType="ListViewItem">
    ...
    <EventSetter Event="PreviewMouseLeftButtonDown"
                 Handler="ListViewItem_PreviewMouseLeftButtonDown"/>
</Style>