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>
Related
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.
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>
I have a toolbox which is a user control, which has a grid control inside it. Each grid cell is populated by another set of user controls representing each tool. Now each tool has a path with gradient fill representing an icon. The tool user control looks fine in the designer, but when I load the tool into the toolbox nothing shows up. I tried changing the background of the tool and it is reflected in the toolbox. So for some reason the path with the gradient will is not rendered when loaded within another control. Any ideas why?
toolbar
<usercontrol ...>
<local:SettingsButton/>
</usercontrol>
toolbox (SettingsButton)
<usercontrol ...>
<Grid x:Name="LayoutRoot" Background="AliceBlue">
<Path Data="M152.76824,152 ... />
<Path.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop.../>
</LinearGradientBrush>
</Path.Fill>
</Grid>
</usercontrol>
Put the control inside a viewbox or else the path wont re-size itself. This helped me.
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}"
I'm getting this warning in Visual Studio output window when binding on a SolidColorBrush property inside a DataTemplate:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=MyColor; DataItem=null; target element is 'SolidColorBrush' (HashCode=22943289); target property is 'Color' (type 'Color')
If I bind directly on the rectangle element, outside the DataTemplate, it all works well.
Can anyone explain why this difference in the two apparently similar usages from the sample code below:
My View:
<UserControl.Resources>
<vm:TestViewModel x:Key="_myTestVM"/>
<DataTemplate x:Key="testVMDataTemplate">
<Grid>
<Rectangle Height="30" Width="200" Margin="5">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=MyColor}" />
</Rectangle.Fill>
</Rectangle>
</Grid>
</DataTemplate>
</UserControl.Resources>
<Grid>
<StackPanel DataContext="{StaticResource _myTestVM}">
<!-- Binding *outside* the DataTemplate = works fine -->
<Rectangle Height="30" Width="200" Margin="5">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=MyColor}"/>
</Rectangle.Fill>
</Rectangle>
<!-- Binding *inside* the DataTemplate = output warning -->
<ContentControl Content="{Binding}" ContentTemplate="{StaticResource testVMDataTemplate}"/>
</StackPanel>
</Grid>
My ViewModel (TestViewModel):
public class TestViewModel {
private Color _color = Colors.Green;
public Color MyColor {
get { return _color; }
}
public TestViewModel() {
}
}
Update:
It evidently has to do with binding the Color property for the SolidColorBrush. The same thing is happening if I bind the Angle property on a RotateTransform object.
Thanks in advance.
Binding with default data source as DataContext wont work for SolidColorBrush type as they are not framework elements. Plus they are freezable and you are not allowed to change their colors dynamically through data context based color binding.
Either you will have to bind the color to the background fill via a converter that converts the color into a solid color brush.
<TextBlock Background="{Binding MyColor,
Converter={StaticResource ColorToBrushConverter}}" />
Or use Color as DynamicResource and refer that in Solid Color Brush.
ControlTemplate Storyboard color animation problem