rounded border on textblock issue - wpf

I am trying to give my textblocks rounded borders. From what I've researched, I should embed a textblock within a border tag and then set the corner radius of the border. For me, it's having the effect of putting a border around the entire row. What am I doing wrong? I set the color of the border to blue to show what is happening. Ideally, I would change it to the same color as the background of the textblock to just give it seamless rounded corners.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Border Grid.Row="0" Margin="25" Grid.Column="1" Background="Blue"
BorderThickness="1" BorderBrush="Red" CornerRadius="30">
<TextBlock Margin="50" Padding="200,0,200,0"
FontSize="100"
FontWeight="Bold"
Background="Black"
Foreground="White"
VerticalAlignment="Center"
Text="bla bla bla"
HorizontalAlignment="Center">
</TextBlock>
</Border>
</Grid>

Look at the HorizontalAlignment / VerticalAlignment values for both the Grid and the Border. Set whatever is most appropriate for your requirements.
A tool such as Kaxaml is great for playing around with this sort of thing without having to build an entire application.

A better way to do this is to create a copy of the default style for TextBox and modify the border in the ControlTemplate. Here is a simple app that has the default TextBox style extracted and modified to round the corners.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="TextBox.Static.Border" Color="#FFABAdB3"/>
<SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FF7EB4EA"/>
<SolidColorBrush x:Key="TextBox.Focus.Border" Color="#FF569DE5"/>
<Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True" CornerRadius="5">
<ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" TargetName="border" Value="0.56"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.MouseOver.Border}"/>
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.Focus.Border}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/>
<Condition Property="IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
</MultiTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<StackPanel>
<TextBox Margin="10" Style="{DynamicResource TextBoxStyle1}" Text="bla bla bla"></TextBox>
</StackPanel>
</Grid>
</Window>
If you want this style to apply to all textboxes in your app, you should move the style into a resource dictionary and remove x:Key="TextBoxStyle1" from the style definition. It would then apply to all TextBox by default and remove the need to set a Style for each one.

Related

WPF TextBox has Blue Outline while in focus

So I am working on a program in WPF, and I made a textbox that has no border when out of focus, and (should) have a black line while focused. However, the line is showing up blue. I looked around Google, and nothing seems to work.
<Window x:Class="RequestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:RequestApp"
mc:Ignorable="d"
Title="MainWindow" Height="900.156" Width="1414.292">
<Window.Resources>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="Text" Value="Come Jam wit me man" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="7*"/>
<RowDefinition Height="18*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1067*"/>
<ColumnDefinition Width="433*"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="Request" HorizontalAlignment="Left" Margin="37,130,0,0" BorderThickness="0" Text="Request" VerticalAlignment="Top" RenderTransformOrigin="-2.971,0.656" Height="39" Width="594" FontFamily="Modern Sans" FontSize="25" InputScope="Default" GotFocus="Request_GotFocus" LostFocus="Request_LostFocus" SelectionBrush="{x:Null}" BorderBrush="Black" Foreground="Gray"/>
</Grid>
PS: I don't know the first thing about XAML
Below is the default style for a TextBox. The behavior you are fighting is caused by these two ControlTemplate triggers - IsMouseOver and IsKeyboardFocused. Just edit the solid color brush definitions as you wish:
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" TargetName="border" Value="0.56"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.MouseOver.Border}"/>
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.Focus.Border}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/>
<Condition Property="IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
</MultiTrigger>
</Style.Triggers>
</Style>
I found this question after searching for a way to get rid of the border in a WPF textbox what is shown blue and didn't fit into my design.
I have done this by setting the Border-Thickness to "0":
<TextBox x:Name="textBoxSearch" BorderThickness="0"/>

ToggleButton Style; Override IsEnabled

