【发布时间】:2018-08-16 02:35:00
【问题描述】:
这适用于 Windows 10 UWP。我需要允许用户更新与整个应用程序中使用的元素相关联的样式值(即允许用户更改各种文本块的字体大小、背景颜色堆栈面板等)。
我目前将所有样式保存在一个单独的文件中。
我的App.xaml如下:
<Application
x:Class="MyTestApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Styles/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
我的Styles.xaml(部分)如下:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:MyTestApp.Views"
xmlns:x1="using:System">
<Style x:Key="HeaderTextBlocks" TargetType="TextBlock" BasedOn="{StaticResource TitleTextBlockStyle}">
<Setter Property="FontSize" Value="24"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="TextWrapping" Value="NoWrap"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="Margin" Value="10,4,0,0"/>
</Style>
<Style x:Key="RegularTextBlocks" TargetType="TextBlock" BasedOn="{StaticResource TitleTextBlockStyle}">
<Setter Property="FontSize" Value="12"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="TextWrapping" Value="NoWrap"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="Margin" Value="0,0,0,0"/>
</Style>
</ResourceDictionary>
我在整个应用程序中使用这样的方式在控件上引用这些样式:
<TextBlock Style="{StaticResource HeaderTextBlocks}" />
我创建了一个设置页面 (settings.xaml),其中包含供用户更新各种样式设置的文本框。
但我不确定如何将这些绑定到styles.xaml 文件中各种样式的设置,以便在用户更改值时更新样式并更新引用样式的控件.
<TextBox Header="Font Size of Header TextBlocks" Text="{x:Bind HeaderTextBlocks.FontSize ???, Mode=TwoWay}" />
<TextBox Header="Font Size of Regular TextBlocks" Text="{x:Bind RegularTextBlocks.FontSize???, Mode=TwoWay}" />
有人可以指出我正确的方向吗?我试图用尽可能少的(或没有代码)来做到这一点。
【问题讨论】:
-
@TobiasTheel 感谢您的回复!但我看不到如何将 DynamicResource 与 Windows 10 UWP 一起使用:/
-
很遗憾,
DynamicResource标记扩展在 UWP 中不可用
标签: c# xaml uwp windows-10-universal