将TextBlock绑定到MouseEventHandlers

时间:2015-08-10 03:58:00

标签: c# wpf xaml binding

我正在尝试将一个简单的字符串绑定到TextBlock,但它没有做任何事情。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.MouseMove += new MouseEventHandler(window_MouseMove);
    }

    public void window_MouseMove (object sender, MouseEventArgs e)
    {
        Point mouseXY = Mouse.GetPosition(this);

        string mouseXYstring = mouseXY.ToString();
    }
}

和XAML ......:

<TextBlock x:Name="MousePosition" HorizontalAlignment="Right"
VerticalAlignment="Top" Width="200" Text="{Binding Path=mouseXYstring}" />

也试过......:

<TextBlock x:Name="MousePosition" HorizontalAlignment="Right"
VerticalAlignment="Top" Width="200" Text="{Binding mouseXYstring}" />

要从window_MouseMove中删除'Text =“{Binding mouseXYstring}',但似乎没有任何效果:

    public void window_MouseMove (object sender, MouseEventArgs e)
    {
        Point mouseXY = Mouse.GetPosition(this);

        string mouseXYstring = mouseXY.ToString();

        TextBlock MP = new TextBlock();

        MP.Text = mouseXYstring;
    }

1 个答案:

答案 0 :(得分:2)

我尝试使用文本框,它的工作是我的代码

$this->session->sess_destroy();

但在您的情况下,您使用文本块并尝试此代码

//mousemove inside my Textbox
    private void TextBox_MouseMove_1(object sender, MouseEventArgs e)
    {
        //txt is the name oh my textbox
        txt.Text = e.GetPosition(this).ToString();
    }

    //mousemove in my windows
    private void wn_MouseMove(object sender, MouseEventArgs e)
    {
        //txt is the name oh my textbox
        txt.Text = e.GetPosition(this).ToString();
    }