我将这种风格融入ResourceDictionary。这是一个ToggleButton的列表。我为每个按钮添加了一个contextMenu,我想将click事件交给viewmodel。我有方法Editindicator进入viewmodel。当我运行项目并单击上下文菜单项时,它会断开并显示此错误“{”找不到方法的目标点击。“}”。我认为这个错误是由于menuitem丢失了viewmodel的datacontext。 有人可以帮忙吗?非常感谢您的建议。
<Style x:Key="ListBoxStyleIndicador" TargetType="{x:Type ListBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Padding" Value="2,0,2,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate x:Name="ListBoxStyleIndicadorTemplate" TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="true">
<Controles:ToggleButtonIndicador
Content="{Binding NombreIndicador}"
IdBIIndicadores="{Binding IdBiIndicadores}"
IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"
Style="{DynamicResource BotonNegro}"
Padding="6,2"
ToolTip="{Binding Descripcion}">
<Controles:ToggleButtonIndicador.ContextMenu >
<ContextMenu>
<MenuItem Header="Editar">
<MenuItem.Icon>
<Image Source="{DynamicResource ImagenBotonEditar}" />
</MenuItem.Icon>
<Interactivity:Interaction.Triggers>
<Interactivity:EventTrigger EventName="Click">
<cal:ActionMessage MethodName="EditIndicator" />
</Interactivity:EventTrigger>
</Interactivity:Interaction.Triggers>
</MenuItem>
</ContextMenu>
</Controles:ToggleButtonIndicador.ContextMenu>
</Controles:ToggleButtonIndicador>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="IsSelected" Value="{Binding Seleccionado, Mode=TwoWay}"/>
</Style>
答案 0 :(得分:0)
由于ContextMenu不是Visual树的一部分,因此您不会引用为您的UserControl设置的ViewModel的DataContext。有两种方法可以为控件获取datacontext -
Set explicitly dataContext for your ContextMenu using the PlacementTarget
。请看我的答案 - cannot set tooltip in style
Use Proxy binding.
请在此处查看我的回答 - Binding from items in ItemsControl to ItemControl's DataContext
编辑:对于DataContext,您可以这样做 -
<Controles:ToggleButtonIndicador Tag="{Binding DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}>
<Controles:ToggleButtonIndicador.ContextMenu >
<ContextMenu DataContext={Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}>
<MenuItem Header="Editar">
<MenuItem.Icon>
<Image Source="{DynamicResource ImagenBotonEditar}" />
</MenuItem.Icon>
<Interactivity:Interaction.Triggers>
<Interactivity:EventTrigger EventName="Click">
<cal:ActionMessage MethodName="EditIndicator" />
</Interactivity:EventTrigger>
</Interactivity:Interaction.Triggers>
</MenuItem>
</ContextMenu>
</Controles:ToggleButtonIndicador.ContextMenu>
</Controles:ToggleButtonIndicador>
我抱怨你的viemodel绑定到UserControl的DataContext属性。
答案 1 :(得分:0)
如果它可以帮助任何人。在使用以下示例代码之前,强烈建议您阅读CodeProject上的Attaching a Virtual Branch to the Logical Tree in WPF。你会知道问题出现的原因以及如何优雅地解决问题。
以下是快速示例。
// Add a DataContextBridge.
<UserControl.Resources>
<FrameworkElement x:Key="DataContextBridge" />
</UserControl.Resources>
// Bind.
<UserControl.DataContext>
<Binding
Mode="OneWayToSource"
Path="DataContext"
Source="{StaticResource DataContextBridge}" />
</UserControl.DataContext>
// Trigger a click event.
<ContextMenu>
<MenuItem>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction
TargetObject="{Binding Source={StaticResource DataContextBridge}, Path=DataContext}"
MethodName="OnClick" />
</i:EventTrigger>
</i:Interaction.Triggers>
</MenuItem>
</ContextMenu>
感谢。