我有這種風格到ResourceDictionary中。這是一個ToggleButton的列表。我爲每個按鈕添加一個contextMenu,並且我想將視圖模型交給click事件。我將Editindicator方法放入viewmodel中。當我運行該項目時,我點擊了contextmenu項目,並且顯示此錯誤「{」沒有找到方法的目標單擊。「}」。我認爲這個錯誤是由於menuitem丟失了viewmodel的數據上下文。 任何人都可以在這裏幫忙嗎?非常感謝諮詢。Doesn't手MenuItem點擊進入viewmodel
<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>
我做到這一點,它doesn't工作:<文本菜單的DataContext = 「{結合PlacementTarget,的RelativeSource = {的RelativeSource自}}」> – user998678
解決方案: [WPF]如何綁定到數據時在DataContext不被繼承 http://tomlev2.wordpress.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/ – user998678
是的,你可以使用這裏的代理綁定。 –