BorderBrush in wpf doesn't work - c#

I am trying to replace this default border color in a groupbox but it doesn't work.
here code snippet:
<GroupBox x:Name="groupBox" BorderBrush="Red" BorderThickness="5" HorizontalAlignment="Center" Height="278" Margin="107,74,33,0" VerticalAlignment="Top" Width="760">
<GroupBox.Header>
<Border Background="#FFDAD5D5" BorderBrush="#FFDAD5D5" BorderThickness="56">
<TextBlock Text="Installation data" />
</Border>
</GroupBox.Header>

Try to use Style definitions, like this: How to use style for GroupBox header?.
You can avoid, that any other style will override yours.
Besides your code gets a cleaner structure and you can re-use this style for other GroupBoxes.
<Style x:Key="MyGroupeBoxStyle" TargetType="{x:Type GroupBox}">
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="BorderThickness" Value="5"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Height" Value="278"/>
<Setter Property="Width" Value="760"/>
<Setter Property="Margin" Value="107,74,33,0"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<Border Background="#FFDAD5D5" BorderBrush="#FFDAD5D5" BorderThickness="56">
<Label Text="{Binding}"/>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
Use it as follow:
<GroupBox x:Name="groupBox"
Style="{StaticResource MyGroupeBoxStyle}"
Header="Installation data" />

i think GroupBox is inside another control which overlays your Border.
Maybe you can try to place your GroupBox to the front - just to try
Let me know whats happening.
Greedings

Related

WPF ToggleButton disappear when checked if I use ControlTemplate

There is something I'm missing but I do not understand what:
If I do this:
<ToggleButton Content="" FontFamily="Segoe MDL2 Assets" />
The button changes like this when pressed
But if I change it using the template then it disappears:
<ToggleButton>
<ToggleButton.Template>
<ControlTemplate>
<TextBlock Style="{StaticResource LargeIconStyle}" Text=""/>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
Before check:
After check:
I cannot understand why.
Can you help me?
The color of the checked button is the system one.
Just for your reference the styles are:
<Style
x:Key="LargeIconStyle"
BasedOn="{StaticResource BaseIconStyle}"
TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="32" />
</Style>
<Style x:Key="BaseIconStyle" TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Segoe MDL2 Assets" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="TextAlignment" Value="Center" />
</Style>
The problem is that you're using a ControlTemplate and the Template property. These are used for the UI and functionality of the control. So essentially, you're turning your control into a TextBlock.
Instead, you want to use a DataTemplate and the ContentTemplate property.

Button not coming in all listview rows

I have a customListView, in that I have created an image button with transparent background. But instead of being shown in all rows, It's only there in the last row
Button Style:
<Style TargetType="Button" x:Key="editButtonStyle" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
<Setter Property="Width" Value="20" />
<Setter Property="Height" Value="20" />
<Setter Property="Content">
<Setter.Value>
<Image Source="edit_icon.png" />
</Setter.Value>
</Setter>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
</Style>
The Call:
<DataTemplate>
<Button Style="{StaticResource editButtonStyle}">
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter Content="{TemplateBinding Content}"/>
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
Set x:Shared="False" on your editButtonStyle. You define an instance of Image in your style which of course can only be used once if it's a shared style (some kind of WPF optimization).
This would work in a DataTemplate since the FrameworkElementFactory would create a new Image for every item, but it doesn't work with styles.

Styling a WPF Passwordbox

