Hi out there!
I created a UserControl an would like to do some logical stuff when the property "MyText" of that UserControl ist changed.
From my Master Window I would like to set a property (or dependency property) to trigger that change. When I set the property directly by name everything works like expected (Name: DirectCalledControl) . But when I try to set the new value with a binding in xaml, the text is updated, but the logik behind ist not fired. In my case the new datetime is not added to the text.
I will use a Control like that in an MVVM app. So it would be nice to do that over propertychanged. How can I make it?
I tried many, many things but nothing worked. I also tried different variations with datacontext and different modes (two way), but also without luck.
UserControl
<UserControl x:Class="WpfUserControlTest.MyTestUserControl"
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:WpfUserControlTest"
mc:Ignorable="d"
x:Name="TestUCName"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<StackPanel>
<TextBlock Text="UserControl from Project"
FontSize="26"/>
<TextBlock Text="{Binding MyText, ElementName=TestUCName}"
FontSize="24"/>
</StackPanel>
</Grid>
</UserControl>
namespace WpfUserControlTest
{
public partial class MyTestUserControl : UserControl
{
public MyTestUserControl()
{
InitializeComponent();
}
public string MyText
{
get { return (string)GetValue(MyTextProperty); }
set { SetValue(MyTextProperty, Changer(value));}
}
// Using a DependencyProperty as the backing store for MyText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyTextProperty =
DependencyProperty.Register("MyText", typeof(string), typeof(MyTestUserControl), new PropertyMetadata("Hallo Welt (a simple default Property value from CodeBehind)"));
private static string Changer(string someString)
{
// This should happen wenn MyText changes, but never will be fired
return someString + " -- " + DateTime.Now.ToString();
}
}
}
Window
<Window x:Class="WpfUserControlTest.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:WpfUserControlTest"
xmlns:ucl="clr-namespace:WpfUserControlTestControlLibrary;assembly=WpfUserControlTestControlLibrary"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
x:Name="Test"
>
<Grid>
<!--DataContext="{Binding RelativeSource={RelativeSource Self}}"-->
<StackPanel>
<Button Content="Change Textblock" Click="Button_Click"/>
<TextBlock Text="AppStart"/>
<local:MyTestUserControl
Margin="10 20 0 0"
Background="Beige"
MyText="{Binding MyTextMaster, ElementName=Test}"/>
<local:MyTestUserControl
x:Name="DirectCalledControl"
Margin="10 20 0 0"
Background="Beige"
MyText="{Binding MyTextMaster, ElementName=Test}"/>
</StackPanel>
</Grid>
</Window>
namespace WpfUserControlTest
{
public partial class MainWindow : Window
{
int Counter = 0;
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MyTextMaster = "Number: " + Counter.ToString();
DirectCalledControl.MyText = MyTextMaster;
Counter++;
}
public string MyTextMaster
{
get { return (string)GetValue(MyTextMasterProperty); }
set { SetValue(MyTextMasterProperty, value); }
}
public static readonly DependencyProperty MyTextMasterProperty =
DependencyProperty.Register("MyTextMaster", typeof(string), typeof(MainWindow), new PropertyMetadata("Initial value from MasterView"));
}
}