Page 1 of 1

[c++]About Style of ListViewItem

Posted: 08 Oct 2019, 13:07
by tkawa
Hi,

I found two different behaviors regarding the style of TextBlock created when specifying "DisplayMemberBinding".
・ VerticalAlignment of created TextBlock is not "Center" by default
・Style defined in Resources is not reflected
Check the code below.
<ListView Grid.Row="3" FontSize="12pt" ItemsSource="{Binding ItemList}">
    <ListView.Resources>
        <Style TargetType="TextBlock" >
            <Setter Property="Background" Value="Red"/>
        </Style>
    </ListView.Resources>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="ColA" DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn Header="ColB">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Grid Height="50">
                            <TextBlock Text="{Binding Path=Name}"/>
                        </Grid>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="ColC">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Grid Height="50">
                            <Grid.Resources>
                                <Style TargetType="TextBlock" >
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                    <Setter Property="Background" Value="LightBlue"/>
                                </Style>
                            </Grid.Resources>
                            <TextBlock Text="{Binding Path=Name}"/>
                        </Grid>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>

        </GridView>
    </ListView.View>
</ListView>

Re: [c++]About Style of ListViewItem

Posted: 10 Oct 2019, 11:10
by sfernandez
Hi,

The vertical alignment difference comes from our default style for the ListViewItem, that sets VerticalContentAlignment to Stretch, while Windows is setting it as Center.
You can override that value from the default style specifying a new Style:
<ListView Grid.Row="3" FontSize="12pt" ItemsSource="{Binding ItemList}">
    <ListView.Resources>
        <Style TargetType="TextBlock" >
            <Setter Property="Background" Value="Red"/>
        </Style>
        <Style TargetType="ListViewItem">
            <Setter Property="VerticalContentAlignment" Value="Center"/>
        </Style>
    </ListView.Resources>
    ...
The problem with generated TextBlocks from DisplayMemberBinding is a bug in our code, could you please report it?
It is incorrectly doing the resource look up only in the ListViewItem scope.

Re: [c++]About Style of ListViewItem

Posted: 16 Oct 2019, 05:14
by tkawa
Thank you, I reported the issue here: #1570

Re: [c++]About Style of ListViewItem

Posted: 16 Oct 2019, 12:34
by sfernandez
Thanks for the report.