如何使ListViewItem的模板样式和ListView.ItemTemplate工作?

时间:2015-05-15 04:37:56

标签: wpf xaml listview styles

我的App.xaml中有以下代码。当我单击/悬停在ListViewItem上时,我覆盖模板以使背景保持透明相同。

    <Style TargetType="{x:Type ListViewItem}">

        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Height" Value="{StaticResource RowHeight}"/>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Border
                     BorderBrush="Transparent"
                     BorderThickness="0"
                     Background="{TemplateBinding Background}">
                        <GridViewRowPresenter HorizontalAlignment="Stretch" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Width="Auto" Margin="0" Content="{TemplateBinding Content}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>

我昨天问了一个问题 How to add a underlying progressbar across two or more ListViewItem in wpf? 所以我在下面写了,如果我没有应用上面的风格,它会很有效。 如果我这样做,我将在ListView中看不到任何内容。

        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <ProgressBar Grid.ColumnSpan="2" Grid.ZIndex="0" Value="{Binding ProgressBarValue}"/>
                    <Label Grid.Column="0" Grid.ZIndex="1" Content="{Binding CCC}"/>
                    <Label Grid.Column="1" Grid.ZIndex="1" Content="{Binding DDD}"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate> 

我确实需要让背景透明,但为什么这些代码不能一起工作?我该如何改进?

另外,你们怎么知道这里有什么问题?我不知道在哪里可以找到参考/解释或研究xaml代码背后的机制..

1 个答案:

答案 0 :(得分:4)

尝试这样:

<ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListViewItem}">
                                <Border
                                     BorderBrush="Transparent"`enter code here`
                                     BorderThickness="0"
                                     Background="{TemplateBinding Background}">
                                    <GridViewRowPresenter HorizontalAlignment="Stretch" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Width="Auto" Margin="0" Content="{TemplateBinding Content}"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListView.ItemContainerStyle>
        <ListView.ItemTemplate>
            <DataTemplate>
                    <Grid Background="Red">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <ProgressBar Grid.ColumnSpan="2" Grid.ZIndex="0" Value="30"/>
                        <Label Grid.Column="0" Grid.ZIndex="1" Content="{binding}"/>
                        <Label Grid.Column="1" Grid.ZIndex="1" Text="{binding}"/>
                    </Grid>
                </DataTemplate>
        </ListView.ItemTemplate>

我正在使用本书WPF 4.5 Unleashed作为Xaml的参考。

相关问题