在转换器中使用类实例的变量?
我有一些代码如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您仅使用主窗口的单个实例,那么将其设为全局怎么样?当然,这不是最好的方法,但我确实想不出任何其他解决方案来解决您的问题。
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.
像这样创建 NextEnabled
您必须在创建 NextEnabled 时调用这个新的构造函数。
Make NextEnabled like this
You have to call this new constructor when you create NextEnabled.