【发布时间】:2015-05-05 06:27:21
【问题描述】:
我有自定义控件 (FooCtrl),它有 4 个 DependencyProperties(Q1、Q2、Q3 和 Q4)。
public partical class FooCtrl : UserControl {
// ...
// Q1:
public static readonly DependencyProperty Q1Property =
DependencyProperty.Register("Q1", typeof(UIElement), typeof(FooCtrl), new UIPropertyMetadata(null));
public UIElement Q1
{
get { return (UIElement)GetValue(Q1Property); }
set { SetValue(Q1Property, value); }
}
// Q2:
public static readonly DependencyProperty Q2Property =
DependencyProperty.Register("Q2", typeof(UIElement), typeof(FooCtrl), new UIPropertyMetadata(null));
public UIElement Q2
{
get { return (UIElement)GetValue(Q2Property); }
set { SetValue(Q2Property, value); }
}
// Q3:
// ....
// Q4:
// ....
public void DoSomething()
{
// ...
}
}
我使用这样的控件:
<my:FooCtrl>
<my:FooCtrl.Q1>
<ContentControl Content="{Binding ...}" />
</my:FooCtrl.Q1>
<my:FooCtrl.Q2>
<ContentControl Content="{Binding ...}" />
</my:FooCtrl.Q2>
<my:FooCtrl.Q3>
<ContentControl Content="{Binding ...}" />
</my:FooCtrl.Q3>
<my:FooCtrl.Q4>
<ContentControl Content="{Binding ...}" />
</my:FooCtrl.Q4>
我可以向 FooCtrl Q1、Q2、Q3 和 Q4 属性添加 4 个用户控件。 通过 FooCtrl 上的“DoSomething()”方法,我可以更改动画 Q1、Q2、Q3 和 Q4:
myFooCtrl.DoSomething();
如何从我的用户控件(通过 Q1、Q2、Q3、Q4 附加)内部的 FooCtrl "DoSomething()" 访问方法? 例如如果我将用户控件绑定到 Q1 - Q4 的内容控件并且用户控件包含一个按钮“执行它!”这如何从 FooCtrl 访问“DoSomething()”以便执行?
如果有帮助:我也在使用 Caliburn.Micro。
【问题讨论】:
标签: c# wpf mvvm user-controls caliburn.micro