I'm currently trying to do the following:
If no password is entered a text "Password" should be shown.
But with my template no password is shown, and if i'm using a decorator or a scrollviewer i can't change the color of the text.
Do you have any ideas to achieve this?
Here's my styling code:
<Style TargetType="{x:Type PasswordBox}" x:Key="Password">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="PasswordBox">
<Grid>
<PasswordBox Background="{StaticResource BrushDark}" Foreground="{StaticResource BrushTextNormal}" BorderBrush="{StaticResource BrushBorderInput}" BorderThickness="1"/>
<TextBlock HorizontalAlignment="Left"
VerticalAlignment="Center"
Text="Password"
Margin="5,0,5,0"
Foreground="#ff808080"
IsHitTestVisible="False"
x:Name="UserMessage"
Visibility="Hidden"/>
<!--<ScrollViewer Foreground="{StaticResource BrushTextNormal}" Background="{StaticResource BrushTextNormal}" x:Name="PART_ContentHost"/>-->
<!--<Decorator TextBlock.Foreground="White" x:Name="PART_ContentHost"/>-->
</Grid>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Tag" Value=""/>
<Condition Property="IsKeyboardFocusWithin" Value="False"/>
</MultiTrigger.Conditions>
<Setter Property="Visibility" TargetName="UserMessage" Value="Visible"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
If you create custom ControlTemplate for PasswordBox or TextBox you need to put ScrollViewer named x:Name="PART_ContentHost instead of inner PasswordBox
From PasswordBox Syles and Templates
PART_ContentHost - A visual element that can contain a FrameworkElement. The text of the PasswordBox is displayed in this element.
And change Foreground as another Setter in you Style. Also, as a side node, I would do the same with Background and use TemplateBinding in your ControlTemplate. This will give more flexibility and allow you to change Background and/or Foreground manually without changing ControlTemplate
<Style TargetType="{x:Type PasswordBox}" x:Key="Password">
<Setter Property="Foreground" Value="{StaticResource BrushTextNormal}" />
<Setter Property="Background" Value="{StaticResource BrushDark}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type PasswordBox}">
<Grid Background="{TemplateBinding Background}">
<ScrollViewer x:Name="PART_ContentHost" .../>
<TextBlock .../>
</Grid>
<ControlTemplate.Triggers>
<!-- removed -->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Change fontweight on button in WPF treeview

I have a templated treeview that is working just as i want, but i have a context menu which allows for a tier 1 node to be marked as "default".
I have a datatrigger which reacts to a property in the viewmodel, which should change the fontweight to bold just to visually show that this is the default node. But the setter will not change the fontweight on the button no matter what!
However if i change another value, like the foreground color for example, it works just fine, font size also no problem.
Why is this, can anyone explain this? Here is some code if needed:
Trigger:
<HierarchicalDataTemplate ItemsSource="{Binding Children,Mode=TwoWay,NotifyOnSourceUpdated=True}">
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
<Image x:Name="nodeImg" Source="{Binding Image}" MaxHeight="16" MaxWidth="16"/>
<Button x:Name="nodeButton" Content="{Binding Name}" Command="{Binding Command}" Style="{StaticResource TreeMenuButton}"/>
</StackPanel>
<HierarchicalDataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem},Path=IsExpanded}" Value="True">
<Setter TargetName="nodeImg" Property="Source" Value="{Binding ImageExpanded}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding IsDefaultConnection}" Value="True">
<Setter TargetName="nodeButton" Property="FontWeight" Value="Bold"></Setter>
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
Default style on button:
<Style x:Key="TreeMenuButton" TargetType="{x:Type Button}">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="MinHeight" Value="23" />
<Setter Property="MinWidth" Value="75" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Border"
CornerRadius="0"
BorderThickness="0"
Background="Transparent"
BorderBrush="Transparent">
<ContentPresenter Margin="2"
HorizontalAlignment="Left"
VerticalAlignment="Center"
RecognizesAccessKey="True"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I'd be surprised if your code even built as you can't add a DataTrigger into a UIElement.Triggers collection. Try using a Style instead (this definitely works):
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding IsDefault}" Value="True">
<Setter Property="TextBlock.FontWeight" Value="Bold" />
</DataTrigger>
</Style.Triggers>
</Style>
If by some miracle your DataTemplate does work in a UIElement.Triggers collection, then try using the class name as well as the property name:
<Setter Property="TextBlock.FontWeight" Value="Bold" />

Abstract border and text block into another style/template in WPF

First of all, I would like to add customized text blocks to my GUI with the least possible overhead. For instance: <TextBlock style={StaticRessources myTextBlock}>Text</TextBlock>
For now I have the following border style:
<Style x:Key="greenBox" TargetType="Border">
<Setter Property="Background" Value="#00FF00"/>
<Setter Property="CornerRadius" Value="10"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Height" Value="40"/>
<Setter Property="Width" Value="100"/>
</Style>
And I apply it in the following way:
<Border Style="{StaticResource greenBox}">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">Custom Text</TextBlock>
</Border>
My problem is, it needs 2 Tags and the properties set in the TextBlock will be redunant. I cannot figure out how to abstract both definitions into a single element.
that's where Label comes into play:
<Style TargetType="Label" x:Key="greenLabel">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<Border Style="{StaticResource greenBox}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Label Style="{StaticResource greenLabel}">Custom Text</Label>
(in accordance with your other question: if this is the only place you use that borderstyle you can of course include these directly in that border not using an extra style)
You would need to create a custom control as described here. Or you could create a UserControl as well.

Categories