所以我知道有类似的问题,但我仍然感到困惑,并希望有人可以帮助我。我有一个能够选择项目的列表框,它会添加一个属性。我现在有一个上下文菜单,因为布局正是我需要的,但我正在努力使我的点击事件工作,目前我不能做我的事情,但我有列表背后的代码的代码工作的框,有人可以帮助我让上下文菜单上的lsitbox点击事件工作:)?
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listBox = sender as ListBox;
if (listBox.SelectedItems.Count == 0)
{
return;
}
var item = listBox.SelectedItems[0] as PropertyNode;
viewModel.AddPropertyNode(item);
}
修改
这是我刚刚在上下文菜单中使用的XAML
<DataTemplate x:Key="AddNodeTemplate">
<Border BorderThickness="1" Background="#F7F7F7">
<Border.BorderBrush>
<DrawingBrush Viewport="8,8,8,8" ViewportUnits="Absolute" TileMode="Tile">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="#F7F7F7">
<GeometryDrawing.Geometry>
<GeometryGroup>
<RectangleGeometry Rect="0,0,50,50"/>
<RectangleGeometry Rect="50,50,50,50"/>
</GeometryGroup>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Border.BorderBrush>
<StackPanel>
<Button x:Name="ButtonAdd" Click="ButtonAdd_Click" Height="30" Width="130" Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type syncfusion:Node}}}">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Background="#F7F7F7">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Source="Images/icon_plus.bmp" HorizontalAlignment="Left" Margin="5,0,0,0"/>
<TextBlock Text="Add Property" HorizontalAlignment="Center" Grid.Column="1" VerticalAlignment="Center" Foreground="LightGray" FontStyle="Italic" FontSize="12"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="#F7F7F7"/>
</Style>
</Button.Style>
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="_Copy Existing" Icon="{StaticResource ImageCopy}" ItemsSource="{Binding Path=AvailableProperties}" Click="MenuItem_Click">
<MenuItem.Resources>
<Style TargetType="MenuItem">
<Style.Resources>
<Image x:Key="img" x:Shared="False" Width="12" Height="12" Source="{Binding Icon, Converter={StaticResource ImageToSourceConverter}}"
Margin="3" VerticalAlignment="Center"/>
<Style TargetType="ContentPresenter">
<Style.Triggers>
<Trigger Property="ContentSource" Value="Icon">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Image Source="{Binding}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Style.Resources>
<Setter Property="Icon" Value="{Binding Icon, Converter={StaticResource ImageToSourceConverter}}"/>
<Style.Triggers>
<Trigger Property="Role" Value="SubMenuItem">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ContentPresenter Content="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</MenuItem.Resources>
</MenuItem>
<MenuItem Header="Upscale well logs"/>
<MenuItem Header="Upscale well_top attributes"/>
<MenuItem Header="Upscale point attributes" Icon="{StaticResource ImagePointSet}">
</MenuItem>
<MenuItem Header="Calculate"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
</StackPanel>
</Border>
</DataTemplate>
上下文菜单背后的代码,我知道它并不多,但这是我可以得到的
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
var menuItem = sender as MenuItem;
}
当前输出
答案 0 :(得分:0)
请确认您已正确链接XAML中的代码。类似的东西:
<ListView SelectionChanged="Selector_OnSelectionChanged">
我注意到我们没有看到你的任何wpf xaml代码。如果这不能回答你的问题那么可能有助于添加你的xaml的一瞥,这样我们就可以确定你的Selector_OnSelectionChanged没有被处理的原因。
谢谢!
答案 1 :(得分:0)
经过大量的谷歌搜索和询问我找到了答案,并认为我发布给任何可能有这个问题的人,只是它以我的问题的形式,这应该很容易编辑:)< / p>
<DataTemplate x:Key="AddNodeTemplate">
<Border BorderThickness="1" Background="#F7F7F7">
<Border.BorderBrush>
<DrawingBrush Viewport="8,8,8,8" ViewportUnits="Absolute" TileMode="Tile">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="#F7F7F7">
<GeometryDrawing.Geometry>
<GeometryGroup>
<RectangleGeometry Rect="0,0,50,50"/>
<RectangleGeometry Rect="50,50,50,50"/>
</GeometryGroup>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Border.BorderBrush>
<StackPanel>
<Button x:Name="ButtonAdd" Click="ButtonAdd_Click" Height="30" Width="130" Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type syncfusion:Node}}}">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Background="#F7F7F7">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Source="Images/icon_plus.bmp" HorizontalAlignment="Left" Margin="5,0,0,0"/>
<TextBlock Text="Add Property" HorizontalAlignment="Center" Grid.Column="1" VerticalAlignment="Center" Foreground="LightGray" FontStyle="Italic" FontSize="12"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="#F7F7F7"/>
</Style>
</Button.Style>
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Copy Exisiting" ItemsSource="{Binding Path=AvailableProperties}" Click="AddExistingProperty_OnClick" Icon="Images/Copy.bmp" FontFamily="MS Reference Sans Serif">
<MenuItem.Resources>
<Style TargetType="MenuItem">
<Style.Resources>
<Image x:Key="img" x:Shared="False" Width="10" Height="10" Source="{Binding Icon, Converter={StaticResource ImageToSourceConverter}}"
Margin="3" VerticalAlignment="Center"/>
<Style TargetType="ContentPresenter">
<Style.Triggers>
<Trigger Property="ContentSource" Value="Icon">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Image Source="{Binding}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Style.Resources>
<Setter Property="Icon" Value="{Binding Icon, Converter={StaticResource ImageToSourceConverter}}"/>
<Style.Triggers>
<Trigger Property="Role" Value="SubMenuItem">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ContentPresenter Content="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</MenuItem.Resources>
</MenuItem>
<MenuItem Header="Upscale well logs" Click="AddProperty_OnClick" Tag="UpscaleWellLogs" Style="{StaticResource MenuItemIcon}" Icon="Images/WellLogs.png" FontFamily="MS Reference Sans Serif"/>
<MenuItem Header="Upscale well top attributes" Click="AddProperty_OnClick" Tag="UpscaleWellTopAttributes" Style="{StaticResource MenuItemIcon}" Icon="Images/WellTop.png" FontFamily="MS Reference Sans Serif"/>
<MenuItem Header="Upscale point attributes" Click="AddProperty_OnClick" Tag="UpscalePointAttributes" Style="{StaticResource MenuItemIcon}" Icon="Images/PointSet.png" FontFamily="MS Reference Sans Serif"/>
<MenuItem Header="Calculate" Click="AddProperty_OnClick" Tag="Calculate" Style="{StaticResource MenuItemIcon}" Icon="Images/calculator.png" FontFamily="MS Reference Sans Serif"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
</StackPanel>
</Border>
</DataTemplate>
private void AddExistingProperty_OnClick(object sender, RoutedEventArgs e)
{
var menuItem = e.OriginalSource as MenuItem;
var item = menuItem.DataContext as PropertyNode;
viewModel.AddPropertyNode(item);
}
private void AddProperty_OnClick(object sender, RoutedEventArgs e)
{
if (MenuItemActivated != null)
{
var menuItem = sender as MenuItem;
var command = menuItem.Tag as CognitiveTreeMenuCommand?;
if (command.HasValue)
{
MenuItemActivated(this, new MenuItemEventHandlerArgs() { Command = command.Value });
}
}
}