在 WPF 中,如何为我的自定义控件提供要在设计模式下使用的默认样式? [英] In WPF, how do I give my custom control a default style to be used in Design Mode?

查看:23
本文介绍了在 WPF 中,如何为我的自定义控件提供要在设计模式下使用的默认样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义 WPF 控件.该控件充当具有各种区域的容器(因此它可以像母版页一样工作).

I have created a custom WPF control. The control acts as a container with various regions (so it can work like a master page).

此控件的样式在运行时从单独的资源字典加载,如下所示:

The style for this control is loaded at runtime from a separate resource dictionary as follows:

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/MyApp.Application;component/Themes/Theme.xaml" x:Name="theme"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

我的自定义控件的样式如下...

My custom control's style looks as follows...

<Style TargetType="{x:Type shareduc:EditControlMaster}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type shareduc:EditControlMaster}">
                <Grid>
                    <Grid.ColumnDefinitions></Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>

                    <Border BorderBrush="{DynamicResource xxBorderBrush}" 
                                BorderThickness="0,1,0,1" Background="White" Grid.Row="0">
                        <Grid >
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto"></ColumnDefinition>
                                <ColumnDefinition Width="*"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="auto"></RowDefinition>
                                <RowDefinition Height="auto"></RowDefinition>
                            </Grid.RowDefinitions>

                            <ContentPresenter Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" Margin="10" Content="{TemplateBinding Image}"  />
                            <ContentPresenter Grid.Row="0" Grid.Column="1" Margin="2" Content="{TemplateBinding Title}"  />
                            <ContentPresenter Grid.Row="1" Grid.Column="1" Margin="2" Content="{TemplateBinding Abstract}"  />
                        </Grid>
                    </Border>

                    <ContentPresenter Grid.Row="1" Margin="2" Content="{TemplateBinding Content}" />

                </Grid>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

问题是这个样式只在运行时加载.所以在设计模式下,我的控件没有任何样式,也没有任何大小或布局.如何为我的控件提供设计模式的默认样式?

The problem is that this style is only loaded at Runtime. So in Design Mode my control does not have any style and does not have any size or layout. How can I give my control a default style for Design Mode?

更新:我正在取得一些进展……看来我可以在名为 Themes\Generic.xaml 的文件中指定要使用的默认主题.这在一个小的示例项目中工作正常,但由于某种原因,当我在实际项目中做同样的事情时,我的 VS2008 设计器保持空白......有帮助吗?:(

Update: I'm making some progress... it appears I can specify a default theme to use in a file called Themes\Generic.xaml. This works fine in a small sample project, but for some reason my VS2008 designer stays blank when I do the same thing in my actual project... Help? :(

请注意,我的自定义控件的代码如下所示:

Note that my custom control's code looks as follows:

public partial class EditControlMaster : Control
    {
        static EditControlMaster()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(EditControlMaster),
              new FrameworkPropertyMetadata(typeof(EditControlMaster)));
        }

        public object Title
        {
            get { return (object)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        public static readonly DependencyProperty TitleProperty =
          DependencyProperty.Register("Title", typeof(object),
          typeof(EditControlMaster), new UIPropertyMetadata());



        public object Image
        {
            get { return (object)GetValue(ImageProperty); }
            set { SetValue(ImageProperty, value); }
        }

        public static readonly DependencyProperty ImageProperty =
          DependencyProperty.Register("Image", typeof(object),
          typeof(EditControlMaster), new UIPropertyMetadata());


        public object Abstract
        {
            get { return (object)GetValue(AbstractProperty); }
            set { SetValue(AbstractProperty, value); }
        }

        public static readonly DependencyProperty AbstractProperty =
          DependencyProperty.Register("Abstract", typeof(object),
          typeof(EditControlMaster), new UIPropertyMetadata());

        public object Content
        {
            get { return (object)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        public static readonly DependencyProperty ContentProperty =
          DependencyProperty.Register("Content", typeof(object),
          typeof(EditControlMaster), new UIPropertyMetadata());
    }

推荐答案

通过对项目文件的大量研究,我发现出了什么问题!

Through lots of poking around project files I have figured out what was wrong!

  1. Themes\Generic.xaml 包含控件的默认样式.这很好.
  2. 您的 Assembly.cs 文件需要包含以下属性:

  1. Themes\Generic.xaml contains your control's default Style. This is fine.
  2. Your Assembly.cs file needs to contain the following attribute:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
    //(used if a resource is not found in the page, 
    // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
    //(used if a resource is not found in the page, 
    // app, or any theme specific resource dictionaries)
)]

瞧!VS2008 设计师作品!

Voila! The VS2008 designer works!

这篇关于在 WPF 中,如何为我的自定义控件提供要在设计模式下使用的默认样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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