嗨,我是新的WPF开发和遇到了一个问题的鼠标绑定的椭圆,这是创建的项目控件,通过数据绑定。这是源代码。我的问题是"CLoadModelFromDisk“绑定没有执行。在另一种情况下,命令的工作没有任何问题。
<ItemsControl ItemsSource="{Binding JointsModelPartView}" Grid.Row="1" Grid.Column="1">
<ItemsControl.ItemContainerStyle>
<Style TargetType="FrameworkElement">
<Setter Property="Canvas.Left" Value="{Binding posx1}" />
<Setter Property="Canvas.Top" Value="{Binding posy1}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="Point">
<Ellipse Fill="AntiqueWhite"
Stroke="Black"
Width="10"
Height="10"
Margin="-5,-5,5,5"
>
<Ellipse.InputBindings>
<MouseBinding
Command="{Binding CLoadModelFromDisk}"
Gesture="LeftClick"
/>
</Ellipse.InputBindings>
</Ellipse>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>如果有人能帮我解决这个问题,那就太好了。
这是不适用于您的解决方案的上下文菜单的代码。你知道为什么吗?
<Ellipse.ContextMenu>
<ContextMenu ItemsSource="{Binding DataContext.ContextActionsView, RelativeSource={RelativeSource AncestorType=ItemsControl}}">
<ContextMenu.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header" Value="{Binding Name}"/>
<Setter Property="Command" Value="{Binding RCommand}"/>
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
</Ellipse.ContextMenu>我已经尝试将相对源设置为Header和Command,但这也不起作用。
发布于 2016-06-06 12:49:03
在您的DataTemplate中,DataContext将是模板中显示的项-- JointsModelPartView中的项目之一。这些项是否具有名为ICommand的CLoadModelFromDisk类型的属性?
我猜它们不是,CLoadModelFromDisk是与JointsModelPartView相同的视图模型的成员。在这种情况下,您需要绑定的不是列表项,而是属性实际所属的父视图模型。这将是DataContext of ItemsControl --这是必然的,因为ItemsControl能够绑定到JointsModelPartView。
试试这个:
<MouseBinding
Command="{Binding DataContext.CLoadModelFromDisk, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
Gesture="LeftClick"
/>https://stackoverflow.com/questions/37657226
复制相似问题