Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带有UserControl的样式继承_C#_Wpf_User Controls - Fatal编程技术网

C# 带有UserControl的样式继承

C# 带有UserControl的样式继承,c#,wpf,user-controls,C#,Wpf,User Controls,我试图自定义usercontrol的样式,继承它的样式并向它添加一些额外的样式。我做了一个小项目来尝试这个。假设我有一个名为UserControl1的用户控件(它包含的内容与此无关-在我的示例项目中为空) 我在我的主窗口中使用它,如下所示: <Window x:Class="WpfStyleInheritance.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

我试图自定义usercontrol的样式,继承它的样式并向它添加一些额外的样式。我做了一个小项目来尝试这个。假设我有一个名为
UserControl1
的用户控件(它包含的内容与此无关-在我的示例项目中为空)

我在我的
主窗口中使用它,如下所示:

<Window x:Class="WpfStyleInheritance.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfStyleInheritance"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <local:UserControl1>
            <local:UserControl1.Style>
                <Style TargetType="{x:Type local:UserControl1}" BasedOn="{x:Type local:UserControl1}">
                    <Setter Property="Margin" Value="0" />
                </Style>
            </local:UserControl1.Style>
        </local:UserControl1>
    </Grid>
</Window>

基本语法不正确。应该是:

BasedOn="{StaticResource {x:Type local:UserControl1}}"

即使遇到设计器错误,也要尝试重新编译代码。确保添加的命名空间已正确映射到UserControl

另外,确保MainWindow和UserControl1位于同一程序集中

如果它们驻留在不同的程序集中,则必须在命名空间声明中指定程序集名称:

xmlns:local="clr-namespace:WpfStyleInheritance;assembly=AssemblyName"

更新

如果尚未为用户控件1定义任何默认样式,则不需要使用基于的默认样式,因为用户控件不存在默认样式。这就是为什么你会得到一个例外

拆下底座,它就会工作得很好


您在XAML中定义的资源不是默认样式,而是UserControl的本地样式,当您在MainWindow中定义另一个本地资源时,该样式将被覆盖

如果您想要UserControl1的默认样式,请在
应用程序资源下声明,即在
App.xaml中声明

<Application.Resources>
   <Style TargetType="{x:Type local:UserControl1}">
      <Setter Property="Height" Value="0" />
   </Style>
</Application.Resources>

现在在MainWindow.xaml中,这将很好地工作:

<local:UserControl1>
    <local:UserControl1.Style>
        <Style TargetType="{x:Type local:UserControl1}"
               BasedOn="{StaticResource {x:Type local:UserControl1}}">
            <Setter Property="Margin" Value="0" />
        </Style>
    </local:UserControl1.Style>
</local:UserControl1>


正如我在问题中解释的那样,我试过了。如果我运行,我会得到一个XamlParseException可以发布XamlParseException的内部异常吗?找不到名为“wpfstyleInheritation.UserControl1”的资源。资源名称区分大小写。能否发布UserControl1的代码?很可能程序集不同或名称不正确。它只是一个空的UserControl,即添加新项后得到的类型。
<local:UserControl1>
    <local:UserControl1.Style>
        <Style TargetType="{x:Type local:UserControl1}"
               BasedOn="{StaticResource {x:Type local:UserControl1}}">
            <Setter Property="Margin" Value="0" />
        </Style>
    </local:UserControl1.Style>
</local:UserControl1>