How can I set up Style basedOn in behind code (WPF) ? Please help me ... My code is not work... Error ==> You can't modify after use 'SetterBaseCollection'. Here is part of my code.
public Style SettingStyle(Style sty, double fs)
{
Style baseStyle = new Style(sty.TargetType);
Style newStyle = Application.Current.TryFindResource(sty.TargetType) as Style;
// Style newStyle = new Style();
// newStyle.BasedOn = new Style(newStyle.TargetType, baseStyle);
newStyle.Setters.Add(new Setter(Control.FontSizeProperty, fs));
if (newStyle.Setters.Count > 1)
{
newStyle.Setters.Add(sty.Setters[1]);
newStyle.Setters.Add(sty.Setters[2]);
if (newStyle.Setters.Count > 3)
{
newStyle.Setters.Add(sty.Setters[3]);
}
}
return newStyle;
}
You can set a
baseStyleas property or by constructor:or
In your case probably
or
It depends on what exactly you want to achieve.