【问题标题】:WinUI 3: How to bind to data itself in a DataTemplate?WinUI 3:如何在 DataTemplate 中绑定到数据本身?
【发布时间】:2023-02-25 16:06:54
【问题描述】:

考虑一个 ListView:

<ListView ItemsSource="{x:Bind People}">
  <ListView.ItemTemplate x:DataType="models:Person">
    <controls:PersonItem Person="{...}" />
  </ListView.ItemTemplate>
</ListView>

在这种情况下,People 是在代码隐藏中定义的 ObservebleCollection&lt;Person&gt; 类型的属性。 Person 类的定义如下:

public class Person : INotifyPropertyChanged
{
  public virtual Guid Id { get; set; }
  public virtual string Name { get; set; }

  // The implemention of INotifyPropertyChanged:
  ...
}

控件PersonItem 是一个自定义用户控件,定义了DependencyProperty,称为Person,并接收Person 对象作为值。该控件将使用该属性的成员在屏幕上显示一些信息。

我想知道的是,如何将 Person 属性绑定到 数据模板,换句话说,应该用什么来代替第一个代码sn-p中的...

虽然我知道Person中的属性很少,我可以在我的自定义控件中将它们简单地定义为DependencyProperty,但我遇到的实际情况是我得到了一个有太多属性需要使用的类在一个控件中,那么我需要做的工作就太多了。

【问题讨论】:

  • x:BindBindingBinding Path=.Binding Path=/都试过了,但是都没有效果所以只能得到null,这是依赖属性Person的默认值。

标签: c# wpf xaml uwp winui-3


【解决方案1】:

x:BindBinding 应该可以。

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace WinUI3App;

public sealed class PersonItem : Control
{
    public static readonly DependencyProperty PersonProperty =
        DependencyProperty.Register(
            nameof(Person),
            typeof(Person),
            typeof(PersonItem),
            new PropertyMetadata(null, OnPersonPropertyChanged));

    public PersonItem()
    {
        this.DefaultStyleKey = typeof(PersonItem);
    }

    public Person Person
    {
        get => (Person)GetValue(PersonProperty);
        set => SetValue(PersonProperty, value);
    }

    private static void OnPersonPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue is Person person)
        {
        }
    }
}
using Microsoft.UI.Xaml;
using System;
using System.Collections.ObjectModel;

namespace WinUI3App;

public class Person
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; } = string.Empty;
}

public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();

        for (int i = 0; i < 10; i++)
        {
            People.Add(new Person()
            {
                Id = Guid.NewGuid(),
                Name = Guid.NewGuid().ToString(),
            });
        }
    }

    public ObservableCollection<Person> People { get; set; } = new();
}
<Window
    x:Class="WinUI3App.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:WinUI3App"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <ListView ItemsSource="{x:Bind People}">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="local:Person">
                <!--<local:PersonItem Person="{Binding}" />-->
                <local:PersonItem Person="{x:Bind}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

</Window>

【讨论】:

    猜你喜欢
    • 2022-10-19
    • 1970-01-01
    • 2022-10-09
    • 1970-01-01
    • 2022-06-10
    • 2022-01-14
    • 2022-10-14
    • 1970-01-01
    • 2016-09-07
    相关资源
    最近更新 更多