我已經創建了一個自定義的WPF控件。該控件充當各個區域的容器(因此它可以像母版頁一樣工作)。在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>
的問題是,這樣式僅在運行時加載。所以在設計模式下,我的控件沒有任何樣式,也沒有任何大小或佈局。我怎樣才能讓我的控件成爲設計模式的默認樣式?
更新: 我取得一些進展...看來我可以指定一個默認的主題在一個名爲\主題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());
}
嗨塞爾吉奧。我嘗試了類似的東西(見上面的代碼)。但是,這不能編譯。也許我是從不同的基類繼承而來的? – willem 2009-08-05 13:47:40