I would like to access the dependency property bound to a text box in the view model. In the view model I have the following code:
Answer _Answer = new Answer();
_Answer.GetValue(Answer.Deutsch1AnswerProperty);
Console.WriteLine(_Answer.Deutsch1Answer);
The dependency property is defined as follows:
public class Answer : DependencyObject
{
// Ereignis
public event EventHandler Deutsch1AnswerChanged;
public static readonly DependencyProperty Deutsch1AnswerProperty;
public String Deutsch1Answer
{
get { return (String)GetValue(Deutsch1AnswerProperty); }
set { SetValue(Deutsch1AnswerProperty, value); }
}
public static void OnDeutsch1AnswerChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
Answer _Answer = (Answer)sender;
if (_Answer.Deutsch1AnswerChanged != null)
_Answer.Deutsch1AnswerChanged(_Answer, null);
}
static Answer()
{
FrameworkPropertyMetadata meta = new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);
meta.PropertyChangedCallback = new PropertyChangedCallback(OnDeutsch1AnswerChanged);
Deutsch1AnswerProperty = DependencyProperty.Register("Deutsch1Answer", typeof(String), typeof(Answer), meta);
}
}
In the MainWindow.Xaml I have the follwoing code:
<Window.Resources>
<local:Answer x:Key="_Answer" Deutsch1Answer=string.Empty />
</Window.Resources>
<Grid Definitions ...>
<TextBox Grid.Column="1" Grid.Row="2" Name="BoxDeutsch1" Text="{Binding Source={StaticResource _Answer}, Path=Deutsch1Answer}">
</TextBox>
I cannot access the Text Property in the view model. Please help.
The view model looks like this:
public class VokabelViewModel : INotifyPropertyChanged
{
private Vokabel _Model;
public VokabelViewModel(Vokabel model)
{
_Model = model;
}
private ICommand _boxDeutsch1_HitEnter;
public ICommand BoxDeutsch1_HitEnter
{
get
{
return _boxDeutsch1_HitEnter ?? (_boxDeutsch1_HitEnter = new CommandHandler(() => MyActionBoxDeutsch1_HitEnter(), _canExecuteBoxDeutsch1_HitEnter));
}
}
private bool _canExecuteBoxDeutsch1_HitEnter;
public void MyActionBoxDeutsch1_HitEnter()
{
Answer _Answer = new Answer();
_Answer.GetValue(Answer.Deutsch1AnswerProperty);
_Model.TestVokabel(_Answer.Deutsch1Answer);
}