我不知道我的代码出了什么问题.我创建了我自己的控件元素的一个实例-ControlA
(在代码中创建).它有一个包含一些 struct 的Data
属性.它显示部分数据,我想使用另一个自定义控件ControlB
来显示数据的子集.但是,当我使用XAML中A组件的数据设置ControlB
组件的属性时,它没有出现.
怎么啦?
创建它们中的ControlA
个:
var controlA = new ControlA();
controlA.Data = new Tuple<string, string>("Some text data", "Some subset data");
_grid.Children.Add(controlA);
ControlA
:
<?xml version="1.0" encoding="utf-8"?>
<UserControl
x:Class="winui_3_app.components.ControlA"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:winui_3_app.components"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel>
<Grid Background="Aquamarine">
<TextBlock Text="{Binding Data.Item1}" />
</Grid>
<Grid Background="Coral">
<local:ControlB Text="{Binding Data.Item2}" />
</Grid>
</StackPanel>
</UserControl>
using System;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace winui_3_app.components;
public sealed partial class ControlA : UserControl
{
public Tuple<string, string> Data
{
get => (Tuple<string, string>)GetValue(DataProperty);
set => SetValue(DataProperty, value);
}
public static readonly DependencyProperty DataProperty = DependencyProperty.Register(nameof(Data), typeof(Tuple<string, string>), typeof(ControlA), new PropertyMetadata(null));
public ControlA()
{
this.InitializeComponent();
this.DataContext = this;
}
}
ControlB
:
<?xml version="1.0" encoding="utf-8"?>
<UserControl
x:Class="winui_3_app.components.ControlB"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:winui_3_app.components"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<TextBlock Text="{Binding Text}" />
</Grid>
</UserControl>
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace winui_3_app.components;
public sealed partial class ControlB : UserControl
{
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(nameof(Text), typeof(string), typeof(ControlB), new PropertyMetadata(string.Empty));
public ControlB()
{
this.InitializeComponent();
this.DataContext = this;
}
}