设计多种类型的 WPF 控件 [英] Styling multiple types of WPF controls

查看:31
本文介绍了设计多种类型的 WPF 控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些(不同)项目的堆栈面板:

I have a stack panel with a few (different) items:

<StackPanel ...>
    <TextBlock ... />
    <CheckBox ... />
    <CheckBox ... />
    <Button ... />
</StackPanel>

我想将 VerticalAlignment="Center" 应用于 StackPanel 的所有子项而无需添加 VerticalAlignent="Center"Style=... 给每个孩子.所以,我想我想定义一个适用于 TextBlock、CheckBoxes 和 Button 的隐式样式.

I would like to apply VerticalAlignment="Center" to all children of the StackPanel without having to add VerticalAlignent="Center" or Style=... to every single child. So, I guess I want to define an implicit style which applies to the TextBlock, the CheckBoxes and the Button.

我尝试在堆栈面板的资源中添加一个 <Style TargetType="FrameworkElement">,但是(显然)这不起作用,因为 TargetType 创建了一个隐式的 x:Key{x:Type FrameworkElement},而 TextBlock 仅自动绑定到具有 {x:Type TextBlock} 的 x:Key 的样式.

I tried adding a <Style TargetType="FrameworkElement"> in the stack panel's resources, but (obviously) this doesn't work, since the TargetType creates an implicit x:Key of {x:Type FrameworkElement}, whereas TextBlock only automatically binds to Styles with an x:Key of {x:Type TextBlock}.

所以,据我所知,我唯一的选择是:(1)为堆栈面板资源中的所有三种类型创建三种样式,(2)创建一种样式并手动将其绑定到所有子项,(3) 手动设置所有子项的 VerticalAlignment 选项.我想要的是:(4) 创建一个样式并自动将其绑定到堆栈面板的所有子项.那可能吗?或者是否有其他解决方案比 (1)-(3) 更不冗余?

So, as far as I can see, the only options I have are: (1) create three Styles for all three types in the stack panel's resources, (2) create one style and manually bind it to all children, (3) manually set the VerticalAlignment option at all children. What I want is: (4) Create one Style and automatically bind it to all children of the stack panel. Is that possible? Or is there some other solution that is less redundant than (1)-(3)?

推荐答案

Heinzi,

如果我正确理解您的问题,您似乎有一个 StackPanel 想要包含一组子项.这些孩子你想要有一个样式 VerticalAlignment="Center" 但你不想把这个属性添加到每个孩子.如果是这种情况,我有一个解决方案:

If I'm understanding your question correctly, it seems like you have a StackPanel that you want to contain a set of children. These children you want to have a style VerticalAlignment="Center" but you don't want to add this property to each child. If this is the case, I have a solution for you:

然而,没有办法设置基对象的属性并让它们在继承的类中使用.因此,FrameworkElement 有一个属性 VerticalAlignment,但您不能直接在样式中设置它并自动应用它.在我提出的解决方案中,您必须为每种对象类型创建一个样式,但您不必为每个对象类型创建不同的样式.在我的解决方案中,您可以创建一个BaseStyle",然后将其他每个样式都基于此样式以获得所需的结果.所以你可以这样做:

There is, however, no way to set a base object's properties and have them used in the inherited class. So, a FrameworkElement has a property VerticalAlignment, but you can't set that directly in a style and have it applied automatically. In my proposed solution, you do have to create a style for each object type, but you don't have to create a distinct style for each. In my solution, you can create a "BaseStyle" then base each of your other styles on this one to get the desired result. So you can do this:

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="500">
    <Window.Resources>
        <ResourceDictionary>
            <Style x:Key="BaseStyle" TargetType="{x:Type FrameworkElement}">
                <Style.Setters>
                    <Setter Property="VerticalAlignment" Value="Center" />
                </Style.Setters>
            </Style>

            <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource BaseStyle}" />
            <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseStyle}" />
            <Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseStyle}" />
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="testing textblock" />
            <CheckBox  Content="testing check1 "/>
            <CheckBox Content="testing check2" />
            <Button Content="testing button" />
        </StackPanel>
    </Grid>
</Window>

此外,您可以通过 Application.xaml 在应用程序级别设置这些.这意味着您只需创建一次这些样式,您不需要在所有页面/屏幕上实现它们.

Also, you can set these at an application level via Application.xaml. This would mean that you only have to create these styles once, and you shouldn't need to implement them across all of your pages/screens.

希望能帮到你,

这篇关于设计多种类型的 WPF 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