Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing state information from view to viewmodel

Tags:

mvvm

wpf

What is the best approach to pass the current view model state - specifically what textbox has focus - back to the view model?

My requirement is for context specific search, depending what text box has focus determines what field to search on in the database.

I'm using the MVVM pattern and really don't want to put any code in the view.

like image 416
Rob Avatar asked Nov 18 '25 09:11

Rob


2 Answers

create an attached property for the TextBox which is an IsFocussed property. Then use 2 way binding to your ViewModel

like image 127
Dean Chalk Avatar answered Nov 20 '25 01:11

Dean Chalk


You could use the InvokeCommandAction available in the Expression Blend SDK:

    <StackPanel>
    <TextBox>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="GotFocus">
                <i:InvokeCommandAction Command="{Binding YourCommand}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>
    </StackPanel>

Where:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
like image 28
Gene C Avatar answered Nov 20 '25 01:11

Gene C