加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

如何在WPF Window.Resources中设置样式.

发布时间:2020-12-14 04:03:00 所属栏目:Windows 来源:网络整理
导读:我想在Window.Resources中创建多个样式.下面是我尝试过的代码,但它不起作用: Window.Resources Style x:Key="StyleOne" TargetType="{x:Type Control}" Setter Property="Control.Background" Value="Blue"/Setter Setter Property="Control.Height" Value=
我想在Window.Resources中创建多个样式.下面是我尝试过的代码,但它不起作用:

<Window.Resources>
    <Style x:Key="StyleOne" TargetType="{x:Type Control}">
        <Setter Property="Control.Background" Value="Blue"></Setter>
        <Setter Property="Control.Height" Value="20"></Setter>
    </Style>
    <Style x:Key="StyleTwo" BasedOn="{StaticResource StyleOne}">
        <Setter Property="Control.Background" Value="Red"></Setter>
        <Setter Property="Control.Height" Value="20"></Setter>
    </Style>
</Window.Resources>
<Button Style="{StaticResource StyleOne}"></Button>
<Button Style="{StaticResource StyleTwo}"></Button>

它抛出一个错误说:

The property “Content” is set more than once.

解决方法

这个错误与样式无关,窗口只能包含一个子节点(设置 Content),使用一些可以包含多个子节点的容器.例如a StackPanelGrid.

<StackPanel>
     <Button .../>
     <Button .../>
</StackPanel>

(另见:Panels Overview)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读