我是 WinUI 和 XAML 的新手,我正在創建一個包含各種CustomControl
.
它們都是獨立的,可以單獨使用。但是其中一個控制元件是通過嵌入庫中的其他一些自定義控制元件來制作的。
容器控制元件的 XAML
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyCustomControls">
<Style TargetType="local:CustomContainer">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomContainer">
<StackPanel>
<local:CustomTextBlock x:Name="Text"
DisplayMode="{TemplateBinding DisplayMode}"
Message="{TemplateBinding Message}">
</local:CustomTextBlock>
<local:CustomIndicator x:Name="Indicator"></local:CustomIndicator>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
正如您在示例中看到的那樣,CustomTextBlock
自定義控制元件包含 2 DependecyProperty
(DisplayMode
和Message
),我需要在CustomContainer
控制元件上“復制”它們才能在我想CustomContainer
在頁面上使用時設定它們。
在我使用TemplateBinding
2的 XAMLDependecyProperty
中,我應該在CustomContainer
.
CustomTextBlock
控制背后的代碼
private static readonly DependencyProperty DisplayModeProperty = DependencyProperty.Register(
nameof(DisplayMode), typeof(bool), typeof(CustomTextBlock), new PropertyMetadata(null));
public bool DisplayMode
{
get => (bool)GetValue(DisplayModeProperty);
set => SetValue(DisplayModeProperty, value);
}
private static readonly DependencyProperty MessageProperty = DependencyProperty.Register(
nameof(Message), typeof(string), typeof(CustomTextBlock), new PropertyMetadata(default(string), (d, e) => ((CustomTextBlock)d).MessageChanged(d, e)));
public string Message
{
get => (string)GetValue(MessageProperty);
set => SetValue(MessageProperty, value);
}
如何公開控制元件CustomTextBlock
上的 2 個屬性,CustomContainer
以便這些值直接設定基礎屬性?他們還需要DependencyProperty
打字嗎?
這似乎有點像包裝或繼承的概念,但我無法弄清楚,特別是對于Message
也注冊了事件處理程式的屬性。
uj5u.com熱心網友回復:
在此示例中,我們有:
InnerControl
帶有一個TextBlock
和一個InnerText
依賴屬性系結到TextBlock
OuterControl
與一個InnerControl
和一個TextForInnerControl
依賴屬性系結到InnerControl
MainWindow
與 aTextBox
和 an系結OuterControl
到TextForInnerControl
TextBox
TextBox
來自in 的文本MainWindow
將傳遞給OuterControl
,然后傳遞給InnerControl
。
這是代碼:
內部控制元件.cs
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace WinUI3CustomControlTest;
public sealed class InnerControl : Control
{
public static readonly DependencyProperty InnerTextProperty =
DependencyProperty.Register(
nameof(InnerText),
typeof(string),
typeof(InnerControl),
new PropertyMetadata(string.Empty));
public InnerControl()
{
this.DefaultStyleKey = typeof(InnerControl);
}
public string InnerText
{
get => (string)GetValue(InnerTextProperty);
set => SetValue(InnerTextProperty, value);
}
}
外部控制元件.cs
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace WinUI3CustomControlTest;
public sealed class OuterControl : Control
{
public static readonly DependencyProperty TextForInnerControlProperty =
DependencyProperty.Register(
nameof(TextForInnerControl),
typeof(string),
typeof(OuterControl),
new PropertyMetadata(string.Empty));
public OuterControl()
{
this.DefaultStyleKey = typeof(OuterControl);
}
public string TextForInnerControl
{
get => (string)GetValue(TextForInnerControlProperty);
set => SetValue(TextForInnerControlProperty, value);
}
}
通用的.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WinUI3CustomControlTest">
<Style TargetType="local:OuterControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:OuterControl">
<StackPanel Orientation="Vertical">
<local:InnerControl InnerText="{TemplateBinding TextForInnerControl}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="local:InnerControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:InnerControl">
<StackPanel>
<TextBlock Text="{TemplateBinding InnerText}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
主視窗.xaml
<Window
x:Class="WinUI3CustomControlTest.MainWindow"
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:local="using:WinUI3CustomControlTest"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel>
<TextBox
x:Name="InputTextBox"
PlaceholderText="Enter you text.." />
<Button
Click="Button_Click"
Content="Set text programmatically" />
<local:OuterControl
x:Name="MyOuterControl"
TextForInnerControl="{x:Bind InputTextBox.Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</Window>
主視窗.xaml.cs
using Microsoft.UI.Xaml;
using System;
namespace WinUI3CustomControlTest;
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.MyOuterControl.TextForInnerControl = DateTime.Now.ToString();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/506309.html
上一篇:如何訪問您的嵌套自定義控制元件?