在转换器中使用类实例的变量?

发布于 2024-10-07 00:15:37 字数 669 浏览 0 评论 0原文

我有一些代码如下:

 public partial class MainWindow : Window
 {
  public bool Adam = true;

  public MainWindow()
  {
   InitializeComponent();
  }

  public class NextEnabled : IValueConverter
  {
   public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
   {

    return Adam;
   }

   public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
   {
    return true;
   }
  }
 }

我希望我的转换器返回的是 Adam 的值。我知道它现在不起作用,因为转换器类没有对 MainWindow 实例的引用。在 XAML 中,主窗口名为“window_main”,我想引用此实例 - 但不能。

有什么办法可以做到这一点吗?使用 return window_main.Adam; 也不起作用 - 它无法识别此实例。

I have some code as follows:

 public partial class MainWindow : Window
 {
  public bool Adam = true;

  public MainWindow()
  {
   InitializeComponent();
  }

  public class NextEnabled : IValueConverter
  {
   public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
   {

    return Adam;
   }

   public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
   {
    return true;
   }
  }
 }

What I want my converter to return is the value of Adam. I understand that it doesn't work right now because the converter class doesn't have a reference to an instance of MainWindow. In the XAML, the main window is named "window_main", and I want to reference this instance - but can't.

Is there any way to do this? Using return window_main.Adam; does not work either - it doesn't recognize this instance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぃ双果 2024-10-14 00:15:37

如果您仅使用主窗口的单个实例,那么将其设为全局怎么样?当然,这不是最好的方法,但我确实想不出任何其他解决方案来解决您的问题。

public static class Globals
{
    public static MainWindow MainWindow;
}

public partial class MainWindow : Window
{
    public bool Adam = true;

    public MainWindow()
    {
        Globals.MainWindow = this;
        InitializeComponent();
    }

    public class NextEnabled : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Globals.MainWindow;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return true;
        }
    }
}

If you are using only a single instance of your main window, how about making it global? Of course, this is not the best approach, but I truly can't think of any other solution for your problem.

public static class Globals
{
    public static MainWindow MainWindow;
}

public partial class MainWindow : Window
{
    public bool Adam = true;

    public MainWindow()
    {
        Globals.MainWindow = this;
        InitializeComponent();
    }

    public class NextEnabled : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Globals.MainWindow;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return true;
        }
    }
}
一百个冬季 2024-10-14 00:15:37

像这样创建 NextEnabled

  public class NextEnabled : IValueConverter
  {
   protected Window window_main;
   public NextEnabled(Window w) { window_main = w; }

   public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
   {

    return window_main.Adam;
   }

   public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
   {
    return true;
   }
  }

您必须在创建 NextEnabled 时调用这个新的构造函数。

Make NextEnabled like this

  public class NextEnabled : IValueConverter
  {
   protected Window window_main;
   public NextEnabled(Window w) { window_main = w; }

   public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
   {

    return window_main.Adam;
   }

   public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
   {
    return true;
   }
  }

You have to call this new constructor when you create NextEnabled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文