我正在窗口中使用我的usercontrol。当window的StateChanged事件触发时,我想在usercontrol中做一些事情。
我想将statechanged事件发送到usercontrol。
我该怎么做?
答案 0 :(得分:1)
选项1:
UserControl
代码中定义一个可以调用以通知事件发生的公共方法StateChanged
代码中的Window
事件,并在UserControl
选项2:
StateChanged
事件实施界面,并在Window
DependencyProperty
UserControl
Window
UserControl
StateChanged
UserControl
事件
一些代码用于演示如何实现和使用选项2:
public interface IStateChanged
{
event EventHandler StateChanged;
}
public partial class MainWindow : Window, IStateChanged
{
public MainWindow()
{
InitializeComponent();
}
}
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
}
public IStateChanged StateChangedHost
{
get { return (IStateChanged)GetValue(StateChangedHostProperty); }
set { SetValue(StateChangedHostProperty, value); }
}
// Using a DependencyProperty as the backing store for StateChangedHost. This enables animation, styling, binding, etc...
public static readonly DependencyProperty StateChangedHostProperty =
DependencyProperty.Register("StateChangedHost", typeof(IStateChanged), typeof(MyUserControl), new FrameworkPropertyMetadata(null, StateChangedHost_PropertyChanged));
private static void StateChangedHost_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var self = d as MyUserControl;
if (e.OldValue != null)
{
((IStateChanged)e.OldValue).StateChanged -= self.NotifyStateChanged;
}
if (e.NewValue != null)
{
((IStateChanged)e.NewValue).StateChanged += self.NotifyStateChanged;
}
}
private void NotifyStateChanged(object sender, EventArgs e)
{
// implementation logic on StateChanged event
}
}
<Window [...]>
<Grid>
<local:MyUserControl StateChangedHost="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"/>
</Grid>
</Window>
答案 1 :(得分:1)
在用户控件中添加一个方法以通知它
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void MainWindow_StateChanged(object sender, EventArgs e)
{
myUserControl.StateChanged();
}
}
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
}
public StateChanged()
{
...
}
}
<Window [...]>
<Grid>
<local:MyUserControl x:Name="myUserControl"/>
</Grid>
</Window>
答案 2 :(得分:1)
这应该有效
public partial class MyUserControl : UserControl
{
public MyUserControl ()
{
InitializeComponent();
DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(Window.WindowStateProperty, typeof(Window));
dpd.AddValueChanged(Application.Current.MainWindow, (s, e) =>
{
//your code
});
}
}
基本上它告诉用户控件观察WindowsStateProperty,并且只要状态发生变化就会运行
答案 3 :(得分:0)
我认为,这不是正确的方法。
我的代码看起来像这样:
public class MyUserControl : UserControl{
event EventHandler ParentWindowStateChanged;
public void RaiseParentWindowStateChanged(Window sender){
this.ParentWindowStateChanged?.Invoke(sender, EventArgs.Empty);
}
}
在Window.StateChanged上你可以调用myUserControl。 RaiseParentWindowStateChanged(本)。
在UserControl的构造函数中,您可以为事件ParentWindowStateChanged添加处理程序,如
MyUserControl(){
this. ParentWindowStateChanged += (sender, args) => {
// do something
};
此致
斯特芬