I want to change the background color from my combobox.
But I would like to retain the color gradients.
I've tried using this code but still does not get the effect.
<Setter Property="Background" Value="White"/> <!-- It's only white :( -->
<ComboBox>
<ComboBox.Background>
<LinearGradientBrush EndPoint="0,1">
<GradientStopCollection>
<GradientStop Color="Blue" Offset="0.5" />
<GradientStop Color="White" Offset="0.5" />
</GradientStopCollection>
</LinearGradientBrush>
</ComboBox.Background>
</ComboBox>
This will change the background color. Change the Color and Offset to get your desired result.
Related
I would like to specify a resource as a default value for my control for the BorderBrushProperty. The reason being the brush is a LinearGradientBrush which I defined in XAML. So I'm looking for something like this in my static CTOR (3rd line):
static Gauge()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof (Gauge), new FrameworkPropertyMetadata(typeof (Gauge)));
BorderThicknessProperty.OverrideMetadata(typeof (Gauge), new FrameworkPropertyMetadata(new Thickness(16)));
BorderBrushProperty.OverrideMetadata(typeof (Gauge), new FrameworkPropertyMetadata("OuterFrameStroke"));
}
This is what my XAML looks like (in themes\generic.xaml):
<LinearGradientBrush x:Key="OuterFrameStroke" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF636060" Offset="1" />
<GradientStop Color="#FF5F5C5C" Offset="0" />
<GradientStop Color="#FFEEDEDE" Offset="0.35" />
<GradientStop Color="#FFA09595" Offset="0.705" />
</LinearGradientBrush>
Well, should have thought 2 seconds longer. Of course I can set this default property in my template
<Style TargetType="gauge:Gauge">
<Setter Property="BorderBrush">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFA3AFD6" Offset="0.321" />
<GradientStop Color="#FF8399A9" Offset="0.674" />
<GradientStop Color="#FF718597" Offset="0.375" />
<GradientStop Color="#FF617584" Offset="1" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
I have a ComboBox with a custom theme I wrote, and I get the error message "Cannot animate (0).(1) on an immutable object." This specifically happens when I set its selectedindex after the user selects one of the options in the combobox.
Doing some research online, I found that this is a common issue with databound items or dynamic resources. I'm not using any databound resources, but what I think is happening is since the combobox is collapsed, it tries to set the state of a button that doesn't exist. I narrowed it down to this code:
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource spPressedStateBrush}" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource spOverStateBrush}" />
</Trigger>
</ControlTemplate.Triggers>
Which depends on these Dynamic Resources:
<LinearGradientBrush x:Key="spOverStateBrush" StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#2C8CBF" Offset="0" />
<GradientStop Color="#2793BF" Offset="0.5" />
<GradientStop Color="#2483BF" Offset="0.5001" />
<GradientStop Color="#2C8CBF" Offset="1" />
</LinearGradientBrush>
<LinearGradientBrush x:Key="spPressedStateBrush" StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#0C6C9F" Offset="0" />
<GradientStop Color="#07739F" Offset="0.5" />
<GradientStop Color="#04639F" Offset="0.5001" />
<GradientStop Color="#0C6C9F" Offset="1" />
</LinearGradientBrush>
So I'm pretty sure those dynamic resources are the culprit, but how would I solve this problem?
After painstakingly trying to debug it by comparing the code to the original Control Template, I figured out that transporting all of the resources that my control uses into the file itself, and replacing all DynamicResources with StaticResources and that fixed the bug I was having.
Be sure that both this two (spOverStateBrush or spPressedStateBrush) Brushes are not used in different place in your code.
In order to be animatable, the Background Brush (spOverStateBrush or spPressedStateBrush) of the Border must be mutable, which the default value isn't.
If you use one of these two Brushes in another place, You should assign a new LinearGradientBrush before animating something like this code:
LinearGradientBrush gradient = new LinearGradientBrush();
gradient.StartPoint = new Point( 0.5, 0 );
gradient.EndPoint = new Point( 0.5, 1 );
gradient.GradientStops.Add(new GradientStop(Colors.Black, 0));
gradient.GradientStops.Add(new GradientStop(Color.FromArgb(100,69,87,186), 1));
Border.Background = gradient;
How can I create a WPF control (similar to a TextBlock) so that any overflowing text is faded out to transparent rather than simply clipping or wrapping?
I need to keep my control fixed-width, so expanding the width of the control to fit the text is not an option. I also do not want to make the text font smaller.
Not quite sure exactly what you are trying to achieve, but you could do something like this:
<TextBlock Text="Some long text here that should fade out">
<TextBlock.Foreground>
<LinearGradientBrush>
<GradientStop Offset="0" Color="Black"/>
<GradientStop Offset="0.7" Color="Black"/>
<GradientStop Offset="1" Color="Transparent"/>
</LinearGradientBrush>
</TextBlock.Foreground>
</TextBlock>
But your control still needs to be wide enough to accommodate all the text for it to display.
<TextBlock Text="fgdfgfdgfddgfdgdfgfdgfdgd" Width="129" TextWrapping="NoWrap">
<TextBlock.Foreground>
<LinearGradientBrush EndPoint="0.661,0.399" StartPoint="0.008,0.496">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#7F000000" Offset="0.803"/>
<GradientStop Color="#4C0A0909" Offset="0.95"/>
<GradientStop Color="#BF000000" Offset="0.729"/>
<GradientStop Color="#F8000000" Offset="0.699"/>
</LinearGradientBrush>
</TextBlock.Foreground>
</TextBlock>
The trick on the gradient is that even though all colors are all based off black, The fade is achieved through opacity of each gradient by playing with A part of RGBA, in pseudo-code:
GradientStop Color="Black" A=100%
GradientStop Color="Black" A=97% Offset="0.803"
GradientStop Color="Black" A=75% Offset="0.95"
GradientStop Color="Black" A=80% Offset="0.729"
GradientStop Color="Black" A=30% Offset="0.699"
Thanks guys, but I found the answer I needed on MSDN.
I am new in Silverlight. I need to fill a rectangle partially with a SolidColorBrush (like a Bar, I need the border). With GradientBrush, the change of color is not sharp.
I can add two rectangles for this, but there has to be a better way.
Any suggestion would be appreciated.
I am using Silverlight 4.0.
Thanks for reading.
-Rakib
What about using gradient brush with gradient stops very close to each other, something like this:
<LinearGradientBrush x:Key="_backgroundBrush" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="Yellow" Offset="0" />
<GradientStop Color="Yellow" Offset="0.65" />
<GradientStop Color="Blue" Offset="0.66" />
<GradientStop Color="Blue" Offset="1" />
</LinearGradientBrush>
I want to apply some gradient to my image how can I achieve that?
Thanx
You could host your image within a Border, applying the gradient to the border background:
<Border>
<Border.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="Red" Offset="0" />
<GradientStop Color="Blue" Offset="1" />
</LinearGradientBrush>
</StackPanel.Background>
<Image Source=..your image source .." />
</Border>
This assumes that your image has some opaque regions which will show the gradient beneath it.
In the gradient you can use one or more colours. Any it is very easy to set the gradient using either VS2010 or expression blend. The following is the sample code of a two-colour gradient with blue and white.
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform> <CompositeTransform CenterY="0.5" CenterX="0.5" Rotation="-90"/>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FFB6D3F9"/>
<GradientStop Color="#FFFDFDFD" Offset="1"/>
</LinearGradientBrush>