WPF UserControl은 어떻게 WPF UserControl을 상속합니까?
다음 WPF UserControl은 동작하는 DataType WholeNumber를 호출했습니다.
이제 DataTypeDateTime 및 DataTypeEmail 등이라고 하는 UserControl을 만듭니다.
대부분의 종속성 속성은 이러한 모든 컨트롤에 의해 공유되기 때문에 공통 메서드를 BaseDataType에 넣고 각 UserControls를 이 기본 유형에서 상속받도록 하겠습니다.
그러나 이 경우 "Partial Declaration(부분 선언)"에 다른 기본 클래스가 없을 수 있습니다.
공유 기능이 모두 기본 클래스에 포함되도록 UserControls를 사용하여 상속을 구현하려면 어떻게 해야 합니까?
using System.Windows;
using System.Windows.Controls;
namespace TestDependencyProperty827.DataTypes
{
public partial class DataTypeWholeNumber : BaseDataType
{
public DataTypeWholeNumber()
{
InitializeComponent();
DataContext = this;
//defaults
TheWidth = 200;
}
public string TheLabel
{
get
{
return (string)GetValue(TheLabelProperty);
}
set
{
SetValue(TheLabelProperty, value);
}
}
public static readonly DependencyProperty TheLabelProperty =
DependencyProperty.Register("TheLabel", typeof(string), typeof(BaseDataType),
new FrameworkPropertyMetadata());
public string TheContent
{
get
{
return (string)GetValue(TheContentProperty);
}
set
{
SetValue(TheContentProperty, value);
}
}
public static readonly DependencyProperty TheContentProperty =
DependencyProperty.Register("TheContent", typeof(string), typeof(BaseDataType),
new FrameworkPropertyMetadata());
public int TheWidth
{
get
{
return (int)GetValue(TheWidthProperty);
}
set
{
SetValue(TheWidthProperty, value);
}
}
public static readonly DependencyProperty TheWidthProperty =
DependencyProperty.Register("TheWidth", typeof(int), typeof(DataTypeWholeNumber),
new FrameworkPropertyMetadata());
}
}
xaml의 첫 번째 태그가 새 기본 유형에서 상속되도록 변경되었는지 확인합니다.
그렇게
<UserControl x:Class="TestDependencyProperty827.DataTypes.DataTypeWholeNumber"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
>
된다
<myTypes:BaseDataType x:Class="TestDependencyProperty827.DataTypes.DataTypeWholeNumber"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:myTypes="clr-namespace:TestDependencyProperty827.DataTypes"
>
따라서, 아래 코멘트의 추가 정보를 포함한 완전한 답변을 요약합니다.
- 기본 클래스에는 xaml 파일을 포함할 수 없습니다.1개의 (부분이 아닌) cs 파일로 정의하고 Usercontrol에서 직접 상속하도록 정의합니다.
- cs code-behind 파일과 xaml의 첫 번째 태그(상기와 같이) 양쪽에서 서브클래스가 베이스 클래스로부터 상속되고 있는 것을 확인합니다.
public partial class MooringConfigurator : MooringLineConfigurator
{
public MooringConfigurator()
{
InitializeComponent();
}
}
<dst:MooringLineConfigurator x:Class="Wave.Dashboards.Instruments.ConfiguratorViews.DST.MooringConfigurator"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:dst="clr-namespace:Wave.Dashboards.Instruments.ConfiguratorViews.DST"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
</Grid>
</dst:MooringLineConfigurator>
이 기사에서 답을 찾았습니다.http://www.paulstovell.com/xmlnsdefinition
기본적으로는 AssemlyInfo.cs 파일에 XML 네임스페이스를 정의해야 합니다.XAML에서 사용할 수 있습니다.이거는 효과가 있었는데 베이스 사용자 컨트롤 클래스를 다른 DLL에 배치했습니다.
디자이너에 의해 작성된 부분 클래스 정의가 있습니다.Initialize Component() 메서드 정의를 통해 쉽게 열 수 있습니다.그런 다음 부분 클래스 상속을 UserControl에서 BaseDataType(또는 클래스 정의에서 지정한 모든 항목)으로 변경하십시오.
그 후 Initialize Component() 메서드가 자녀 클래스에 숨겨져 있다는 경고가 표시됩니다.
따라서 CustomControl을 UserControl이 아닌 base clas로 만들어 기본 클래스의 부분적인 정의를 피할 수 있습니다(한 코멘트 참조).
같은 문제가 발생했지만 디자이너가 지원하지 않는 추상 클래스에서 제어를 상속해야 했습니다.이 문제를 해결한 것은 사용자 제어를 표준 클래스(UserControl을 상속하는 클래스)와 인터페이스 모두에서 상속하는 것입니다.디자이너가 이렇게 작업하고 있습니다.
//the xaml
<local:EcranFiche x:Class="VLEva.SIFEval.Ecrans.UC_BatimentAgricole"
xmlns:local="clr-namespace:VLEva.SIFEval.Ecrans"
...>
...
</local:EcranFiche>
// the usercontrol code behind
public partial class UC_BatimentAgricole : EcranFiche, IEcranFiche
{
...
}
// the interface
public interface IEcranFiche
{
...
}
// base class containing common implemented methods
public class EcranFiche : UserControl
{
... (ex: common interface implementation)
}
언급URL : https://stackoverflow.com/questions/887519/how-can-a-wpf-usercontrol-inherit-a-wpf-usercontrol
'programing' 카테고리의 다른 글
Bash에서 파일 내용을 루프하는 중 (0) | 2023.04.09 |
---|---|
텍스트 및 파일스트림을 통해 openpyxl 파일 저장 (0) | 2023.04.09 |
R의 두 목록을 결합하는 방법 (0) | 2023.04.09 |
Forked Repo에서 풀 요청을 업데이트하려면 어떻게 해야 합니까? (0) | 2023.04.09 |
WPF 바인딩을 사용한2개의 명령어파라미터 전달 (0) | 2023.04.09 |