Once the ToggleButton is checked, the Button should be set to disabled and the foreground color should change. The following is not working:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ToggleButton Content="Hallo" Margin="113,97,72,46" >
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="IsEnabled" Value="True"/>
<Setter Property="IsChecked" Value="False"/>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="IsEnabled" Value="False"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
</Grid>
How can I do this?
The appearance of disabled controls is different across different versions of windows and different Windows account settings etc, this is why it's working for others but not you. Generally speaking you should respect such settings, but if you really are adamant then you'll need to explicitly override the template as demonstrated in this question:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>

Custom TextBox with part of fixed content

I need to create a custom TextBox in WPF and the textbox will have some digit and the string "% Per Annum" like "55% Per Annum". The user should be able to alter only the digit from the textbox, I want to keep the string "% Per Annum" as uneditable. Please help me.
In WPF you can modify the template of a control to completely change the appearance while keeping the functionality. In your case you can modify the control template of a TextBox by adding some extra text inside the border of the control.
In Visual Studio add a TextBox to your XAML. Then in the Document Outline you can right click the TextBox and select Edit Template => Edit a Copy.... A Style for a TextBox is then added to your XAML including a copy of the ControlTemplate used by the default TextBox. You can then modify the template as you like.
The main part of the TextBox is the ScrollViewer inside the ListBoxChrome border. To get the result you desire you can wrap the ScrollViewer inside a Grid with two columns and then place a TextBlock with the text "% Per Annum" in the second column. You probably also want to right align the text in the TextBox and you can do that by default by setting HorizontalContentAlignment to Right in the Style of the TextBox.
Here is a complete sample XAML where I have added some comments to show where I made the edits:
<Window
x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<LinearGradientBrush x:Key="TextBoxBorder" EndPoint="0,20" MappingMode="Absolute" StartPoint="0,0">
<GradientStop Color="#ABADB3" Offset="0.05"/>
<GradientStop Color="#E2E3EA" Offset="0.07"/>
<GradientStop Color="#E3E9EF" Offset="1"/>
</LinearGradientBrush>
<Style x:Key="PerAnnumTextBoxStyle" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<!-- New setter -->
<Setter Property="HorizontalContentAlignment" Value="Right"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Themes:ListBoxChrome x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" SnapsToDevicePixels="true">
<!-- ScrollViewer wrapped in a Grid -->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<TextBlock Grid.Column="1" Margin="0,1"> % Per Annum</TextBlock>
</Grid>
</Themes:ListBoxChrome>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/>
<Condition Property="IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
</MultiTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<TextBox Text="50" Style="{StaticResource PerAnnumTextBoxStyle}" Width="100" HorizontalAlignment="Left"/>
</StackPanel>
</Window>
The simplest and in my opionion better way is to put the text box inside a StackPanel
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Path=MyTextProperty}"/>
<Label Content="55% Per Annum" />
</StackPanel>

Change Fill property of Path control inside ListBox

