【问题标题】:Get all elements that have texts获取所有包含文本的元素
【发布时间】:2017-03-14 17:58:59
【问题描述】:

我有一个应用程序,它已经使用样式模板来定义字体系列、字体大小、前景颜色等。

该应用程序的一个功能是使用户能够选择特定颜色,然后应用于所有文本,包括文本块、列表视图、按钮等。

我已经阅读了这个链接 (Find all controls in WPF Window by type),在那里我可以找到所有对象的 FrameworkElement 类型,然后根据他的类型将颜色应用于每个元素。但是,我不确定这是最好的方法。

【问题讨论】:

  • 对于所有必须能够更改颜色的元素,将 color 属性绑定到一个可以更改的中心颜色。

标签: c# .net wpf


【解决方案1】:

正如你所说,有很多方法可以做到这一点。 首先,您可以拥有一个样式模板,然后使用 StaticResource 为其获取样式。例如:在你看来 mainwindow.xaml

<Window.Resources>
    <Style TargetType="{x:Type Control}" x:Key="baseStyle">
        <Setter Property="FontSize" Value="100" />
    </Style>
    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource baseStyle}"></Style>
    <Style TargetType="{x:Type Label}" BasedOn="{StaticResource baseStyle}"></Style>
    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource baseStyle}"></Style>
    <Style TargetType="{x:Type ListView}" BasedOn="{StaticResource baseStyle}"></Style>
    <!-- ComboBox, RadioButton, CheckBox, etc... -->
</Window.Resources>

OR 另一种方法是: 查看:mainwindow.xaml

<Window>
    <Window.resources>
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="FontSize"
                Value="14" />
    </Style>
    </Window.resource>

    <Grid>
       <TextBox Text="blah"/>
    </Grid>
</Window>

你可以从主窗口构造函数中做到这一点

Style = (Style)FindResource(typeof(Window));

这个,你将把它放在 app.xaml 中

<Style TargetType="{x:Type Window}">
    <Setter Property="FontSize"
            Value="14" />
</Style>

【讨论】:

    猜你喜欢
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    相关资源
    最近更新 更多