2013-07-31 14 views
3

我有一个自定义UserControl(例如称为MyUserControl)与几个子控件。我想创建一个UserControl.Resources,其中包含用于设计目的的样式集和依赖属性分配(没有样式,控件看起来像泥球)。稍后我会评论这些风格。如何在WPF中将TargetType设置为此?

问题是,我不知道如何设置样式TargetType以指出正在开发的UserControl

下面是一个例子怎么可能是这样的:

<UserControl.Resources> 
    <Style TargetType="this"> 
    </Style> 
</UserControl.Resources> 

回答

5

你可以这样说:

<UserControl x:Class="YourProjectName.UserControl1" 
      xmlns:local="clr-namespace:YourProjectName" 

    <UserControl.Resources> 
     <Style TargetType="{x:Type local:UserControl1}"> 
      <Setter Property="Background" Value="Green"></Setter> 
     </Style> 
    </UserControl.Resources> 

</UserControl> 
相关问题