gpt4 book ai didi

wpf - 在 WPF 中,如何为我的自定义控件提供在设计模式中使用的默认样式?

转载 作者:行者123 更新时间:2023-12-01 11:09:50 25 4
gpt4 key购买 nike

我已经创建了一个自定义 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());
}

最佳答案

通过大量查看项目文件,我发现了问题所在!

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

    [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 设计器工作!

关于wpf - 在 WPF 中,如何为我的自定义控件提供在设计模式中使用的默认样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1232584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com