Thursday, 15 August 2013

Dependency property not set in OnOk

Dependency property not set in OnOk

If I edit a text in a TextBox and then press enter in order to fire the
Click event of the default key, I don't get the correct value in the Click
handler.
How can I get this value in my handler?
Here is my xaml:
<Window x:Class="WpfApplication1.DefaultKeyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DefaultKeyWindow" Height="300" Width="300">
<StackPanel>
<TextBox Text="{Binding test}" />
<Button Click="OnOk" IsDefault="True">OK</Button>
</StackPanel>
</Window>
And here my code behind:
public partial class DefaultKeyWindow : Window
{
public string test
{
get { return (string)GetValue(testProperty); }
set { SetValue(testProperty, value); }
}
// Using a DependencyProperty as the backing store for test. This
enables animation, styling, binding, etc...
public static readonly DependencyProperty testProperty =
DependencyProperty.Register("test", typeof(string),
typeof(DefaultKeyWindow), new PropertyMetadata("initial value"));
public DefaultKeyWindow()
{
InitializeComponent();
this.DataContext = this;
}
private void OnOk(object sender, RoutedEventArgs e)
{
MessageBox.Show("Value of test is " + test);
DialogResult = true;
}
}

No comments:

Post a Comment