Why could not I call this brush here? - wpf

I created a linearGradientBrush inside "Application.Resources" and tried to call it with one of the style setters. The compiler says the name could not be resolved. Could not see why....
<Style x:Key="GridTextBoxStyle" TargetType="TextBox">
<Setter Property="Background" Value="{StaticResource TextBlinearBrush}"/>
</Style>
<LinearGradientBrush x:Key="TextBlinearBrush" EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="Gray" Offset="1" />
<GradientStop Color="DarkGray" Offset="0.541" />
</LinearGradientBrush>

You'll need to define the brush before it is used. Just move the LinearGradientBrush above the Style where it is referenced and that should do the trick.

Related

Wpf - Using Shapes as a Resource

Iv'e got a Rectangle in my resource dictionary which i would like to place inside numerous Grid objects
<Rectangle HorizontalAlignment="Left" Width="10" x:Key="ShadowRect">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="1.7,0.603" StartPoint="0.3,0.603">
<GradientStop Color="White" Offset="1"/>
<GradientStop Color="Black" Offset="0.009"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
Now of course i could place it directly like so :
<Grid>
<Rectangle HorizontalAlignment="Left" Width="10" >
<Rectangle.Fill>
<LinearGradientBrush EndPoint="1.7,0.603" StartPoint="0.3,0.603">
<GradientStop Color="White" Offset="1"/>
<GradientStop Color="Black" Offset="0.009"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
but i would like to use it as a Resource so i do not have to write this XAML for every Grid ,
how can i place the rectangle using it's resource key ?
You could have a Rectangle Style instead of a Rectangle in your ResourceDictionary:
<Style x:Key="ShadowRectStyle" TargetType="Rectangle">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Width" Value="10"/>
<Setter Property="Fill">
<Setter.Value>
<LinearGradientBrush EndPoint="1.7,0.603" StartPoint="0.3,0.603">
<GradientStop Color="White" Offset="1"/>
<GradientStop Color="Black" Offset="0.009"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
Then use it like this:
<Grid>
<Rectangle Style="{StaticResource ShadowRectStyle}"/>
</Grid>

Override Selected Item in Combobox's style to show comboxbox's background not gray background

I am having an issue with getting a custom property set with a combobox. I am using .Net 4.0, WPF with xaml resources for types set in a dictionary and some brushes set in the app.xaml. I can get the nice rounded corner just find and a gradient and layout to my combobox using the surrounding 'border' trick. However I can't seem to get the 'Selected Item' in the combobox to have a background other than dull gray. I would prefer to change it to transparent and inherit the gradient of the parent border. However I am missing the property or relationship to do this.
Does anyone know how to do this in the xaml?
Image:
code:
Dictionary item:
<Style TargetType="{x:Type Border}">
<Setter Property="Background" Value="{StaticResource MoneyBrush}" />
<Setter Property="BorderBrush" Value="#071C07" />
<Setter Property="BorderThickness" Value="3" />
<Setter Property="CornerRadius" Value="20" />
<Setter Property="SnapsToDevicePixels" Value="True" />
</Style>
Brush in main App.xaml:
<LinearGradientBrush x:Key="MoneyBrush" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#3A883A" Offset="1" />
<GradientStop Color="#FFFFFF" Offset="0" />
<GradientStop Color="#FF53AA75" Offset="0.50" />
<GradientStop Color="#073307" Offset="0.95" />
</LinearGradientBrush>
<LinearGradientBrush x:Key="FontBrush" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="#107810" Offset="0.50" />
<GradientStop Color="Black" Offset="0.65" />
</LinearGradientBrush>
Actual item in mainwindow:
<Border Margin="5" >
<ComboBox Height="30" Width="170" Margin="10" x:Name="combopersons"
FontSize="20"
ItemsSource="{Binding Path=People}"
DisplayMemberPath="FirstName"
SelectedValuePath="PersonId"
SelectedValue="{Binding Path=CurrentUser}"
Foreground="{StaticResource FontBrush}">
</ComboBox>
</Border>
EDIT >>>
I like the solution proposed by #iltzortz, but I wanted a gradient so in this case this would work better:
<ComboBox.Resources>
<LinearGradientBrush x:Key="{x:Static SystemColors.WindowBrushKey}" >
<GradientStop Color="#3A883A" Offset="1" />
<GradientStop Color="#FFFFFF" Offset="0" />
<GradientStop Color="#FF53AA75" Offset="0.50" />
<GradientStop Color="#073307" Offset="0.95" />
</LinearGradientBrush>
<LinearGradientBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" >
<GradientStop Color="#000000" Offset="1" />
<GradientStop Color="#FFFFFF" Offset="0" />
</LinearGradientBrush>
</ComboBox.Resources>
EDIT 2 >>>
For some reason this will only work on applications I have built using .NET 4.0 or higher on Visual Studio 2012 with Windows 7. When I try to run the code at home for some reason
it won't render I believe this is due to either Windows 8 or Visual Studio 2010 interpreting different colors for different system values. Be aware of this if you have Windows 8 or Visual Studio 2010 as it will work for me for one environment but not the other... curious.
The following code seems to do the job
<Grid Background="Pink">
<ComboBox Margin="10,0" Width="100" Height="40">
<ComboBoxItem>1</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
<ComboBoxItem>3</ComboBoxItem>
<ComboBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="Green" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green" />
</ComboBox.Resources>
</ComboBox>
<TextBox VerticalAlignment="Top" Margin="10"/>
</Grid>

