none
TextBlock Foreground problem RRS feed

  • Question

  • Hi,

    Am binding the ForeGround of TextBlock to a bool property of ViewModel. Am using Convertor to return Brush based on bool value, if its true return Brushes.Red else Brushes.Black.

    Am using this TextBlock in side a Gridview, when i select the GridViewRow the text foreground remains black. But i need a normal behaviour of windows text. As i am using Windows Classic Theme, when GridViewRow is selected the whole row will be in blue color and my text remains black. Which is very difficult to read.

    Can any one suggest some solution.

    My Code

    // ListView Code
    				<ListView Grid.Row="1">
                        <ListView.View>
                            <GridView>
                                <GridView.Columns>
                                    <GridViewColumn>
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <StackPanel Orientation="Horizontal" Margin="5">
                                                    <TextBlock Text="{Binding Name}" Foreground="{Binding IsMale, Converter={StaticResource BoolToBrush}}"></TextBlock>
                                                </StackPanel>
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>                               
                                </GridView.Columns>
                            </GridView>
                        </ListView.View>
                    </ListView>
    				
    // Convertor Code
    
    			if (value.GetType() == typeof(bool))
                {
                    if ((bool)value)
                    {
                        result = Brushes.Red;
                    }
                    else
                    {
                        result = Brushes.Black;
                    }
                }

    Regards.


    Please mark the post as answer if it is helpfull to you - Hiran Repakula

    Friday, September 14, 2012 1:50 PM

Answers

  • You could try to use below workarounds:

    1) change the selection color of your ListView:

    <ListView Name="listview">
        <ListView.Resources>
            <Style TargetType="ListViewItem">
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow" />
                </Style.Resources>
            </Style>
        </ListView.Resources>

    2) change the foreground of your textBlock when it is selected:

    <ListView Name="listview">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Margin="5">
                                    <TextBlock Name="textblock" Text="{Binding Name}" Foreground="{Binding IsMale, Converter={StaticResource BoolToBrush}}"></TextBlock>
                                </StackPanel>
                                <DataTemplate.Triggers>
                                    <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource  Mode=FindAncestor, AncestorType=ListViewItem}}" Value="true">
                                        <DataTrigger.Setters>
                                            <Setter TargetName="textblock" Property="Foreground" Value="Yellow"/>
                                        </DataTrigger.Setters>
                                    </DataTrigger>
                                </DataTemplate.Triggers>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Hiran.Repakula Monday, September 17, 2012 2:41 PM
    Monday, September 17, 2012 6:17 AM
    Moderator

All replies

  • Hello,

    Have considered changing the background of your GridView.Columns / Rows?

    Regards,

    Linvi

    Friday, September 14, 2012 5:42 PM
  • You could try to use below workarounds:

    1) change the selection color of your ListView:

    <ListView Name="listview">
        <ListView.Resources>
            <Style TargetType="ListViewItem">
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow" />
                </Style.Resources>
            </Style>
        </ListView.Resources>

    2) change the foreground of your textBlock when it is selected:

    <ListView Name="listview">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Margin="5">
                                    <TextBlock Name="textblock" Text="{Binding Name}" Foreground="{Binding IsMale, Converter={StaticResource BoolToBrush}}"></TextBlock>
                                </StackPanel>
                                <DataTemplate.Triggers>
                                    <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource  Mode=FindAncestor, AncestorType=ListViewItem}}" Value="true">
                                        <DataTrigger.Setters>
                                            <Setter TargetName="textblock" Property="Foreground" Value="Yellow"/>
                                        </DataTrigger.Setters>
                                    </DataTrigger>
                                </DataTemplate.Triggers>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Hiran.Repakula Monday, September 17, 2012 2:41 PM
    Monday, September 17, 2012 6:17 AM
    Moderator
  • Hi Sheldon,

    Exact thing which i was looking for. Thanks for your reply.


    Please mark the post as answer if it is helpfull to you - Hiran Repakula

    Monday, September 17, 2012 2:43 PM