Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从代码隐藏到网格的WPF绑定_C#_Wpf - Fatal编程技术网

C# 从代码隐藏到网格的WPF绑定

C# 从代码隐藏到网格的WPF绑定,c#,wpf,C#,Wpf,我正在尝试查看WPF屏幕上鼠标单击的坐标,但没有运气。我有一个基本的网格布局,带有一个文本块,可以显示这些坐标。我之前已经将xaml中的值绑定到了一个代码后面,但不确定这个方向是否可行。我的xaml如下 <Window x:Class="MouseUpExample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.mic

我正在尝试查看WPF屏幕上鼠标单击的坐标,但没有运气。我有一个基本的网格布局,带有一个文本块,可以显示这些坐标。我之前已经将xaml中的值绑定到了一个代码后面,但不确定这个方向是否可行。我的xaml如下

<Window x:Class="MouseUpExample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    Name="MyWindow">
    <Grid>
        <TextBlock Text="{Binding Path=GetMouseCoordinates}"/>
    </Grid>
</Window>
    using System.Windows;
using System.Windows.Input;
using System.Windows.Shapes;

namespace MouseUpExample

{
    public partial class MainWindow : Window
{

    Point currentPoint = new Point();

    public MainWindow()
    {
        InitializeComponent();
    }

    private string GetMouseCoordinates(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        if (e.ButtonState == MouseButtonState.Pressed)
        {
            currentPoint = e.GetPosition(this);
            return currentPoint.ToString();
        }
        return "error";
    }
}

非常感谢您的帮助。

要能够绑定,您必须将DataContext设置为实现INotifyPropertyChanged的类,并绑定到其属性。由于您没有定义它,我将直接在视图中设置该文本

<Window x:Class="MouseUpExample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    Name="MyWindow" MouseDown="MainWindow_OnMouseDown">
    <Grid>
        <TextBlock Name="MyTextBlock"/>
    </Grid>
</Window>

你这里有很多问题

问题1是无法绑定到方法。它需要是一个属性,最好是
DependencyProperty
或参与
INotifyPropertyChanged
接口的属性

问题#2是默认情况下绑定与DataContext一起工作,但您没有显式或隐式地设置
TextBlock
DataContext

问题3是,这真的没有意义。
GetMouseCoordinates
是某个事件的事件处理程序吗?您可能希望拆分事件处理程序和属性


我建议您,然后再试一次。

您应该绑定属性而不是方法。通过绑定属性,我会绑定MouseButtonState.Pressed吗?获取值的字符串
private void MainWindow_OnMouseDown(object sender, MouseButtonEventArgs e)
{
    MyTextBlock.Text = e.GetPosition(this).ToString();
}