I have a small question, I've created a ListBox that only contains 2 items. Each item is a Path control that has a Fill attribute set to Black.
Now, what I'm trying to do is, change the colour of this Fill attribute when you select one of the items in the listbox... I would think this should be done with a Style. But when doing so, the style contains a ContentPresenter that maps to the Path and this ContentPresenter has no Fill attribute to change through the IsSelected trigger!
So in other words, how can I still use a Style that maps the Fill attribute?
My current XAML code of the Window in the WPF project:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="XAMLPathStyleProblem.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Window.Resources>
<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Padding" Value="2,0,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true"/>
<Condition Property="Selector.IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<ListBox x:Name="ImageBar" ItemContainerStyle="{DynamicResource ListBoxItemStyle}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem>
<Path Data="M15.992466,14.680105C20.892824,14.680104 23.97299,17.360288 28.013305,17.360288 31.943504,17.360288 34.333683,14.680104 39.994102,14.680105 44.274425,14.680104 48.804641,17.000391 52.034961,21.0308 41.454162,26.831151 43.174373,41.942682 53.865043,45.983101 52.394974,49.24342 51.694851,50.703518 49.794819,53.583672 47.154625,57.614079 43.424389,62.624561 38.803959,62.664604 34.703705,62.704647 33.643696,59.994431 28.073246,60.024464 22.50292,60.054249 21.342806,62.714657 17.23254,62.674614 12.622241,62.634571 9.0819604,58.104115 6.441766,54.083717 -0.95864094,42.822647 -1.7287129,29.611443 2.8315349,22.590761 6.0717456,17.600301 11.19209,14.680104 15.992466,14.680105z M38.751411,0C39.321331,3.8093758 37.761547,7.538764 35.701835,10.178331 33.502144,12.997869 29.702673,15.197509 26.033186,15.077528 25.373277,11.438125 27.093038,7.6887398 29.172746,5.1591539 31.462427,2.3696117 35.39188,0.23996067 38.751411,0z"
Fill="Black" />
</ListBoxItem>
<ListBoxItem>
<Path Data="M32.127438,4.0459317E-05C34.679321,-0.0059787218,51.370113,0.63573532,60.553993,18.050023L60.522991,18.050023 60.543671,18.086075C61.200066,19.24132 68.004066,31.93957 59.575981,47.967091 59.575981,47.967091 51.176838,64.148377 30.558096,63.870453L29.65756,63.847004 29.649204,63.861397C29.644096,63.870198,29.641504,63.874661,29.641504,63.874661L29.638971,63.874695 29.681444,63.800747C30.804413,61.84562,39.865662,46.068413,42.345753,41.710415L42.378082,41.653572 42.392643,41.638874 42.472183,41.501145 42.692501,41.246766C44.087284,39.55642,45.09919,37.538369,45.595421,35.325478L45.613995,35.233231 45.681602,34.931549C45.857914,34.084336,45.977459,33.046578,45.939392,31.839535L45.927822,31.607016 45.927765,31.604247 45.927345,31.597495 45.913135,31.311926 45.901112,31.172703 45.89138,31.015126 45.867527,30.783802 45.865814,30.76396 45.8638,30.747662 45.831325,30.432713C45.783504,30.046782,45.720222,29.665644,45.64212,29.289938L45.605244,29.129017 45.579826,29.001641C45.3101,27.769034 44.871658,26.423209 44.200989,24.977549 43.870582,24.491171 43.539108,24.000555 43.182049,23.514327L42.899601,23.140976 60.287002,18.042616C60.287002,18.042616,39.292564,18.022913,34.351002,18.039915L34.393581,18.050023 34.172077,18.050023C33.849613,18.050023,33.54248,18.050023,33.252323,18.050023L33.158501,18.050023 32.880497,18.023783C32.497307,17.992794 32.109821,17.977 31.718649,17.977 31.350422,17.977 30.985464,17.990992 30.624279,18.018473L30.292705,18.050023 30.278829,18.050023C30.225145,18.050023 30.197481,18.050023 30.197481,18.050023 30.197481,18.050023 30.175093,18.049284 30.131918,18.049599 29.747402,18.052403 27.714258,18.138693 25.166611,19.573328L25.156681,19.579142 25.090729,19.612418C22.198151,21.138638,19.8955,23.632718,18.613605,26.663617L18.496868,26.959704 5.5749997,14.177C5.5749997,14.177,15.021078,30.765849,17.85829,35.692574L17.988001,35.917668 18.035503,36.093228C19.728666,42.05547 25.213291,46.422997 31.718649,46.422997 32.332252,46.422997 32.936783,46.384125 33.529907,46.308712L33.816097,46.268658 29.596954,63.874993 29.542429,63.874833C28.213777,63.865578 13.814976,63.407895 4.1510181,48.093563 4.1510176,48.093563 -5.6624084,32.728032 4.8882693,15.012328L5.3907794,14.192161 5.3934535,14.187385C5.6228327,13.780242 13.109029,0.74591898 31.796461,0.0057129142 31.796461,0.0057133239 31.911178,0.00055203911 32.127438,4.0459317E-05z"
Fill="Black" />
</ListBoxItem>
</ListBox>
</Grid>
</Window>
I recommend you using MVVM pattern with MVVM Light Toolkit. Then you should create Collection of data that you want to dispaly on the list. In your case it can be Data and Fill (Color). Then you need to handled Click event and that's all. I have code that provides such feature, so If you want it, please ask.
But in your case (without MVVM Light toolkit) I would rather do sth like this:
<Style x:Key="Path" TargetType="{x:Type Path}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource, AncestorType={x:Type ListBoxItem}}, Path=IsSelected" Value="True">
<Setter Property="Fill" Value="YouColor"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource, AncestorType={x:Type ListBoxItem}}, Path=IsSelected" Value="False">
<Setter Property="Fill" Value="YouAnotherColor"/>
</DataTrigger>
</Style.Triggers>
</Style>
Best regards.
Mateusz
Ok... found the answer myself!
When changing the Fill value inside the Path control to a binding one, all works as expected!
Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"

