我有一长串用户控件,其中一些具有自定义控件。在标准MVVM方式中,我有一个按钮绑定到viewmodel:
<Button Content="+" Grid.Column="2" Command="{Binding InsertCharacter}" />
此按钮的viewmodel现在将事件引发为:
private DelegateCommand insertCharacter;
public ICommand InsertCharacter
{
get
{
if (insertCharacter == null)
{
insertCharacter = new DelegateCommand(
() =>
{
InsertCharacterEventArgs e = new InsertCharacterEventArgs
{
InsertPosition = 1,
LengthOfInsertion= 20,
CharacterToInsert = 'C'
};
OnInsertCharacter(e);
});
}
return insertCharacter;
}
}
// Raise the InsertCharacter event.
public void OnInsertCharacter(InsertCharacterEventArgs e)
{
EventHandler<InsertCharacterEventArgs> handler = InsertCharacterEvent;
if (handler != null)
{
handler(this, e);
}
}
public event EventHandler<InsertCharacterEventArgs> InsertCharacterEvent;
现在有几层,我有一个CustomControl我希望收听这个事件。 custom控件由xaml从其usercontrol实例化。自定义控件生成的可视化树是:
CustomInkCanvas <-- My CustomControl
Grid
ScrollContentPresenter
Grid
ScrollViewer
Canvas
Grid
ContentPresenter
Border
InkEditorView <--InkEditorView
因此,当按钮放在InkEditorView.xaml上,并且从InkEditorViewModel.cs引发相应的事件时,如何让自定义控件CustomInkCanvas监听事件?
对此有任何帮助将非常感激。