I am using OneWayToSource
binding and it seems that it always sets my source property to null. Why is that so? It's causing me troubles because I need value from the target property in my source property and not null.
Here is my code:
MyViewModel.cs:
public class MyViewModel
{
private string str;
public string Txt
{
get { return this.str; }
set { this.str = value; }
}
}
MainWindow.cs:
public MainWindow()
{
InitializeComponent();
MyViewModel vm = new MyViewModel();
vm.Txt = "123";
this.DataContext = vm;
}
MainWindow.xaml:
<Window x:Class="OneWayToSourceTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:local="clr-namespace:OneWayToSourceTest">
<Grid>
<local:MyButton Content="{Binding Path=Txt, Mode=OneWayToSource}"/>
</Grid>
</Window>
MyButton.cs:
public class MyButton : Button
{
public MyButton()
{
this.Content = "765";
}
}
The target property is MyButton.Content
. The source property is MyViewModel.Txt
. The Txt
property should be set to "765" but instead it is null.
Why do I receive null instead 765?
EDIT:
Please take a look inside MyButton
constructor. Actually If you would use simple TwoWay
it will work. I tested it out and it has nothing to do with content being set inside constructor. Its something with OneWayToSource
binding I guess.
Now to explain how I used TwoWay
binding, I did set the value of dp inside the constructor by calling setvalue
method but then inside the wrapper or better said the getter and setter I didn't offer any setter hence why I made my TwoWay
kinda look like its OneWayToSource
. I did it to test out if its constructor fault. I figured the property inside viewmodel had the value 765 so that's what I meant with TwoWay
binding. I just tested if it was control constructor. Its all fine with setting a value inside the constructor.
By hiding setter I mean this set {}