WPF set border background in trigger

I need to create a trigger, that will change Border background property, when MouseEnter occurred. I did the follow:
<Border Width="20" Height="30" Focusable="True">
<Border.Background>
<LinearGradientBrush>
<LinearGradientBrush.GradientStops>
<GradientStop Color="Aquamarine" Offset="0"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Border.Background>
<Border.Style>
<Style TargetType="{x:Type Border}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush>
<LinearGradientBrush.GradientStops>
<GradientStop Color="Aquamarine" Offset="0"/>
<GradientStop Color="Beige" Offset="0.2"/>
<GradientStop Color="Firebrick" Offset="0.5"/>
<GradientStop Color="DarkMagenta" Offset="0.9"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
but it doesn't work. Thanks.
Common mistake. You have set the Border.Background property directly which will always override the value set by your trigger. (Locally set values have a very high precedence, style has a pretty low precedence.)
Instead, you should move your "normal" background into the Style like so:
<Border>
<Border.Style>
<Style TargetType="Border">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush>
<LinearGradientBrush.GradientStops>
<GradientStop Color="Aquamarine" Offset="0"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Style.Triggers>
<!-- the trigger you showed -->
</Style.Triggers>
</Style>
</Border.Style>
</Border>

Using Styles in Windows Presentation Foundation

Imagine I have a data bound ListView and in the <ControlTemplate.Triggers>
I have the following
<DataTrigger Binding="{Binding Path=Status}" Value="Completed">
<Setter Property="Background" Value="{StaticResource CompletedBackground}" />
<Setter Property="Foreground" Value="Black" />
</DataTrigger>
I want that to be bound to a Style i have in my Grid.Resources which looks like the following:
<Style x:Key="CompletedBackground" TargetType="ListViewItem">
<Setter>
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFBCFAA6" Offset="0"/>
<GradientStop Color="#FFA3E88B" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
However, as you might imagine this doesn't work, suprise suprise, you can't bind "Setter" to "Background", so my question is, how do I actually solve the problem?
I've looked through the following a lot of times, cant find any information here.
What you're trying to do is fundamentally flawed. For starters, your style's setter doesn't specify a target property. Presumably, the target property should be Background:
<Style x:Key="CompletedBackground" TargetType="ListViewItem">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFBCFAA6" Offset="0"/>
<GradientStop Color="#FFA3E88B" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
Secondly, you're then trying to assign a Style instance to the Background property, which is of type Brush, not Style.
Depending on exactly what you're trying to achieve, you should be able to just change the Style to a Brush resource:
<LinearGradientBrush x:Key="CompletedBackground" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFBCFAA6" Offset="0"/>
<GradientStop Color="#FFA3E88B" Offset="1"/>
</LinearGradientBrush>
Then use it from your trigger in the same fashion you already are.

Vista style window Close button in XP

How can I create a a custom button that looks and feels like Vista close button? Can any one have a template?
This template will helps to make a Vista style Window close button
<Button>
<Button.Template>
<ControlTemplate TargetType="Button">
<Rectangle Width='60' Height='40' x:Name='MyRectangle'>
<Rectangle.Fill>
<LinearGradientBrush x:Key="RedButtonBackground" StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0" Color="#F89C8C" />
<GradientStop Offset="0.45" Color="#D47F75" />
<GradientStop Offset="0.45" Color="#C04C3C" />
<GradientStop Offset="1" Color="#C98172" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName='MyRectangle' Property="Fill" >
<Setter.Value>
<LinearGradientBrush x:Key="RedButtonMouseOverBackground" StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0" Color="#F89C8C" />
<GradientStop Offset="0.45" Color="#E36A53" />
<GradientStop Offset="0.45" Color="#C72B0E" />
<GradientStop Offset="0.75" Color="#D44310" />
<GradientStop Offset="1" Color="#F5E478" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>

Resources