Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.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# 如何将依赖属性从基本WPF用户控件继承到继承的新用户控件?_C#_Wpf_Vb.net_Inheritance - Fatal编程技术网

C# 如何将依赖属性从基本WPF用户控件继承到继承的新用户控件?

C# 如何将依赖属性从基本WPF用户控件继承到继承的新用户控件?,c#,wpf,vb.net,inheritance,C#,Wpf,Vb.net,Inheritance,我可以用VB或C回答,我都知道,最终的解决方案将用VB.Net编写。本质上,我想使用一个模板来重用n个置换中基的依赖属性,但我使用了一个xaml,使用代码隐藏路线,放弃了一个样式模板。基本上,我想在一个用户控件中做一些类似的事情,我想用它作为一个基础: XAML: 我想做的是这样的: 用途1: 所以我有点摸不着头脑,对WPF的语言和功能了解不够,这是可以做到的,还是应该做到的。如果想要合理的继承,应该使用自定义控件 在您的情况下,我将继承TextBlock并向其添加BottomText依赖属性。

我可以用VB或C回答,我都知道,最终的解决方案将用VB.Net编写。本质上,我想使用一个模板来重用n个置换中基的依赖属性,但我使用了一个xaml,使用代码隐藏路线,放弃了一个样式模板。基本上,我想在一个用户控件中做一些类似的事情,我想用它作为一个基础:

XAML:

我想做的是这样的:

用途1:

所以我有点摸不着头脑,对WPF的语言和功能了解不够,这是可以做到的,还是应该做到的。如果想要合理的继承,应该使用自定义控件

在您的情况下,我将继承TextBlock并向其添加BottomText依赖属性。然后在Generic.xaml中对其进行样式设置,大致与您所做的相同


另请参见此处了解UserControls和Custom控件之间的区别后者是为您的用例构建的:

您不能重用基本UserControls的内容,因为这些内容将被派生控件覆盖。基类应该只定义依赖属性,而不具有任何XAML标记

请参考以下示例代码:

Test.vb基类:

UserControl1.xaml:


基类不应该有任何内容或XAML文件,因为这将被派生的UserControl实例覆盖。是否希望能够在派生控件中使用PART_TestLayout网格?是的,大部分绘图都是在代码隐藏中完成的,但我仍然在传统MVVM体系结构中绑定数据元素。我希望重用常见的依赖属性,而不是真正挑剔如何重用。我只是不想为不同的迭代设置2次、3次或4次。@mm8 XAML用户控件继承任何东西时,继承必须有更多的内容。每次尝试时,我都会得到:“为类“Test”指定的基类“BaseTest”不能与其他部分类型之一的基类“UserControl”不同。”。我可以让它继承“UserControl”本身,也可以不继承,实际上,它看起来像是Visual Studio 2017 RC中的某种奇怪的随机错误。这就是我尝试使用RC编写生产代码的结果。我提供了一个工作示例。希望这能有所帮助。不过,我对走这条路线感到好奇的是,通常你的风格是这样的:。这是否意味着我可以设置一种样式作为基础,然后从该样式继承更多样式?这个问题是关于一个更大的问题,我将有大约12个相关属性,所有这些属性都将被重用,但不同的do方法将在曲面上进行不同的设计。我可以制作n个有细微差别的复写副本,但实际上除了三种方法外,其他方法都是一样的。@Djangjazz您继承了一个带有BasedOn属性的样式。在没有看到更完整的示例的情况下,我尝试了这一点,通常您还需要一个模板控件及其绑定的数据的可视元素。我认为您所说的在这种情况下不适用于继承,因为这比继承颜色或特定于UI的属性更复杂。我想继承我从一个定制的基础上设置的东西。@djangojazz-在这种情况下,我必须让你回到我的答案。这就是我一直在寻找的东西,也是我最终选择的路线。即使只是重用我的依赖属性也是一个巨大的胜利。
<UserControl x:Class="Test"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WPFControls"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid Name="PART_TestLayout">
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <TextBlock Text="{Binding TestTitle}" Height="30" Background="White" Foreground="Black" />
    <TextBlock Name="PART_Text2" Grid.Row="1" Background="White" />
  </Grid>
</UserControl>
Imports System.ComponentModel

Public Class Test


  Public Sub New()
    InitializeComponent()
    PART_TestLayout.DataContext = Me
  End Sub

  Public Shared ReadOnly TestTitleProperty As DependencyProperty = DependencyProperty.Register("TestTitle", GetType(String), GetType(Test), New UIPropertyMetadata(String.Empty, AddressOf TestChanged))

  Public Property TestTitle As String
    Get
      Return CType(GetValue(TestTitleProperty), String)
    End Get
    Set
      SetValue(TestTitleProperty, Value)
    End Set
  End Property

  Private Shared Sub TestChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
    Dim m = DirectCast(d, Test)
    m.PART_Text2.Text = $"Changed {DateTime.Now.ToLongTimeString}"
  End Sub
  Public MustOverride Sub DoThing()
End Class
<local:Test x:Class="TestInheritance"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WPFControls"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
    <Label Content="I am the first implentation"/>
    <local:Test TestTitle="{Binding TestText}" />
  </Grid>
</local:Test>
<local:Test x:Class="TestInheritance2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WPFControls"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
    <Label Content="I am the second implentation"/>
    <local:Test TestTitle="{Binding TestText}" />
  </Grid>
</local:Test>
<UserControl x:Class="TestInheritance"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WPFControls"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
    <local:Part1 TestTitle="{Binding TestText}" />
    <!-- myproprietaryContent -->
    <local:Part2 TestLegend="{Binding TestText2}" />
  </Grid>
</local:Test>
'Base class 'Test' specified for class 'TestInheritance' cannot be different from the base class 'UserControl' of one of its other partial types.'
Public MustInherit Class Test
    Inherits UserControl

    Public Shared ReadOnly TestTitleProperty As DependencyProperty = DependencyProperty.Register("TestTitle", GetType(String), GetType(Test), New UIPropertyMetadata(String.Empty, AddressOf TestChanged))

    Public Property TestTitle As String
        Get
            Return CType(GetValue(TestTitleProperty), String)
        End Get
        Set
            SetValue(TestTitleProperty, Value)
        End Set
    End Property

    Private Shared Sub TestChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
        '...
    End Sub
    Public MustOverride Sub DoThing()
End Class
<local:Test x:Class="UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplicationVb1"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>

    </Grid>
</local:Test>
Public Class UserControl1
    Inherits Test

    Public Overrides Sub DoThing()
        '...
    End Sub
End Class