Hi Martin,
I think you are having trouble setting DataContext correctly. Try following demo based an your code:
XAML Mainwindow:
<Window x:Class="WpfApp120.Window120"
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:WpfApp120"
xmlns:customControls="clr-namespace:WpfApp120"
mc:Ignorable="d"
Title="Rauter_Martin_231129" Height="450" Width="800">
<Grid>
<customControls:EditToolBar Grid.Row="0"
Test="{Binding Test, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Width="300" Height="100" />
</Grid>
</Window>
CodeBehind MainWindow:
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Windows;
namespace WpfApp120
{
/// <summary>
/// Interaction logic for Window120.xaml
/// </summary>
public partial class Window120 : Window, INotifyPropertyChanged
{
public Window120()
{
InitializeComponent();
DataContext = this;
var timer = new System.Timers.Timer();
timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
timer.Interval = 200;
timer.Start();
}
private string test = "Test";
public string Test
{
get => test;
set { test = value; OnPropertyChanged(); }
}
Random rnd = new Random();
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
string text = rnd.Next().ToString();
Test = text;
}
SynchronizationContext sc = SynchronizationContext.Current;
public event PropertyChangedEventHandler PropertyChanged;
public event EventHandler CanExecuteChanged;
private void OnPropertyChanged([CallerMemberName] string propName = "") =>
sc.Post(new SendOrPostCallback((p) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName))), null);
}
}
XAML UserControl:
<UserControl x:Class="WpfApp120.EditToolBar"
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:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Border BorderBrush="Red" BorderThickness="2">
<TextBox x:Name="tb" Text="{Binding Test}" Width="200" Height="50"/>
</Border>
</UserControl>
CodeBehind UserControl:
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
namespace WpfApp120
{
/// <summary>
/// Interaction logic for Window120UC1.xaml
/// </summary>
public partial class EditToolBar : UserControl, INotifyPropertyChanged
{
public EditToolBar()
{
InitializeComponent();
this.tb.DataContext= this;
}
public static readonly DependencyProperty TestProperty =
DependencyProperty.RegisterAttached("Test", typeof(string), typeof(EditToolBar),
new FrameworkPropertyMetadata(new PropertyChangedCallback(OnTestChanged)));
public string Test
{
get => (string)GetValue(TestProperty);
set { SetValue(TestProperty, value); OnPropertyChanged(); }
}
private static void OnTestChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is string testString) Debug.WriteLine("new value: " + testString);
}
SynchronizationContext sc = SynchronizationContext.Current;
public event PropertyChangedEventHandler PropertyChanged;
public event EventHandler CanExecuteChanged;
private void OnPropertyChanged([CallerMemberName] string propName = "") =>
sc.Post(new SendOrPostCallback((p) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName))), null);
}
}
Result: