I have a Brush defined in a code file and I am able to reference it using the DynamicResource extention in XAML at runtime. What I would like to do is to grab the Brush.Color and bind it to an element.
I've tried the approach bellow,
<SolidColorBrush Color="{DynamicResource ButtonHoverTopBrush.Color}" Opacity="0" />
but it doesn't work. How grab that Color?
Try this:
<SolidColorBrush Color="{Binding Color, Source={StaticResource ButtonHoverTopBrush}}"
Opacity="0" />
It doesn't work with DynamicResource instead of StaticResource but if you change the Color of ButtonHoverTopBrush dynamically, it will affect the above brush. You cannot replace the Brush itself though.
Related
I have a local resource that I want to use on a control. I'd like to define the resource directly on the control since that's the only place it's going to be used.
<Button Background="{StaticResource MyBrush}">
<Button.Resources>
<SolidColorBrush x:Key="MyBrush" Color="#FFD71526"/>
</Button.Resources>
</Button>
This doesn't work MyBrush hasn't been defined at the time Background is set. It would work as a DynamicResource, but I want to keep it static.
Question: How can I set a property on a control using a resource that is defined directly on the control?
I tried setting the property after creating the resource, but if it's possible I can't seem to find the correct syntax.
<Button>
<Button.Resources>
<SolidColorBrush x:Key="MyBrush" Color="#FFD71526"/>
</Button.Resources>
<Button.Background>
{StaticResource MyBrush}
</Button.Background>
</Button>
The correct syntax is the following one:
<Button>
<Button.Resources>
<SolidColorBrush x:Key="MyBrush" Color="#FFD71526"/>
</Button.Resources>
<Button.Background>
<StaticResource ResourceKey="MyBrush" />
</Button.Background>
</Button>
if you do not want to use the color anywhere else there is no point in making a static ressource in the first place!
If it is one time use just put it in the property directly like this:
<Button Background="#FFD71526">
</Button>
Defining a color as a static ressource only makes sense on a parent with multible childs that plan to reuse that property
Edit:
You can use this for your purpose, set the backround once and bind all other properties (for example BorderBrush) to the background like so:
<Button
Background="#FFD71526"
BorderBrush="{Binding RelativeSource={RelativeSource Self}, Path=Background}"
BorderThickness="5">
</Button>
I have a MetroAnimatedSingleRowTabControl from MahApps Metro. I want to change the header's foregorund color from blue (defined by the template) to white.
This is what I have got:
<Grid x:Name="Body" Grid.Row="1" Margin="20,0,20,0">
<controls:MetroAnimatedSingleRowTabControl>
<controls:MetroAnimatedSingleRowTabControl>
<controls:MetroTabItem Header="Server"/>
</controls:MetroAnimatedSingleRowTabControl>
</controls:MetroAnimatedSingleRowTabControl>
</Grid>
I want to let the Server Header to appear in white.
The colors are set in the triggers towards the end of the TabItem template.
Once you got the Keys you can overwrite the resource bindings
<Grid x:Name="Body" Grid.Row="1" Margin="20,0,20,0">
<Controls:MetroAnimatedSingleRowTabControl>
<Controls:MetroAnimatedSingleRowTabControl.Resources>
<SolidColorBrush x:Key="AccentColorBrush" Color="Red"/>
<SolidColorBrush x:Key="HighlightBrush" Color="Orange"/>
</Controls:MetroAnimatedSingleRowTabControl.Resources>
<Controls:MetroTabItem Header="Server"/>
</Controls:MetroAnimatedSingleRowTabControl>
</Grid>
C# code:
A class Theme with a member Color themeColor And
MainWindow has a member Theme winTheme.
XAML:
A Grid.
I want to bind the winTheme.themeColor to the Grid's Background.
So that when the variable changes, Grid's background changes automatically...
How can I do that?
<Grid>
<Grid.Background>
<SolidColorBrush Color="{Binding Color}"/>
</Grid.Background>
</Grid>
I have a TextBox control and I would like to be able to set a background image and a background color.
Currently I can set one, or the other but not both. When I try to set both simultaneously I receive a "The property 'Background' is set more than once" error.
Here is the code I used:
<TextBox Name="tbImageTextBox">
<TextBox.Background>
<ImageBrush ImageSource="/Resources/Images/image.png"
AlignmentX="Right" Stretch="None"/>
<SolidColorBrush>#FF8D8A8A</SolidColorBrush>
</TextBox.Background>
</TextBox>
I have also attempted to set the background color in the style for the TextBox and the image in the <TextBox.Background>, but the color is ignored.
Use the grid resource for background as needed. Same resource can be used for multiple textboxes.
<Grid>
<Grid.Resources>
<ImageBrush x:Key="img" ImageSource="Blue hills.jpg"></ImageBrush>
<SolidColorBrush x:Key="brownBrush" Color="Brown"></SolidColorBrush>
</Grid.Resources>
<TextBox x:Name="test" Background="{StaticResource img}" Width="100" Height="40" />
</Grid>
I ended up putting the TextBox into a grid with the Background color set and applying the background image to the TextBox itself as using VisualBrush and DrawingBrush stretched my image or only applied the background color to the image - not the rest of the TextBox.
You will need to combine the colour and the image in a single Brush instance, you could use a DrawingBrush or a VisualBrush containing an Image control with your image and the Background set to the colour for example.
You probably want either a VisualBrush or a DrawingBrush. More information on those can be found at MSDN here. Something like this might get you started:
<Rectangle Width="75" Height="75">
<Rectangle.Fill>
<VisualBrush TileMode="Tile">
<VisualBrush.Visual>
<Grid>
<Image BaseUri="somepic.png" />
<Rectangle Brush="FF8D8A8A" />
</Grid>
</VisualBrush.Visual>
</VisualBrush>
</Rectangle.Fill>
</Rectangle>
I have a definition for a data template that looks as follows:
<DataTemplate DataType="{x:Type HeatMap:BlockItem}">
<Grid Visibility="{Binding IsVisible}">
<Border Name="BlockBorder" Width="{Binding Width}" Height="{Binding Height}">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{Binding Colour}" Offset="1"/>
<GradientStop Color="White"/>
</LinearGradientBrush>
</Border.Background>
</Border>
</Grid>
</DataTemplate>
As can be seen, BlockItem has a property of type Color called Colour which is bound to the first color of the LinearGradientBrush which fills a border, making it look like a filled rectangle.
Now I don't always want Linear Gradient Brushes to style the fill of this rectangle. Some rectangles on my canvas may need to be filled with SolidBrushes, for example. I considered creating a Brush property on the BlockItem class instead of a Color property and binding the entire Border.Background to that, but there are 2 problems with this:
I don't know how the XAML should look to specify a binding to the entire object Background property.
In the code where I create BlockItems, if I instantiate a new Brush for every single BlockItem (bear in mind, there maybe be many drawn on a canvas at a time), will this not make it really inefficient and slow?
1) You can bind the Background directly to a brush on your BlockItem:
<Border Name="BlockBorder" Background="{Binding MyBackgroundBrush}">
2) You could bind to a static resource, or create a static brush for your BlockItem.
Background="{StaticResource myStaticBrush}"