禁用其他控件的复选框 [英] Disable checkbox from some other control
问题描述
继承的WPF应用程序。有UserControl,DockPanel,网格,复选框。工作得很好。但现在我需要启用或禁用另一个控件[或线程]的复选框。如何获得复选框的实例并能够设置IsEnabled?
Inherited WPF application. Has UserControl, DockPanel, Grid, Checkbox. Works just fine. But now I need to enable or disable the checkbox from another control [or thread]. How to gain an instance of the checkbox and be able to set IsEnabled?
我尝试保存"this"在UserControl的构造函数中,然后使用"this"访问复选框和IsEnabled,但是引发了"线程不拥有控件"例外。
I tried saving "this" in the constructor of the UserControl, then using "this" to access the checkbox and IsEnabled, but that threw a "thread does not own the control" exception.
推荐答案
"来自其他控件"是什么意思?
What do you mean by "from other control"?
一些其他用户控件?
控件是一个私有成员,所以你无法从其他用户控件获取它。
A control is a private member so you can't get at it from some other usercontrol.
或者至少没有做到一些非常狡猾的东西并使它成为一个公共对象。
Or at least not without doing something pretty dodgy and making it a public object.
和
UI控件具有线程亲和力,所以你需要dispatcher.invokeasync来回到ui线程,如果你在另一个。
UI controls have thread affinity so you need to dispatcher.invokeasync to get back to the ui thread if you're on a different one.
Application.Current.Dispatcher.InvokeAsync(new Action(() =>
{
// Do something with UI
}));
你还没有真正描述过什么能给你一个好的答案。
You haven't really described what's going on sufficiently to give you a good answer.
如果两个控件位于同一个datacontext中,则可能会使用绑定。将您的复选框的IsEnabled绑定到属性,并通过绑定来将其设置为false,而不是其他任何东西。
If the two controls are in the same datacontext you could potentially use binding. Bind IsEnabled of your checkbox to a property and set that to false via binding from your other-whatever-it-is.
或。
你可以告诉你的usercontrol使用mvvmlight messenger以解耦方式禁用或启用它的usercontrol。
You could tell your usercontrol to disable or enable it's usercontrol in a decoupled manner using mvvmlight messenger.
https://social.technet.microsoft.com/wiki/contents/articles/26070.communication -between-classes.aspx?Redirected = true
https://social.technet.microsoft.com/wiki/contents/articles/26070.communicating-between-classes.aspx?Redirected=true
这篇关于禁用其他控件的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!