Skip to main content
 首页 » 编程设计

c#之在 WPF 中的 MainWindow 和自定义类之间进行通信

2024年02月13日10shanyou

MainWindow 与 WPF 中的其他自定义类之间进行通信的最佳方式是什么?

假设我们有这个结构。

- MainWindow.xaml 
    MainWindow.xaml.cs 
 
- MyClass.cs 

我在 MainWindow.xaml 中使用了 Class1.cs,如下所示:

<drawings:MyClass> 
   . 
   . 
   . 
</drawings:MyClass> 

在 MyClass 中,我有一个名为 X 的变量,现在从 MainWindow.xaml.cs 访问此变量的最佳方法是什么?

我知道我在这里遗漏了一些关于面向对象编程的非常明显的东西,我只是不知道它是什么。

更新: 这是我到目前为止放在一起的代码,但它不起作用。并且 TextBlock 的值不会改变。

XAML:

<Window x:Class="CustomClasses.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:customclasses="clr-namespace:CustomClasses" 
        Title="MainWindow" Height="350" Width="525"> 
    <Window.DataContext> 
        <customclasses:MyClass></customclasses:MyClass> 
    </Window.DataContext> 
    <Grid> 
        <Grid.RowDefinitions> 
            <RowDefinition></RowDefinition> 
            <RowDefinition Height="150"></RowDefinition> 
        </Grid.RowDefinitions> 
        <customclasses:MyClass Width="525" Height="350" Background="Blue"> 
        </customclasses:MyClass> 
        <StackPanel Grid.Row="1"> 
            <TextBlock Text="{Binding X, Mode=TwoWay}"></TextBlock> 
        </StackPanel> 
    </Grid> 
</Window> 

我的类(class):

using System.Windows.Controls; 
using System.Windows.Input; 
 
namespace CustomClasses 
{ 
    class MyClass : Border 
    { 
        public double X { get; set; } 
 
        public MyClass() 
        { 
            MouseMove += MyMouseMove; 
        } 
 
        private void MyMouseMove(object sender, MouseEventArgs e) 
        { 
            var b = (Border) sender; 
            X = Mouse.GetPosition(b).X; 
        } 
    } 
} 

请您参考如下方法:

如果您有兴趣访问您在 Grid 中声明的实例的 X,即

<customclasses:MyClass Width="525" Height="350" Background="Blue"> 
</customclasses:MyClass> 

我会说在这个实例上设置 x:Name 然后你可以通过这个名字在代码后面访问它。

<customclasses:MyClass x:Name="myClass" Width="525" Height="350" 
                       Background="Blue"/> 

在后面的代码中,您可以像这样访问它:

double value = myClass.X; 

如果有兴趣为 DataContext 实例获取 X,即

<Window.DataContext> 
    <customclasses:MyClass></customclasses:MyClass> 
</Window.DataContext> 

你可以像这样在代码背后获得值(value):

double value = ((MyClass)DataContext).X; 

更新

首先声明为DataContext 并在Grid 下定义的所有MyClass 是两个独立的实例。删除 DataContext 并使用 ElementName 与在 Grid 内声明的实例进行绑定(bind)。

XAML

<Grid> 
    <Grid.RowDefinitions> 
        <RowDefinition></RowDefinition> 
        <RowDefinition Height="150"></RowDefinition> 
    </Grid.RowDefinitions> 
    <local:MyClass x:Name="myClass" Width="525" Height="350" Background="Blue"/> 
    <StackPanel Grid.Row="1"> 
        <TextBlock Text="{Binding X, ElementName=myClass}"></TextBlock> 
    </StackPanel> 
</Grid> 

第二 UI 更新类 MyClass 应该实现 INotifyPropertyChanged这样 UI 属性中的任何更改都会反射(reflect)到 UI 上。

我的类(class):

class MyClass : Border, INotifyPropertyChanged 
{ 
    private double x; 
    public double X 
    { 
        get { return x; } 
        set 
        { 
            if (x != value) 
            { 
                x = value; 
                RaisePropertyChanged("X"); 
            } 
        } 
    } 
 
    public MyClass() 
    { 
        MouseMove += MyMouseMove; 
    } 
 
    private void MyMouseMove(object sender, MouseEventArgs e) 
    { 
        var b = (Border)sender; 
        X = Mouse.GetPosition(b).X; 
    } 
 
    public event PropertyChangedEventHandler PropertyChanged; 
    private void RaisePropertyChanged(string propertyName) 
    { 
        var handler = PropertyChanged; 
        if (handler != null) 
        { 
            handler(this, new PropertyChangedEventArgs(propertyName)); 
        } 
    } 
}