如何让用户在运行时编辑控件样式?

发布于 2024-10-20 23:30:44 字数 413 浏览 4 评论 0原文

可以使用什么方法让用户通过在某些自定义控件样式中设置个人值来定义自己的应用程序首选项?

我们可以在设计时在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箜明 2024-10-27 23:30:44

您需要将样式中的值绑定到某个静态类(例如应用程序的默认设置),该静态类可以由定义值的任何类进行修改。

在下面的应用程序中,我在 Settings.settings 文件中创建了一个名为 FontSize 的属性。我在 XAML 文件中添加了适当的命名空间,现在可以根据需要绑定到它:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:WpfApplication1"
        xmlns:prop="clr-namespace:WpfApplication1.Properties"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Grid.Resources>
            <Style TargetType="TextBlock" x:Key="myStyle">
                <Setter Property="FontSize" Value="{Binding FontSize, Source={x:Static prop:Settings.Default}}" />
            </Style>
        </Grid.Resources>

        <TextBlock Style="{DynamicResource myStyle}" Text="The quick brown fox jumped over the lazy dog." />

        <TextBox Grid.Row="1" Text="{Binding FontSize, Source={x:Static prop:Settings.Default}, UpdateSourceTrigger=PropertyChanged}" />
    </Grid>
</Window>

我将值直接绑定到 TextBox 但不言而喻,某些控制机制(例如在视图模型中)强烈推荐。

最后,如果您想保存设置,您只需调用类的 Save 方法即可,例如,在应用程序的 Exit 事件的事件处理程序中:

private void Application_Exit(object sender, ExitEventArgs e)
{
    WpfApplication1.Properties.Settings.Default.Save();
}

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 the Settings.settings file. I added the appropriate namespace in the XAML file and can now bind to it as I like:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:WpfApplication1"
        xmlns:prop="clr-namespace:WpfApplication1.Properties"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Grid.Resources>
            <Style TargetType="TextBlock" x:Key="myStyle">
                <Setter Property="FontSize" Value="{Binding FontSize, Source={x:Static prop:Settings.Default}}" />
            </Style>
        </Grid.Resources>

        <TextBlock Style="{DynamicResource myStyle}" Text="The quick brown fox jumped over the lazy dog." />

        <TextBox Grid.Row="1" Text="{Binding FontSize, Source={x:Static prop:Settings.Default}, UpdateSourceTrigger=PropertyChanged}" />
    </Grid>
</Window>

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's Exit event:

private void Application_Exit(object sender, ExitEventArgs e)
{
    WpfApplication1.Properties.Settings.Default.Save();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文