【发布时间】:2016-10-11 16:47:23
【问题描述】:
我正在尝试创建一个像 chrome 一样的下载栏。
我目前遇到的问题是尝试将单击事件绑定到列表框项中按钮的上下文菜单。单击上下文菜单项时,它表示未找到该操作。
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Button BorderBrush="Transparent" BorderThickness="0" telerik:StyleManager.Theme="Windows8" Click="ButtonBase_OnClick">
<StackPanel Name="Panel" SnapsToDevicePixels="True"
Orientation="Horizontal" Margin="1 0"
Height="30">
<ContentControl Margin="0 0 10 0" Height="20">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate" Value="{StaticResource Icons.File}"></Setter>
</Style>
</ContentControl.Style>
</ContentControl>
<TextBlock Foreground="Black" Text="{Binding FileName}"
VerticalAlignment="Center"
TextAlignment="Center"
Margin="1 0 0 0"/>
<Button x:Name="ExpandButton" Background="Transparent" Click="ExpandButton_OnClick" BorderThickness="0" VerticalAlignment="Center" ContextMenuService.IsEnabled="false">
<Button.ContextMenu>
<ContextMenu x:Name="popup">
<MenuItem Header="Open" cal:Message.Attach="[Click] = [Open($this)]"></MenuItem>
</ContextMenu>
</Button.ContextMenu>
<ContentControl ContentTemplate="{StaticResource Icons.ArrowUp}" Width="10" Height="10" Margin="2" VerticalAlignment="Center"/>
</Button>
<Rectangle Width="2" Fill="Gray" Margin="0 0 0 0"/>
</StackPanel>
</Button>
</ControlTemplate>
我可以将它绑定在应用程序的 code(xaml.cs) 端,但我也忘记了上下文应该指向的项目。为此,我将 caliburn 的 click 事件替换为常规的 Click 事件。 SelectedItem 和 SelectedItems 分别为 null 或空。
private void MenuItem_OnClick(object sender, RoutedEventArgs e)
{
var originalSource = e.OriginalSource;
var selectedItem = FileListBox.SelectedItem;
var SelectedItems = FileListBox.SelectedItems;
}
【问题讨论】:
-
我有点困惑你想要做什么。第一张照片是
ListBoxItem,最右边有一个按钮,对吗?当您单击ListBoxItem最右侧的按钮时,您希望上下文菜单打开? -
@kalamazoowho 这正是我想要完成的。所以看起来你并不感到困惑。
-
我已经用代码隐藏做过类似的问题,但我没有将上下文菜单弹出从右键单击更改为左键单击。您说您忘记了上下文指向的项目。你知道你可以从哪些项目中挑选吗?就像上下文菜单将指向的总是
Button类型?