question

OneZeroOne-7553 avatar image
0 Votes"
OneZeroOne-7553 asked ·

WPF: How do I bind with this behavior?

Here is an image to help clarify my question.

4992-devenv-jsuauoulmt.png



Three TextBoxes and a TextBlock. I want the TextBlock to mirror the contents of one TextBox, but, if I mouse over another TextBox the TextBlock will mirror the Text of that TextBox instead. Mousing over a TextBox is just an arbitrary way to choose one TextBox over another. I have one requirement though and that is that the TextBlock be bound to a VM property and never directly to the Text property of a TextBox.

Any help appreciated,
Thanks.

windows-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AlexLi-MSFT avatar image
0 Votes"
AlexLi-MSFT answered ·

Hi,

Welcome to our Microsoft Q&A platform!

I think you better use MouseEnter event instead of IsMouseOver:

Xaml:

 <TextBox  Height="20" Width="100" Margin="20" MouseEnter="TextBox_MouseEnter" />
             <TextBox  Height="20" Width="100" Margin="20" MouseEnter="TextBox_MouseEnter"/>
             <TextBox  Height="20" Width="100" Margin="20" MouseEnter="TextBox_MouseEnter"/>

C# code:

    private void TextBox_MouseEnter(object sender, MouseEventArgs e)
         {
            textBlock1.Text =(sender as TextBox).Text;
         }

Thanks.


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.