WPF: How do I bind with this behavior?

OneZeroOne 1 Reputation point
2020-03-19T13:03:54.717+00:00

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 Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,686 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Alex Li-MSFT 1,096 Reputation points
    2020-03-20T05:10:04.843+00:00

    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.

    0 comments No comments