Changing the Foreground colour of a contentpresenter in a listbox

I have created the following style for a listbox that will have an image displayed next to some text:
<Style x:Key="ImageListBoxStyle" TargetType="{x:Type ListBox}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<!-- Simple ListBoxItem - This is used for each Item in a ListBox. The item's content is placed in the ContentPresenter -->
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid SnapsToDevicePixels="true">
<Border x:Name="Border">
<Grid Height="40">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image
x:Name="DisplayImage"
Source="{Binding Path=ThumbnailImage}"
Height="30"
Width="30"
Grid.Column="0"/>
<ContentPresenter
x:Name="DisplayText"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Grid.Column="1"/>
<!--<ContentPresenter.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Black"/>
</Style>
</ContentPresenter.Resources>-->
<!--Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=DisplayMemberPath, Converter={StaticResource myDisplayMemberConverter}}"-->
<!--<Label
x:Name="Text"
Content="{Binding Path=FullNameAndTitle}"
Foreground="Black"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
VerticalContentAlignment="Center"
HorizontalAlignment="Stretch"
Grid.Column="1"
Height="40"/>-->
</Grid>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<!--<Setter Property="FontWeight" Value="Bold" TargetName="DisplayText"/>-->
<!--<Setter Property="Style" Value="{StaticResource SelectedTextStyle}" TargetName="DisplayText"/>-->
<Setter Property="Background" Value="DarkBlue" TargetName="Border"/>
<Setter Property="Width" Value="40" TargetName="DisplayImage"/>
<Setter Property="Height" Value="40" TargetName="DisplayImage"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<Grid>
<Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<ScrollViewer Margin="1,1,1,1" Focusable="false" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<StackPanel IsItemsHost="true"/>
</ScrollViewer>
</Grid>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I have to use the contentpresenter as I am filtering what is displayed (text wise) using the DisplayMemberPath of the ListBox itself.
All I want to do is set the FontWeight to Bold and the Foreground to White when an item is selected in the ListBox.
Has anyone encountered a problem like this? I have looked at some related questions but people have been able to use a TextBlock to get around their issues I can't unfortunately.
Any info ppl can give will be appreciated.
Cheers
There is also another way. You can add in your ContentPresenter this attribute
TextBlock.Foreground="YourColour"
In this case you can also use animations over that property.
It's all ok, I have managed to answer this question myself, I was trying to modify the foreground/fontweight of the contentpresenter which doesn't contain a definition for foreground/fontweight all i simply needed to do was this:
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="White"/>
i.e. remove the:
TargetName="DisplayText"
Based on this related answer, I was able to solve a similar issue with the following:
<Setter TargetName="ctContentPresenter" Property="TextBlock.Foreground" Value="{StaticResource StyleForeColorBrush}" />
<Storyboard x:Key="Storyboard1">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="myContentPresenter">
<EasingColorKeyFrame KeyTime="0" Value="Black"/>
<EasingColorKeyFrame KeyTime="0:0:0.2" Value="White"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>

Resources