0

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);
    }
laserman
  • 233
  • 2
  • 10
  • You have a Resource in the XAML with an empty Deutsch1Answer, which is what is bound to your TextBox. I don't see where you set a DataContext to the "new Answer()" you did at the very top. You probably only want one or the other. – Todd Sprang Oct 23 '15 at 12:32
  • My DataContext is set to my ViewModel. How do I set the DataContext to new Answer()? this.DataContext = _VokabelViewModel; – laserman Oct 23 '15 at 15:20
  • Check out the top 2 answers here if you want to set DataContext in XAML: https://stackoverflow.com/questions/4590446/how-do-i-set-a-viewmodel-on-a-window-in-xaml-using-datacontext-property. I personally prefer to set it in code, however: https://stackoverflow.com/questions/5824600/databind-from-xaml-to-code-behind – Todd Sprang Oct 23 '15 at 15:25
  • Thanks - but I still dont have a solution for my problem. I have set my DataContext in the code to my viewmodel. The Class Answer is separate from my viewmodel because it needs to be a dependency object. I have also posted part of my view model above. – laserman Oct 23 '15 at 16:05
  • Try this. 1. *Define* an xmlns in your `` in XAML. e.g. `` 2. *Replace* the `` with `` – Todd Sprang Oct 23 '15 at 16:13
  • Thanks - can I set multiple data contexts in the code behind as well? – laserman Oct 23 '15 at 16:27
  • DataContext can only be set once. If you need more data in your DataContext, you need to extend the viewmodel itself to represent more data. – Todd Sprang Oct 23 '15 at 16:28
  • OK understood. Then my real problem is how do I inherit both: INotifyPropertyChanged and DependencyObject for my Viewmodel? – laserman Oct 23 '15 at 16:34
  • You should not need to do both. The DO is superior to the alternative (https://stackoverflow.com/questions/291518/inotifypropertychanged-vs-dependencyproperty-in-viewmodel). However if you still want both for some reason it's just `class Answer : DependencyObject, INotifyPropertyChanged {...}`. – Todd Sprang Oct 23 '15 at 16:37
  • Ah - thanks - now I feel stupid - I tried to inherit the Interface first - that didn't work. Thanks again. – laserman Oct 23 '15 at 16:39

1 Answers1

1

If you want to access the Deutsch1Answer DependencyProperty in your ViewModel you should bind to it. However I dont really see the point in why you've got the "Answer"-class.

If you want to bind the textboxs text to a property in your view model you should add an Answer property and bind it directly to the textbox.

In your ViewModel:

private string m_answer;

public string Answer
{
  get { return m_answer; }
  set 
    { 
      m_answer = value;
      OnPropertyChanged("Answer");
    }
}

In your View:

...
<TextBox Grid.Column="1" Grid.Row="2" Name="BoxDeutsch1" Text="{Binding Answer}">
...
IGL
  • 26
  • 2