在WPF中,如何为我的自定义控件设置一个默认样式以在设计模式下使用?

3

我创建了一个自定义的WPF控件。该控件作为一个包含多个区域的容器(因此可以像主页面一样工作)。

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

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

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

<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>

问题在于此样式仅在运行时加载。 因此,在设计模式下,我的控件没有任何样式,也没有任何大小或布局。我该如何为设计模式下的控件设置默认样式?
更新: 我正在取得一些进展……似乎我可以指定一个默认主题来使用一个名为Themes\Generic.xaml的文件。这在一个小的示例项目中可以正常工作,但出于某种原因,当我在实际项目中执行相同的操作时,我的VS2008设计器会保持空白……帮帮忙?:(
请注意,我的自定义控件代码如下:
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());
    }
4个回答

6

通过对项目文件的大量查找,我已经找出了问题所在!

  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设计器可以使用了!


1

你试过了吗?

public EditControlMaster()
{
  DefaultStyleKey = typeof(EditControlMaster);
}

作为构造函数的一部分?


嗨Sergio。我尝试了类似的东西(参见上面的代码)。然而,它无法编译。也许我继承的基类与你不同? - willem
我认为那是针对SilverLight的。 - n.podbielski

0

还有一件事,在我的经验中,如果在 themes\generic.xaml 中定义的样式中使用 DynamicResource(就像您为边框所做的那样),它通常不起作用。您应该考虑将其更改为 StaticResource 查找。


0

尝试将样式移动到标准位置。

  • 添加/新建项目/自定义控件(WPF)/"MyDummyControl"
  • 现在将您的样式放置在创建的"Themes/Generic.xaml"中
  • 删除"MyDummyControl"文件和样式
  • 删除您的Theme.xaml和MergedDictionaries

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接