user3819226
user3819226

Reputation: 481

How to make both ListViewItem's template style and ListView.ItemTemplate working?

I have the code below in my App.xaml. I override the template to make the background stays the same as transparent when I click/hover on a 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>

And I asked a question yesterday How to add a underlying progressbar across two or more ListViewItem in wpf? So I wrote below and it works greatly if I didn't apply the style above. If I do so, I will see nothing in the 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> 

I do need to make the background transparent but why won't these codes work together? How can I improve?

In addition, how do you guys know what's wrong here? I have no idea where can I find reference/explanations or to study the mechanism behind xaml code..

Upvotes: 4

Views: 21028

Answers (1)

Dinesh balan
Dinesh balan

Reputation: 495

Try Like this:

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

And I am using the book WPF 4.5 Unleashed as reference for Xaml.

Upvotes: 6

Related Questions