2

i have the following code:

XAML Snippet:

<TextBox HorizontalAlignment="Left" Height="23" Margin="10,57,0,0" TextWrapping="Wrap" Text="{Binding Name}" VerticalAlignment="Top" Width="140">
   <TextBox.DataContext>
      <ViewModels:FilterViewModel/>
   </TextBox.DataContext>
</TextBox>
<Button Content="Filtern" HorizontalAlignment="Left" Margin="420,57,0,0" VerticalAlignment="Top" Width="75" Command="{Binding FilterButton}" CommandParameter="{Binding Filter}">
   <Button.DataContext>
      <ViewModels:FilterViewModel/>
   </Button.DataContext>
</Button>

FilterViewModel.cs:

class Button : ICommand
{
    public delegate void ICommandOnExecute(object parameter);
    public delegate bool ICommandOnCanExecute(object parameter);

    private ICommandOnExecute _execute;
    private ICommandOnCanExecute _canExecute;

    public Button(ICommandOnExecute onExecuteMethod, ICommandOnCanExecute onCanExecuteMethod)
    {
        _execute = onExecuteMethod;
        _canExecute = onCanExecuteMethod;
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute.Invoke(parameter);
    }

    public void Execute(object parameter)
    {
        _execute.Invoke(parameter);
    }
}

class FilterViewModel
{
    public ICommand FilterButton { get; set; }

    public FilterViewModel()
    {
        this.FilterButton = new Button(FilterExecute, canExecute);
    }

    public bool canExecute(object parameter)
    {
        return true;
    }

    public void FilterExecute(object paramter)
    {
        Console.WriteLine("Test: " + name);
    }

    private String name;
    public String Name
    {
        get
        {
            return name;
        }
        set
        {
            Console.WriteLine(value);
            name = value;
        }
    }
}

So when i click the button i want the content of the textbox printed to the console. e.g. input = "123" -> output should be "Test: 123". However it doesn't matter what i am writing into the textbox, the result is always only the output: "Test: ". The value of the property "name" is ignored completely.

Thanks for your help!

3
  • 1
    don't make multiple instances of view model. you change Name in the first view model, but trigger command from the second - where Name is null Commented Jul 9, 2018 at 12:33
  • 1
    In other words, do not explicitly set the DataContext of the TextBox and the Button. Instead, set the MainWindow's DataContext once. The DataContext value is then inherited by all child elements. Commented Jul 9, 2018 at 12:37
  • that absolutely makes sense! thank you. should had been clear from the start. Commented Jul 9, 2018 at 12:40

1 Answer 1

2

You seem to be working with two instances of the view model. Move the definition to the MainWindow (or similar top-level parent) containing the TextBox and the Button. The DataContext value is then inherited by all child elements.

<Window.DataContext>
   <ViewModels:FilterViewModel />
</Window.DataContext>

Then you can remove the definitions for the TextBox and the Button.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.