0

I have a a ListView that's bound to a collection of objects in my ViewModel.

Everything is fine, but I want to change the font size of the items displayed in the list.

So I have

    <ListView ItemsSource="{Binding MyItems}" Grid.Row="1" Margin="0, 0, 0, 32">
        <ListView.View>
            <GridView AllowsColumnReorder="false">
                <GridViewColumn DisplayMemberBinding="{Binding Path=Id}" Header="Id"  />
                <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="Name" />
            </GridView>
        </ListView.View>
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="FontSize" Value="12" />
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

However, elsewhere I have

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="FontSize" Value="18" />
</Style>

So the font size in the ListView.ItemContainerStyle is ignored.

The answer to this question : Overriding implicit styles in nested controls - shows how to override this, by changing the ContentPresenter. So I wanted to do something similar.

I change my style to...

   <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="FontSize" Value="12" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListViewItem">
                        <ContentPresenter
                             Content="{TemplateBinding Content}"
                             ContentTemplate="{TemplateBinding ContentTemplate}">
                             <ContentPresenter.Resources>
                                 <Style
                                     TargetType="{x:Type TextBlock}"
                                     BasedOn="{x:Null}" />
                             </ContentPresenter.Resources>
                        </ContentPresenter>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>

Now, it respects the FontSize as set in the snippet above, but instead of displaying the actual content in the list it displays the name of the class! (MyNamespace.MyItemsModel)

I have seen similar questions, however they involve the DataTemplate of each individual GridViewColumn. I would rather not set the DataTemplate of every column because actually there are a lot more columns (the code above is a simplication) and it would seem like a lot repetitive work...

komodosp
  • 3,316
  • 2
  • 30
  • 59

0 Answers0