Skip to content

TextBlock Binding Fails When Placed in ContentControl with Content Set to Custom Helper [WinUI] #10584

@SenthilkumarSF4640

Description

@SenthilkumarSF4640

Describe the bug

In WinUI, the TextBlock binding does not work correctly when it is placed inside a ContentControl and the Content is set to a custom helper object. The same implementation works as expected in WPF.

**MainWindow.xaml**
<Grid x:Name="RootGrid" >
    <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Width="200">
        <TextBlock Text="Enter the text below:" FontSize="15" FontWeight="SemiBold" Padding="10 0 0 0"/>
        <TextBox Text="{Binding Path=MyText, Mode=TwoWay}"/>
        <ContentControl x:Name="contentControl" Padding="10 0 0 0">
            <ContentControl.ContentTemplate>
                <DataTemplate>
                    <TextBlock x:Name="txtBlock" Text="{Binding Path=Value}" FontSize="20" Foreground="Blue"/>
                </DataTemplate>
            </ContentControl.ContentTemplate>
        </ContentControl>
    </StackPanel>
</Grid>

**MainWindow.xaml.cs**
public sealed partial class MainWindow : Window
{
    public MyViewModel ViewModel { get; } = new MyViewModel();
    public CustomHelper customHelper = new CustomHelper();
    public MainWindow()
    {
        this.InitializeComponent();
        RootGrid.DataContext = ViewModel;
        Binding binding = new Binding
        {
            Path = new PropertyPath("MyText"),
            Source = ViewModel,
            Mode = BindingMode.OneWay
        };
        customHelper.SetValueBinding(binding);
        contentControl.Content = customHelper;
    }
}

**ViewModel**
    public class MyViewModel : INotifyPropertyChanged
    {
        private string _myText;
        public string MyText
        {
            get => _myText;
            set
            {
                _myText = value;
                OnPropertyChanged(nameof(MyText));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name) =>
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }

**CustomHelper**
 public class CustomHelper : DependencyObject, INotifyPropertyChanged
 {
     public object Value
     {
         get { return GetValue(ValueProperty); }
         set 
         { 
             SetValue(ValueProperty, value);
             OnPropertyChanged("Value");
         }
     }

     public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(object), 
         typeof(CustomHelper), new PropertyMetadata(null, OnValueChanged));

     public event PropertyChangedEventHandler? PropertyChanged;
     protected void OnPropertyChanged(string name) =>
         PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));

     private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
     {

     }

     public void SetValueBinding(BindingBase binding)
     {
         BindingOperations.SetBinding(this, ValueProperty, binding);
     }
 }

Steps to reproduce the bug

  1. Enter a value in the TextBox.
  2. Press the Tab key to commit the value.

Observed Behavior:
Value is not updated based on binding.

Expected behavior

The entered value should be reflected in the TextBlock because of binding.

Screenshots

WinUI Demo Video

WinUI_Demo.mp4

WPF Demo Video

WPF_Demo.mp4

NuGet package version

WinUI 3 - Windows App SDK 1.7.2: 1.7.250513003

Windows version

Windows 11 (24H2): Build 26100

Additional context

WinUI Sample - WinUI_SimpleSample.zip

WPF Sample - WPF_NetCoreSimpleSample.zip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions