gpt4 book ai didi

wpf - 从WPF中的数据库字段应用样式

转载 作者:行者123 更新时间:2023-12-02 04:15:06 25 4
gpt4 key购买 nike

我的问题是
我有存储在数据库表中的不同样式。
我将获取此样式并将其存储到一些字符串变量中。现在我要应用此样式
样式放入我的wpf控件中。那我该怎么办呢?

例如:

我的代码是..

Window1.xaml
===========================

<Window x:Class="DynamicBindResourceDictionary.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Button Margin="103,64,68,86" Name="button1" Content="Click Here!" Style="{DynamicResource baseStyle}"></Button>
</Grid>
</Window>

Window1.xaml.cs
================

public partial class Window1 : Window
{
//Store the style here from database
string style = "<Style x:Key='baseStyle' TargetType='{x:Type Button}'>" +
"<Setter Property='FontSize' Value='15' />" +
"<Setter Property='Background' Value='Red' /></Style>";

public Window1()
{
InitializeComponent();
/* How to do that? */
}
}

如果我们不能使用字符串变量,那是替代解决方案。

请帮助我

达摩什

最佳答案

尝试这个:

    public MainWindow()
{
string styleString = "<Style xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Key='baseStyle' TargetType='{x:Type Button}' >" +
"<Setter Property='FontSize' Value='15' />" +
"<Setter Property='Background' Value='Red' />"+
"<Setter Property='Height' Value='18' />" +
"</Style>";
StringReader stringReader = new StringReader(styleString);
XmlReader xmlReader = XmlReader.Create(stringReader);
Style readerLoadStyle = (Style)XamlReader.Load(xmlReader);
ResourceDictionary rd = new ResourceDictionary();
rd.Add("baseStyle", readerLoadStyle);
Application.Current.Resources.MergedDictionaries.Add(rd);
InitializeComponent();
}

关于wpf - 从WPF中的数据库字段应用样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3228025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com