检测何时选择设计表面上的控件

Tim*_*Tim 5 c# wpf controls expression-blend

我正在写一个Expression Blend 4 Extension,我想在选择设计图面上的Control或Element时检测(在我的扩展名中).有人能告诉我如何检测它吗?蒂姆,谢谢

Sor*_*oot 0

我继续了有关编写扩展的教程。当您查看该项目的示例代码时,下面的代码应该很清楚。

当活动文档更改时,将调用下面的第一个方法。此方法处理IDocumentServiceActiveDocumentChanged事件。首先,它从调色板注册表中获取TimelinePane的内容。此内容中包含ActiveSceneViewModel。ActiveSceneViewModel 是包含活动场景(= 当前正在编辑的 xaml 文件)的视图模型。ActiveSceneViewModel 包含一组选定的元素,即 ElementSelectionSet。其中有一个事件(Changed),当它被更改时会被触发。处理这个事件。

在此事件处理程序中,您可以在更改后立即访问选择集。

private void ActiveDocumentChanged(object sender, DocumentChangedEventArgs e)
{
    var timelinePane = 
         (TimelinePane)WindowService.PaletteRegistry["Designer_TimelinePane"].Content;
    _activeSceneViewModel = timelinePane.ActiveSceneViewModel;
    _activeSceneViewModel.ElementSelectionSet.Changed += 
         new System.EventHandler(ElementSelectionSet_Changed);

    //some other goes here....
}

void ElementSelectionSet_Changed(object sender, System.EventArgs e)
{
    SceneElementSelectionSet selectionSet 
        = sender as SceneElementSelectionSet;
    // get the selected elements from the selection set
}
Run Code Online (Sandbox Code Playgroud)