user2027080
user2027080

Reputation: 125

MVVM - WPF: Updating view model boolean property when TextBox is being edited

I found a number of examples on how to select a control from the view model but must not be searching on the right terms on how to get it to work the other way around. Basically I just want to have a boolean value in my view model updated when a text box is being edited and when it is not. I assume this is the same as wanting to know when it gets and looses focus.

So I have created a FocusExtension class as described here: Set focus on textbox in WPF from view model (C#)

But it is not triggering the accessors to my view model property "EditingMyTargetField" with the following line in my XAML:

my:FocusExtension.IsFocused="{Binding EditingMyTargetField}"

Upvotes: 1

Views: 2170

Answers (2)

Vishal
Vishal

Reputation: 624

I modified my answer after going through your exact requirement.

You just need to use 2 events in this case when you want to enable your button when the mouse isOver the textbox and when its NOT.

Those 2 events are: MouseEnter and MouseLeave

These events capture the moment when the cursor is over the control and when its not.

And you need a bool property to bind to your Button inorder to change its Enablity according to the condition*(Mouse over or not)*.

private bool _isBtnEnable = false;

        public bool IsBtnEnable
        {
            get { return _isBtnEnable; }
            set
            {
                _isBtnEnable = value;
                OnPropertyChanged("IsBtnEnable");
            }
        }

And in Xaml:

  <Grid>
        <StackPanel Orientation="Horizontal">
            <TextBox Width="100" Text="{Binding 
Txt,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
 Mouse.MouseEnter="TextBox_MouseEnter" 
 Mouse.MouseLeave="TextBox_MouseLeave"/>
                <Button Width="70" Name="btn" Content="Save" Margin="20,0,0,0" 
IsEnabled="{Binding IsBtnEnable,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged
/>

        </StackPanel>

    </Grid>

And in Xaml.cs:

private void TextBox_MouseEnter(object sender, MouseEventArgs e)
        {
            IsBtnEnable = true;
        }

        private void TextBox_MouseLeave(object sender, MouseEventArgs e)
        { 

   if (string.IsNullOrEmpty(Txt)) (Checking if the TextBox is empty,remove it 
if regardless of text you want to disable the button on cursor being not over the
 TextBox but how will you click Save then because it will get disabled when you moved your mouse)
            IsBtnEnable = false;
        }

I think this will do the trick you want. :)

Upvotes: 0

Kevin Nacios
Kevin Nacios

Reputation: 2853

I would explore the other answers besides the top answer, as others have improved upon the approved solution. I tested Zamotic's FocusExtension with a textbox and it successfully triggered the binding back to the viewmodel. His solution has events that trigger when the element gains or loses focus, that then sets the dependencyproperty value to the right state. I also had to explicitly state the binding mode as TwoWay in the markup, but you could alter the extension to set twoway binding by default if you wanted.

<TextBox local:FocusExtension.IsFocused="{Binding TextIsFocused, Mode=TwoWay}"/>

Upvotes: 2

Related Questions