0

So I have a UserControl, which contains an ItemsControl. I need the controls defined inside the DataTemplate to bind to a dependency property in my UserControl.

Let's say my UserControl has a dependency property MyProp. I want to do the following in my xaml code...

<UserControl>
  ...
   <ItemsControl>
      <ItemsControl.ItemTemplate>
        <DataTemplate x:DataType="MyType">
           <custom:ControlThatExpectsMyType
              SomeProperty={x:Bind MyProp}>
           </custom:ControlThatExpectsMyType>
        </DataTemplate>
      <ItemsControl.ItemTemplate>
   </ItemsControl>
  ...
</UserControl>

The MyProp dependency property in the above snippet is not part of MyType, but of my UserControl. Both Binding and x:Bind are acceptable solutions, but none of the "old wpf ways" seem to be working in winui 3.

So, what's the proper -expected- way to do it in winui 3?

1 Answer 1

0

Setting the x:Name for the UserControl should help. Then use Binding with the ElementName instead of x:Bind.

For example:

<UserControl x:Name="This" ...>
...
   <ItemsControl>
      <ItemsControl.ItemTemplate>
        <DataTemplate x:DataType="MyType">
           <custom:ControlThatExpectsMyType
              SomeProperty={Binding ElementName=This, Path=MyProp}>
           </custom:ControlThatExpectsMyType>
        </DataTemplate>
      <ItemsControl.ItemTemplate>
   </ItemsControl>
...
</UserControl>

Here is a sample code that works on my side:

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

namespace UnpackagedDemo;

public sealed partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
        MyProperty = "Hello, World!";
    }

    public string MyProperty
    {
        get => (string)GetValue(MyPropertyProperty);
        set => SetValue(MyPropertyProperty, value);
    }

    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.Register(
            nameof(MyProperty),
            typeof(string),
            typeof(UserControl1),
            new PropertyMetadata(default));
}
<UserControl
    x:Class="UnpackagedDemo.UserControl1"
    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:UnpackagedDemo"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Name="This"
    mc:Ignorable="d">

    <Grid>
        <ItemsControl>
            <x:String>This text will be replaced with MyProperty.</x:String>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding ElementName=This, Path=MyProperty}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</UserControl>
Sign up to request clarification or add additional context in comments.

3 Comments

A well-known solution, perfectly working in WPF. Unfortunately, it doesn't seem to be the case in winui 3.
I confirmed it works before I posted my answer. I just added the sample code that works on my side.
@FrustratedSoul It works for generic controls and see learn.microsoft.com/en-us/answers/questions/1611265/…. For UserControl, could you please show a minimal, reproducible sample without private information?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.