简体   繁体   English

在WPF中全局更改控件属性

[英]Change the control property globally in wpf

I have a back button which is copied almost to all the Controls in my application. 我有一个后退按钮,几乎将其复制到应用程序中的所有控件。

I have set the styles and properties of the button on each individual control (usercontrol) 我已经在每个单独的控件(用户控件)上设置了按钮的样式和属性

Now I want to change the text property of the button of all the control (usercontrol). 现在,我想更改所有控件(用户控件)的按钮的text属性。

I don't want to go and change the property of each control. 我不想去更改每个控件的属性。

Please help me setting a global property which sets the property in one place. 请帮助我设置一个将其设置在一个地方的全局属性。

Since the style is common to all pages. 由于样式是所有页面通用的。 Create the style without a key/name, just the target type would do. 创建没有键/名称的样式,只需目标类型即可。

<Style TargetType="{x:Type Button}">

Then do either of the following - 然后执行以下任一操作-

  • Add it to the App.XAML for visibility throughout the app 将其添加到App.XAML中以在整个应用程序中可见
  • Better approach would be to define a resource dictionary file and import it, wherever you need it. 更好的方法是定义资源字典文件并在需要时将其导入。
<Style TargetType="{x:Type Button}">
            <Setter Property="Text" Value="{Binding text}" />
            <Setter Property="...." Value="{Binding ....}"/>
        </Style>

Add this to App.xaml file as you want it to be global style for all your user controls. 希望将其添加到App.xaml文件中,以使其成为所有用户控件的全局样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM