我为LineSymbol创建了一个ControlTemplate:
<esri:SimpleLineSymbol
x:Key="PolylineSymbol"
Width="3"
>
<esri:SimpleLineSymbol.ControlTemplate>
<ControlTemplate>
<Grid
>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
...
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
...
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Path
x:Name="Element" Fill="{x:Null}"
Stroke="Navy" StrokeThickness="3"
StrokeLineJoin="Round" StrokeStartLineCap="Round" StrokeEndLineCap="Round">
<Path.ContextMenu>
<ContextMenu
x:Name="popUpMenu"
DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type esri:Graphic}}}">
<MenuItem
x:Name="miSelect"
Header="Select"
IsCheckable="True"
IsChecked="{Binding Selected, FallbackValue=False}"
/>
...
</ContextMenu>
</Path.ContextMenu>
</Path>
</Grid>
</ControlTemplate>
</esri:SimpleLineSymbol.ControlTemplate>
</esri:SimpleLineSymbol>
除了IsChecked在“选择”菜单项上的绑定外,一切都运行良好:由于SymbolElement和Symbol都没有从FrameworkElement继承,因此没有任何绑定表达式会将我从此处带到此SimpleLineSymbol的包含图形。
我还尝试了一个Click事件(它给出了一个发送者,一个命令(支持一个参数)或一个MouseRightButtonDown事件(在图形上) - 没有方法从我路径上的右键单击点开始符号包含图形...
菜单的DataContext在VS2012的设计器中看起来没问题,但是在运行时它不起作用,因为Menu位于Symbol的ControlTemplate中定义的路径内,而不是FrameworkElement!
我已经为ContextMenu添加了一个名称,但我无法从ViewModel(我创建图形和符号的位置)中检索它;如果我能够这样做,我将能够添加所需的datacontext在代码中:
var graphic = new Graphic { Symbol = Resources["PolylineSymbol"] as SimpleLineSymbol;
var menu = graphic.Symbol.ControlTemplate.FindName("popUpMenu", graphic.Symbol); // ???
menu.DataContext = graphic;
)
有什么想法吗?
答案 0 :(得分:0)
如果我正确理解您的问题,似乎您在WPF中遇到了常见问题。解决方案是利用Tag
的{{1}}属性,使用ContextMenu.PlacementTarget
property将Path
“传递”到DataContext
。此获取或设置ContextMenu在打开时所处的UIElement 。试试这个:
ContextMenu
答案 1 :(得分:0)
解决方案是实现上下文菜单的Opened。在后面的代码中,我在其DataContext上分配了视图模型的实例。
<Path.ContextMenu>
<ContextMenu
Opened="PopUpMenu_OnOpened"
>
代码背后的代码:
private void PopUpMenu_OnOpened(object sender, RoutedEventArgs e)
{
var menu = sender as ContextMenu;
if (menu != null)
{
menu.DataContext = ViewModel;
}
}
另一个挑战是获取点击的图形和点击的点。 解决方案是在View模型中创建属性,并在LeftMouseDown和RightMouseDown上分配两者。
private Graphic GetPolylineGraphic(ESRI.ArcGIS.Client.Geometry.Geometry geometry = null)
{
var drawLayer = Model.GetDrawLayer(MyMap, "Polyline");
var graphic = new Graphic
{
// clone the resourced PolylineSymbol (from Model)
Symbol = new SimpleLineSymbol
{
Color = PolylineSymbol.Color,
Width = PolylineSymbol.Width,
ControlTemplate = PolylineSymbol.ControlTemplate
}
};
if (geometry != null) graphic.Geometry = geometry;
graphic.MouseLeftButtonDown += GraphicOnMouseLeftButtonDown;
graphic.MouseRightButtonDown += GraphicOnMouseRightButtonDown;
drawLayer.Graphics.Add(graphic);
return graphic;
}
private Graphic m_clickedGraphic;
public Graphic ClickedGraphic
{
get { return m_clickedGraphic; }
set
{
if (!Equals(m_clickedGraphic, value))
{
m_clickedGraphic = value;
OnPropertyChanged(value);
}
}
}
private MapPoint m_clickedPoint;
public MapPoint ClickedPoint
{
get { return m_clickedPoint; }
set
{
if (m_clickedPoint != value)
{
m_clickedPoint = value;
OnPropertyChanged(value);
}
}
}
private void GraphicOnMouseRightButtonDown(object sender, MouseButtonEventArgs args)
{
//// This does not work because GraphicElement is internal!!!
//var s = args.Source;
//ClickedGraphic = ((GraphicElement)(e.Source)).Graphic;
//ClickedPoint = ((GraphicElement)(e.Source)).Origin;
ClickedGraphic = sender as Graphic;
ClickedPoint = MyMap.ScreenToMap(args.GetPosition(MyMap));
//// not here - else context menu won't pop!
//args.Handled = true;
}
private void GraphicOnMouseLeftButtonDown(object sender, MouseButtonEventArgs args)
{
var g = sender as Graphic;
if (g != null)
{
ClickedGraphic = g;
ClickedPoint = MyMap.ScreenToMap(args.GetPosition(MyMap));
// select/unselect the graphic on left click
if (g.Selected) g.UnSelect();
else g.Select();
args.Handled = true;
}
}
为了使一切正常,我必须克隆符号。