我不知道我的代码出了什么问题.我创建了我自己的控件元素的一个实例-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;
    }
}

推荐答案

因此,由于某种原因,绑定无法解析(也许有人可以添加这个原因),即使ControlA中的DataContext已经设置,您也会得到Error: BindingExpression path error: 'Data' property not found on 'CustomInCustom.ControlB'. BindingExpression: Path='Data.Item2' DataItem='CustomInCustom.ControlB'; target element is 'CustomInCustom.ControlB' (Name='null'); target property is 'Text' (type 'String').也许是因为ControlBControlA之前装填?我不确定.

重要的是,解决方案是用x:Bind取代Binding.

Csharp相关问答推荐

System.Data.SQLite:判断SQLite数据库是否为空(任何表中至少有一行)

Serilog SQL服务器接收器使用UTC作为时间戳

CsWin32如何创建PWSTR的实例,例如GetWindowText

在. NET Core 8 Web API中,当为服务总线使用通用消费者时,如何防止IServiceProvider被释放或空?"

EF Core在请求列表时忽略列,但在按ID获取时包含

如何在Visual Studio代码中更改大括号模式{},用于C#语言

如何忽略API JSON响应中的空字符串?

.NET SDK包中的官方C#编译器在哪里?

NET8 Maui&;iOS:AppCenter崩溃错误

ASP.NET Core MVC将值从视图传递到控制器时出现问题

LINQ to Entities中的加权平均值

在C#.NET项目中启动时,如何等待提升的PowerShell进程退出?

在C#ASP.NET内核中使用INT AS-1进行控制器场景的单元测试

我找不到ASP.NET Web应用程序(.NET框架).已 Select .NET框架项目和项模板以及此组件(&Q;)

为什么我的用户界面对象移动到略低于实际目标?

如何在Cake脚本中设置MSBuild.exe的绝对路径

Xamarin Forms应用程序中登录页面的用户名和密码编辑文本之间不需要的空格

使用ITfoxtec.Identity.Saml2解析相同键多值SAML 2声明

无法创建工具窗口(用于VBIDE、VBA的COM加载项扩展)

部署Aspnet Blazor服务器时出现未处理的Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]异常