简体   繁体   English

如何在不进行数据绑定的情况下从另一个用户控件访问控件?

[英]How to access controls from another user control without data binding?

I want to toggle a rectangle's visibility from another user control. 我想从另一个用户控件切换矩形的可见性。 I believe my current code isn't working because I'm creating a new instance of the first user control wherein I should be referencing from the old one instead. 我相信我当前的代码无法正常工作,因为我正在创建第一个用户控件的新实例,而我应该从旧实例中进行引用。 Unfortunately, I don't know how to make that reference. 不幸的是,我不知道该如何做。

User Control 1: 用户控件1:

public one()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Window window = new Window
        {
            Title = "Second User Control",
            Content = new two(),
            WindowStartupLocation = WindowStartupLocation.CenterScreen,
            ResizeMode = ResizeMode.NoResize
        };
        window.ShowDialog();
    }

User Control 2: 用户控件2:

one oneUC;
public two()
    {
        InitializeComponent();
        oneUC = new one();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        oneUC.rectangleControl.Visibility = Visibility.Hidden;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        oneUC.rectangleControl.Visibility = Visibility.Visible;
    }

Concerns: 关注:

  1. I understand it's not a good practice to do this but I'm only using wpf to create this very simple personal project. 我知道这样做不是一个好习惯,但是我只是使用wpf来创建这个非常简单的个人项目。 After this small project is done, I'm done with wpf as well. 完成这个小项目后,我也将使用wpf。
  2. Without data binding 没有数据绑定

User Control 1: 用户控件1:

private void Button_Click(object sender, RoutedEventArgs e)
    {
    two tw = new two();
    tw.oneUC = this;
        Window window = new Window
        {
            Title = "Second User Control",
            Content = tw,
            WindowStartupLocation = WindowStartupLocation.CenterScreen,
            ResizeMode = ResizeMode.NoResize
        };
        window.ShowDialog();
    }

User Control 2: 用户控件2:

public two()
    {
        InitializeComponent();
    }

In User Control 1 you need create User Control 2 and set User Control 1 into oneUC variable. 在用户控件1中,您需要创建用户控件2并将用户控件1设置为oneUC变量。 In User Control 2 constructor you have to remove oneUC = new one(); 在User Control 2构造函数中,您必须删除oneUC = new one();。 It will work for you. 它将为您工作。

Dirty Version 脏版

Create a singleton class that will have access to all User Controls, eg.: 创建一个可以访问所有用户控件的单例类,例如:

public static class Container 
{
    public static UserControl1 Control1 {get;set;}
    public static UserControl2 Control2 {get;set;}
}

In form constructor (after InitializeComponent()) assing your controls to singleton variables like this: 在表单构造函数中(在InitializeComponent()之后),将控件赋给单例变量,如下所示:

Container.Control1 = control1;
Container.Control2 = control2;

Then in UserControl2 you can do following: 然后,在UserControl2中,您可以执行以下操作:

 private void Button_Click(object sender, RoutedEventArgs e)
 {
     Container.Control1.rectangleControl.Visibility = Visibility.Hidden;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM