【问题标题】:WPF: How to a define a custom control's look in designer?WPF:如何在设计器中定义自定义控件的外观?
【发布时间】:2011-02-11 01:01:21
【问题描述】:

所以我喜欢可以在 generic.xaml 中重新定义自定义控件的外观这一事实,所以我想创建一个。但我的问题是,您最初如何设计(使用设计器)自定义控件的外观?与用户控件不同,它没有附加的 xaml/设计器窗口。所以必须用代码来设计吗?

【问题讨论】:

  • Expression Blend 为您提供了一个设计表面,可用于...

标签: c# wpf wpf-controls


【解决方案1】:

您创建一个ResourceDictionary XAML 文件,在该文件中为控件定义CustomTemplate/Style,然后将所有此类字典合并到Generic.xaml 字典中。

例如,定义一个控件样式:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid x:Name="RootElement" >
                        <ContentPresenter 
                            Content="{TemplateBinding Content}" 
                            ContentTemplate="{TemplateBinding ContentTemplate}" 
                            Foreground="{TemplateBinding Foreground}" 
                            VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

在你的Generic.xaml:

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="MyButtonStyle.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

结果应该是 Button 类型的控件现在以指定的样式呈现,即使在 IDE 设计器中也是如此。

【讨论】:

  • 这没有回答我的问题
  • 如果您更新样式,所有样式载体都会承担设计器中的更改。如果你想编辑 inline,可以说是在 XAML/Display 分屏中,然后添加一个自定义控件项,在设计器中进行设计,然后将其撕成一个样式——但这很复杂,有点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
相关资源
最近更新 更多