我有一个ContextMenu
,它被定义为ItemsControl.ItemTemplate -> DataTemplate
的一部分。问题是我无法在后面的代码中获得Click
事件通知。以下是我的代码的简化版本:
型号:
public class RunYear
{
public RunYear(int year)
{
Year = year;
Months = new Month[3];
}
public int Year { get; set; }
public Month[] Months { get; set; }
}
public class Month
{
public int ColumnIndex { get; set; }
public string MonthName { get; set; }
// some other props
}
XAML代码背后:
private ObservableCollection<RunYear> _years = new ObservableCollection<RunYear>();
public ObservableCollection<RunYear> Years { get{return _years; } }
public MainWindow()
{
InitializeComponent();
GenerateData();
}
private void GenerateData()
{
for (int i = 2010; i < 2015; i++)
{
var runYear = new RunYear(i);
runYear.Months[0] = new Month() { ColumnIndex = 0, MonthName = $"Jan {i}" };
runYear.Months[1] = new Month() { ColumnIndex = 1, MonthName = $"Feb {i}" };
runYear.Months[2] = new Month() { ColumnIndex = 2, MonthName = $"Mar {i}" };
Years.Add(runYear);
}
}
public void OnManageClicked(object sender, RoutedEventArgs args)
{
MessageBox.Show("manage");
}
public void OnDeleteClicked(object sender, RoutedEventArgs args)
{
MessageBox.Show("delete");
}
XAML:
<Window x:Class="SimplerOne.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SimplerOne"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ItemsControl Name="icYears" ItemsSource="{Binding Years}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="75"/>
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" Content="{Binding Year}" />
<ItemsControl Grid.Column="1" Name="icMonths" ItemsSource="{Binding Months}">
<ItemsControl.Resources>
<ContextMenu x:Key="ctxMenuFilingFrequency">
<MenuItem Header="Manage" Click="OnManageClicked" />
<MenuItem Header="Delete" Click="OnDeleteClicked" />
</ContextMenu>
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="75"></RowDefinition>
</Grid.RowDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Column" Value="{Binding ColumnIndex}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="0.5" BorderBrush="Black">
<StackPanel Background="White">
<StackPanel.Style>
<Style TargetType="{x:Type StackPanel}">
<Setter Property="ContextMenu" Value="{StaticResource ctxMenuFilingFrequency}"></Setter>
</Style>
</StackPanel.Style>
<TextBlock Text="{Binding MonthName}" Padding="2" Margin="2"></TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
答案 0 :(得分:0)
您可以使用CallMethodAction
中的WPF
。
只需将Microsoft.Expression.Interactions
和System.Windows.Interactivity
添加到您的项目中即可。
<ItemsControl.Resources>
<ContextMenu x:Key="ctxMenuFilingFrequency">
<!--<MenuItem Header="Manage" Click="OnManageClicked" />-->
<MenuItem Header="Manage">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction MethodName="ManageClicked" TargetObject="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:MainWindow}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</MenuItem>
</ContextMenu>
</ItemsControl.Resources>
在XAML
代码中使用它。
public void ManageClicked()
{
//I get fired from XAML
}
在代码后面定义一个方法。它让我public void
没有任何参数!