Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF:如何定义自定义控件&x27;设计师的造型?_C#_Wpf_Wpf Controls - Fatal编程技术网

C# WPF:如何定义自定义控件&x27;设计师的造型?

C# WPF:如何定义自定义控件&x27;设计师的造型?,c#,wpf,wpf-controls,C#,Wpf,Wpf Controls,因此,我喜欢自定义控件的外观可以在generic.xaml中重新定义,所以我想创建一个。但我的问题是,最初如何设计(使用设计器)自定义控件的外观?与用户控件不同,它没有附加的xaml/designer窗口。所以必须在代码中进行设计吗?您创建一个资源字典XAML文件,在该文件中为控件定义自定义模板/样式,然后将所有这类字典合并到Generic.XAML字典中 例如,定义控件样式: <ResourceDictionary xmlns="http://schemas.microsoft

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

您创建一个
资源字典
XAML
文件,在该文件中为控件定义
自定义模板
/
样式
,然后将所有这类字典合并到
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>

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

Expression Blend为您提供了一个无法使用的设计图面……这并不能回答我的问题。如果您更新样式,则所有样式载体都将承担设计器中的更改。如果您想在XAML/Display分割屏幕中进行内联编辑,然后添加一个自定义控件项,在设计器中进行设计,然后将其撕成一个样式-但这有点复杂。
<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="MyButtonStyle.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>