开发者

How to know if a WPF user control gets enabled?

I have a user control (x:Name=UserControl1) which i have used in another control(x:Name=UserControl2). Now when a check box present in UserControl2 gets set then only UserControl1 get enabled. I want to set the focus to some particular text box present in UserControl1 when ever it get enabled. Is there any event or anything thru which UserControl1 gets to know tha开发者_JAVA百科t its enabled state is changed to 1?


Firstly, as the previous answer states, IsEnabled is a dependency property, so you can set up a binding to do something when that changes. You say "it is required so that it can set the focus to some particular control present in it", in this case you might want to set a binding on a property of the child control (eg IsFocused).

If you have some particular logic you need, you can always set up your own dependency property, bind that to IsEnabled, and set your custom logic in the changed handler. Or to do basically the same thing without setting up a fresh dependency property, you can add your own changed handler to IsEnabled with AddOwner:

UIElement.IsEnabledProperty.AddOwner(typeof(MyUserControl), new FrameworkPropertyMetadata(OnIsEnabledChanged));
public static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    ((MyUserControl)d).DoSomething();
}


The IsEnabled property of the UserControl class is a dependency property which means you can bind it to a property of your choice and hook into there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