Search code examples
c#wpfxaml

How to change Listview default text color?


How i could change the default text color of this ListView? default is black, i couldnt find a "text color" option under the properties window (visual studio).

   <ListView
        Name="LV"
        Grid.Row="2"
        HorizontalContentAlignment="Stretch"
        FocusManager.IsFocusScope="True"
        MouseDoubleClick="LV_MouseDoubleClick"
        ScrollViewer.ScrollChanged="LV_ScrollChanged" Foreground="White" BorderBrush="#FFABADB3" Background="Black">
        <ListView.Resources>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListViewItem}">
                            <Border
                                x:Name="Bd"
                                Padding="{TemplateBinding Padding}"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                SnapsToDevicePixels="true">
                                <ContentPresenter
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="Selector.IsSelectionActive" Value="False" />
                                        <Condition Property="IsSelected" Value="True" />
                                    </MultiTrigger.Conditions>
                                    <Setter TargetName="Bd" Property="Background" Value="#FFFF0080" />
                                </MultiTrigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="Selector.IsSelectionActive" Value="True" />
                                        <Condition Property="IsSelected" Value="True" />
                                    </MultiTrigger.Conditions>
                                    <Setter TargetName="Bd" Property="Background" Value="#FFFF0080" />
                                </MultiTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.Resources>
        <ListView.ItemTemplate>
            <DataTemplate>
                <local:TraceEventElement Event="{Binding}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

I tried these options, none was able to change the text color:

            Foreground="Red"
            TextBlock.Foreground="Red"
            TextElement.Foreground="Red"

Solution

  • You can modify TraceEventElement class by adding Foreground dependency property by DependencyProperty.AddOwner method and replacing hardcoded Brushes.Black with this Foreground.

    public class TraceEventElement : FrameworkElement
    {
        public Brush Foreground
        {
            get { return (Brush)GetValue(ForegroundProperty); }
            set { SetValue(ForegroundProperty, value); }
        }
        public static readonly DependencyProperty ForegroundProperty =
            TextElement.ForegroundProperty.AddOwner(typeof(TraceEventElement));
    
        ...
    }
    

    Then TextElement.Foreground at ListView level should work.