Does anyone know how to create a wpf Style in code behind, I can't find anything on the web or MSDN docs. I have tried this but it is not working:
Style s = new Style(typeof(TextBlock));
s.RegisterName("Foreground", Brushes.Green);
s.RegisterName("Text", "Green");
breakInfoControl.dataTextBlock.Style = s;
You need to add setters to the style rather than using RegisterName. The following code, in the Window_Loaded event, will create a new TextBlock style which will become the default for all instances of a TextBlock within the Window. If you'd rather set it explicitly on one particular TextBlock, you can set the Style property of that control rather than adding the style to the Resources dictionary.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Style style = new Style(typeof (TextBlock));
style.Setters.Add(new Setter(TextBlock.ForegroundProperty, Brushes.Green));
style.Setters.Add(new Setter(TextBlock.TextProperty, "Green"));
Resources.Add(typeof (TextBlock), style);
}
This should get you what you need:
Style style = new Style
{
TargetType = typeof(Control)
};
style.Setters.Add(new Setter(Control.ForegroundProperty, Brushes.Green));
myControl.Style = style;
Related
I have a border generated dynamically.
I want to bind thickness property of it, with my style model.
Border db = new Border();
Binding borderthickness = new Binding();
borderthickness.Source = this.imodel.GridStyleProperties.BorderThickness;
borderthickness.Path = new PropertyPath("BorderThickness");
BindingOperations.SetBinding(db, Border.BorderThicknessProperty, borderthickness);
This is what I have done so far. Its not working for me.
Please suggest me what is wrong with this code.
Thank you
You are binding to a BorderThickness object, with Path set to BorderThickness. That means the binding will look in this.imodel.GridStyleProperties.BorderThickness.BorderThickness which doesn't exist.
Try
borderthickness.Source = this.imodel.GridStyleProperties;
borderthickness.Path = new PropertyPath("BorderThickness");
or just
borderthickness.Source = this.imodel.GridStyleProperties.BorderThickness;
I have following code to set the foreground property
TextBlock label = new TextBlock() { Style = LabelStyle };
Binding binding = new Binding(LabelColor) { Source = dataContextProperty };
label.SetBinding(TextBlock.ForegroundProperty, binding);
Note that LabelColor is a Brush.
If I set,
label.SetBinding(TextBlock.BackgroundProperty, binding)
It worked. So why it is not working only for foreground?
I've discovered what the problem was, It is nothing to do with the C# code itself, But it's in the XAML instead, The issue was the default colors that I've set in the XAML were overriding my style's colors.
So in conclusion, when you are setting any property by XAML it always overrides later styles set by C# code at runtime, this seems strange to me but at least that is how it worked for me.
The default Background colors in the XAML code avoided the C#'s style to apply on the panels (At-least avoided the new Background to be applied over the default ones).
I used your code and modified little bit for verification. Seems to be working fine. Have a look:
Style Style_Panel = new Style(typeof(Panel));
public void Init_Style()
{
// Create Styles :
#region "Create Styles"
Style_Panel.Setters.Add(new Setter()
{
Property = Panel.BackgroundProperty,
Value = new SolidColorBrush(Colors.Red)
});
Resources.Add(Style_Panel.TargetType, Style_Panel);
#endregion
// Apply Styles :
#region "Apply Styles"
List<Visual> List_Visual = new List<Visual>();
List_Visual.Add(new StackPanel() { Name = "btn" });
//Enum_Visual(Panel_Main, List_Visual);
foreach (Visual visual in List_Visual)
{
if (visual is Panel)
{
Panel panel = visual as Panel;
//if (Tagged(panel, "titlebar"))
//{
//}
//else if (Tagged(panel) == false)
{
// panel.Background = new SolidColorBrush( Colors.Red ); // <- WORKS .
panel.Style = Style_Panel; // <- DOES NOT WORKS !
}
}
}
#endregion
}
You haven't posted the creation of your style, maybe something is missing there?
There is another similar answer on StackOverflow which is a very good and short example of creating and setting a style in code:
Q: Does anyone know how to create a wpf Style in code behind, I can't find anything on the web or MSDN docs. I have tried this but it is not working:
A: You need to add setters to the style rather than using RegisterName. The following code, in the Window_Loaded event, will create a new TextBlock style which will become the default for all instances of a TextBlock within the Window. If you'd rather set it explicitly on one particular TextBlock, you can set the Style property of that control rather than adding the style to the Resources dictionary.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Style style = new Style(typeof (TextBlock));
style.Setters.Add(new Setter(TextBlock.ForegroundProperty, Brushes.Green));
style.Setters.Add(new Setter(TextBlock.TextProperty, "Green"));
Resources.Add(typeof (TextBlock), style);
}
I'm looking to change the font colour of specific nodes on a DevComponent AdvTree. I found the following information, relating to changing style, on the DevComponents 'Knowledge Base':
// Create new style
ElementStyle styleRightAligned = new ElementStyle();
// Set text alignment to right
styleRightAligned.TextAlignment = eStyleTextAlignment.Far;
advTree1.Styles.Add(styleRightAligned);
// Add new cell to the AdvTree
Cell cell = new Cell();
cell.Text = "Right";
cell.StyleNormal = styleRightAligned;
// Assign style to cell, same style can be assigned to any number of cells
node1.Cells.Add(cell);
I can't understand what object is being referred in eStyleAlignment.Far.
Does anyone have experience with changing styles within the DevComponents DotNetBar?
Thanks,
Andy
I figured out how to do this. The AdvTree control has a Styles property. This is a collection; styles can be added to it at design time.
Then the code to change the style of a particular node is:
void ChangeNodeStyle(AdvTree tree, int node, int style)
{
tree.Nodes[node].Style = tree.Styles[style];
}
Thanks,
Andy
I am having difficulties in adding Style property to a UserControl. There is a parser exception when I try to view the consumer page.
private Style _headerStyle = new Style();
public Style HeaderStyle
{
get { return _headerStyle ; }
set
{
_headerStyle .CopyFrom(value);
}
}
Usage:
Style="border: 1px solid blue;"
Error:
Cannot create an object of type 'System.Web.UI.WebControls.Style' from its string representation ...
A Style instance is not a string and vice-versa. Style.CopyFrom expects a Style as argument and you're passing a String. That's the reason why it cannot be copied to the new style object.
If you want to give your UserControl a border programmatically:
myControl.HeaderStyle.BorderStyle = BorderStyle.Solid;
myControl.HeaderStyle.BorderWidth = new Unit(1);
myControl.HeaderStyle.BorderColor = System.Drawing.Color.Blue;
You'r trying to create an ASP.NET Style but declaring a CSS Style, that's the problem. If you want to apply an ASP.NET style you need to call the ApplyStyle method instead:
control.ApplyStyle(styleName);