ListViewItem样式绑定无法正常工作

时间:2012-10-04 12:10:11

标签: wpf xaml templates binding styles

我设置了ListViewItem(ItemContainerStyle)的样式,但是如果我在样式中创建自定义ControlTemplate,则它不会绑定更正数据。

<ListView x:Name="PART_lstWidgets" ItemsSource="{Binding Items}" Grid.Row="1" ItemContainerStyle="{StaticResource ItemContStyle}" BorderThickness="0" Background="Transparent">
<ListView.View>
    <GridView ColumnHeaderContainerStyle="{StaticResource NoHeaderStyle}">
        <GridViewColumn Width="27">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox VerticalAlignment="Center" Margin="0,2,0,1" IsChecked="{Binding IsVisibile}"/>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
        <GridViewColumn Header="Title">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Title}"/>
                        <TextBlock Text="{Binding SubTitle}" FontStyle="Italic" Margin="4,0,0,0"/>
                    </StackPanel>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
    </GridView>
</ListView.View>

这是ListViewItem/ItemContainerStyle

的风格
<Style x:Key="ItemContStyle" TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource DefaultListViewStyle}">
    <Setter Property="FontSize" Value="10"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="Transparent"/>
                        <Setter Property="BorderBrush" Value="Transparent"/>
                    </Trigger>
                    <Trigger Property="jas:ListViewItemDragState.IsBeingDragged" Value="True">
                        <Setter Property="Foreground" Value="{StaticResource HoverListsItemForeground}"/>
                        <Setter Property="Background" Value="{StaticResource HoverListsItemBackground}"/>
                        <Setter Property="BorderBrush" Value="{StaticResource HoverListsItemBorder}"/>
                    </Trigger>
                    <Trigger Property="jas:ListViewItemDragState.IsUnderDragCursor" Value="True">
                        <Setter Property="Foreground" Value="{StaticResource SelectedListsItemForeground}"/>
                        <Setter Property="Background" Value="{StaticResource SelectedListsItemBackground}"/>
                        <Setter Property="BorderBrush" Value="{StaticResource SelectedListsItemBorder}"/>
                    </Trigger>

                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                        <Setter Property="Background" Value="{StaticResource ListsItemAlternateBackground}"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="{StaticResource DisabledListsItemForeground}"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="true"/>
                            <Condition Property="Selector.IsSelectionActive" Value="false"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" Value="Transparent"/>
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

0 个答案:

没有答案