如何让用户在运行时编辑控件样式?
可以使用什么方法让用户通过在某些自定义控件样式中设置个人值来定义自己的应用程序首选项?
我们可以在设计时在 XAML 中设置它们:
<UserControl.Resources>
<Style TargetType="{x:Type cc:MyControl}">
<Setter Property="SWidth" Value="20" />
...
<Setter Property="SBrush" Value="Blue" />
</Style>
</UserControl.Resources>
但是如何在运行时编辑这些样式值呢?
What approach can be used for enabling users to define their own application preferences by setting personal values in some custom controls styles?
We can set them in XAML in design-time:
<UserControl.Resources>
<Style TargetType="{x:Type cc:MyControl}">
<Setter Property="SWidth" Value="20" />
...
<Setter Property="SBrush" Value="Blue" />
</Style>
</UserControl.Resources>
But how to edit these style values in runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将样式中的值绑定到某个静态类(例如应用程序的默认设置),该静态类可以由定义值的任何类进行修改。
在下面的应用程序中,我在
Settings.settings
文件中创建了一个名为FontSize
的属性。我在 XAML 文件中添加了适当的命名空间,现在可以根据需要绑定到它:我将值直接绑定到
TextBox
但不言而喻,某些控制机制(例如在视图模型中)强烈推荐。最后,如果您想保存设置,您只需调用类的
Save
方法即可,例如,在应用程序的Exit
事件的事件处理程序中:You'll want to bind the values in your style to some static class—for example, the application's default settings—that can be modified by whatever class defines what the values should be.
In the app below, I created a property called
FontSize
in theSettings.settings
file. I added the appropriate namespace in the XAML file and can now bind to it as I like:I bound the value directly to a
TextBox
but it goes without saying that some control mechanism, in a viewmodel for instance, is strongly recommended.Finally, if you want to save the settings, all you have to do is call the class's
Save
method, for example, in the event handler of the application'sExit
event: