Martien de Jong
Martien de Jong

Reputation: 781

Weird WPF binding behavior

My UserControl binding in WPF does not work when using another bound variable. Here is my (simplified) code.

I have the this WPF UserControl:

XAML:

<UserControl x:Class="WpfApplication.BindingTest"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <TextBlock Text="{Binding Test}" />
</UserControl>

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication
{
    public partial class BindingTest
    {
        public static readonly DependencyProperty TestProperty =
            DependencyProperty.Register(
                "Test", typeof(string),
                typeof(BindingTest)
            );

        public string Test
        {
            get => (string)GetValue(TestProperty);
            set => SetValue(TestProperty, value);
        }

        public BindingTest()
        {
            InitializeComponent();
            DataContext = this;
        }
    }
}

MainWindow XAML:

<Window x:Class="WpfApplication.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <local:BindingTest Test="{Binding hoi}" />
        <TextBlock Margin="5,0,0,0" Text="{Binding hoi}" />
        <local:BindingTest Test="XxX" />
        <TextBlock Margin="5,0,0,0" Text="XxX" />       
    </StackPanel>
</Window>

C#:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Kadaster.NewsLetters.DomainModel;

namespace WpfApplication
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public string hoi => "yYy";

        public MainWindow()
        {
            DataContext = this;
            InitializeComponent();
        }
    }
}

The output that I expect is:

yYy yYy XxX XxX

Instead I get:

yYy XxX XxX

My BindingTest control does not print the yYy text, while the TextBlock does. Any idea why this is?

Upvotes: 0

Views: 185

Answers (2)

Dean Chalk
Dean Chalk

Reputation: 20471

The binding expression in your UserControl is incorrect, and you cannot set the DataContext to this in the UserControl.

1) Remove DataContext assignment:

public BindingTest()
{
    InitializeComponent();
}

2) In your UserControl XAML replace the binding with this:

<TextBlock Text="{Binding RelativeSource={RelativeSource 
    Mode=FindAncestor, AncestorType=local:BindingTest}, 
    Path=Test}" />

Upvotes: 1

Rekshino
Rekshino

Reputation: 7325

TomerAgmon1 has pointed it correct in the comment, that you changed the DataContext for the user control. To solve the issue you can use e.g. a RelativeSource.

<local:BindingTest Test="{Binding DataContext.hoi, RelativeSource={RelativeSource AncestorType=local:MainWindow, Mode=FindAncestor}}"/>

I would prefer to not hard code a DataContext:

namespace WpfApplication
{
    public partial class BindingTest
    {
        public static readonly DependencyProperty TestProperty =
            DependencyProperty.Register(
                "Test", typeof(string),
                typeof(BindingTest)
            );

        public string Test
        {
            get => (string)GetValue(TestProperty);
            set => SetValue(TestProperty, value);
        }

        public BindingTest()
        {
            InitializeComponent();
        }
    }
}

<UserControl x:Class="WpfApplication.BindingTest"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <TextBlock Text="{Binding Test, RelativeSource={RelativeSource AncestorType=local:BindingTest}}" />
</UserControl>

Upvotes: 1

Related Questions