wpf 更改样式中的 setter 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20575775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:23:27  来源:igfitidea点击:

change setter value in style

c#wpfxamlstyles

提问by Babak.Abad

I'm programming in WPF(c#). I'm trying to change value in a setter of style.

我正在用 WPF(c#) 编程。我正在尝试以一种风格改变价值。

my style is:

我的风格是:

<Style TargetType="Control" x:Key="st">
    <Setter Property="FontFamily" Value="Tahoma"/>
    <Setter Property="FontSize" Value="14"/>
</Style>

and I use it in a button:

我在一个按钮中使用它:

<Button x:Name="btnCancel" Style="{StaticResource st}" Content="??????" Canvas.Left="30" Canvas.Top="18" Width="139" Height="53" FontFamily="2  badr" FlowDirection="LeftToRight" Click="btnCancel_Click_1" />

and what I try to do is this code:

我试图做的是这段代码:

Style style = new Style();
style = (Style) Resources["st"];
Setter setter =(Setter) style.Setters[1];
setter.Value = 30;

after setting font size to 30I get this error?

将字体大小设置为30我收到此错误后?

After a “SetterCollectionBase” is in use (sealed), it cannot be modified

“SetterCollectionBase”在使用(密封)后不能修改

How can I solve this problem?

我怎么解决这个问题?

采纳答案by 123 456 789 0

Since you are doing pure UI and behind the code while some answers recommend you to use MVVM which will really make a lot of things easier.

由于您正在做纯 UI 并在代码背后,而一些答案建议您使用 MVVM,这确实会让很多事情变得更容易。

Why do you need to manipulate the Style? Is it just for the button and you want to manipulate its FontSize? I assume you are doing this on the Click event of the button where it changes the fontsize.

为什么需要操纵 Style?是否仅用于按钮而您想操作其 FontSize?我假设您是在更改字体大小的按钮的 Click 事件上执行此操作。

Try this then

然后试试这个

 private void btnCancel_Click_1(object sender, RoutedEventArgs e)
    {
        var button = sender as Button;
        if (button != null) button.FontSize = 30;
    }

回答by seddik

The styles can be set only once (sealed after compiling), you can't change it with code

样式只能设置一次(编译后密封),不能用代码更改

so the solutions are

所以解决方案是

  1. create a style by code

        Style st = new Style(typeof(System.Windows.Controls.Control));
        st.Setters.Add(new Setter(Control.FontFamilyProperty, new FontFamily("Tahoma")));
        st.Setters.Add(new Setter(Control.FontSizeProperty, 14.0));
    
  1. 通过代码创建样式

        Style st = new Style(typeof(System.Windows.Controls.Control));
        st.Setters.Add(new Setter(Control.FontFamilyProperty, new FontFamily("Tahoma")));
        st.Setters.Add(new Setter(Control.FontSizeProperty, 14.0));
    

later you can change it

以后你可以改变它

        st.Setters.OfType<Setter>().FirstOrDefault(X => X.Property == Control.FontSizeProperty).Value = 30.0;//safer than Setters[1]

or

或者

  1. change the property directly

    btnCancel.FontSize=30.0;
    
  1. 直接修改属性

    btnCancel.FontSize=30.0;
    

回答by Mark Feldman

You'll need to create a view model, something like this (I'm using the MVVM Lite class ViewModelBase, you just need something that supports property change notification):

你需要创建一个视图模型,像这样(我使用的是 MVVM Lite 类 ViewModelBase,你只需要支持属性更改通知的东西):

public class MyViewModel : ViewModelBase
{
    private double _FontSize = 0.0;
    public double FontSize
    {
        get { return this._FontSize; }
        set { this._FontSize = value; RaisePropertyChanged(() => this.FontSize); }
    }
}

Then create an instance of it in your window along with a getter:

然后在您的窗口中创建它的一个实例以及一个 getter:

public partial class Window1 : Window
{
    public MyViewModel MyViewModel {get; set;}
    public Window1()
    {
        InitializeComponent();
        this.MyViewModel = new MyViewModel { FontSize = 80 };
    }
}

And then finally you need to bind your style to use the value in the view model:

最后,您需要绑定您的样式以使用视图模型中的值:

<Window.Resources>
    <Style TargetType="Control" x:Key="st">
        <Setter Property="FontFamily" Value="Tahoma"/>
        <Setter Property="FontSize" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}, Path=MyViewModel.FontSize}"/>
    </Style>
</Window.Resources>