WPF--ContextMenu绑定命令的一个问题

出现的问题

xaml代码:

    <Window.DataContext>
        <vm:MainWindowViewModel/>
    </Window.DataContext>
     <ListView x:Name="ImageList" 
                      Grid.Column="0" Grid.Row="1"     
                      MinHeight="470"
                      ItemsSource="{Binding Images}" 
                      SelectedItem="{Binding SelectedImage, Mode=OneWayToSource}" Margin="0,27,0,-16.667" Height="470" Grid.RowSpan="2" VerticalAlignment="Top" Background="#FFF0F0F0" 
                      >
                <ListView.ItemTemplate >
                    <DataTemplate >
                            <Border BorderThickness="1" BorderBrush="AliceBlue">
                                <Image Width="{Binding ActualWidth, ElementName=textBlock, Mode=OneWay}" Source="{Binding}" Stretch="UniformToFill">
                                    <Image.ContextMenu>
                                        <ContextMenu>
                                            <MenuItem Header="删除" 
                                                      Command="{Binding DeleteCommand}"/>
                                        </ContextMenu>
                                    </Image.ContextMenu>
                                </Image>
                            </Border>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

部分ViewModel代码:

        private BitmapImage _selectedImage;
        private BitmapImage _image;
        private ObservableCollection<BitmapImage> _images;

        public BitmapImage Image
        {
            get
            {
                return _image;
            }
            set
            {
                _image = value;
                base.OnPropertyChanged("Image");
            }
        }
        public ObservableCollection<BitmapImage> Images
        {
            get
            {
                return _images;
            }
            set
            {
                _images = value;
                base.OnPropertyChanged("Images");
            }
        }
        public BitmapImage SelectedImage
        {
            get
            {
                return _selectedImage;
            }
            set
            {
                _selectedImage = value;
                base.OnPropertyChanged("SelectedImage");
            }
        }
        public ICommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                {
                    _deleteCommand = new RelayCommand(param => this.DeleteFile());
                }
                return _deleteCommand;
            }
        }
        private void DeleteFile()
        {
            _images.Remove(_selectedImage);
            base.OnPropertyChanged("Images");
        }

问题说明:将ListView的suorce设置为Images,将图片显示在列表中,想要给显示的图片添加一个ContextMenu,使右击图片时,执行删除命令,移除这一张图片,但是上述的方法没反应。

解决方法:

https://stackoverflow.com/questions/25205640/wpf-binding-command-to-contextmenu

 <Image  Tag="{Binding DataContext,ElementName=ImageList}" Width="{Binding ActualWidth, ElementName=textBlock, Mode=OneWay}" Source="{Binding}" Stretch="UniformToFill">
    <Image.ContextMenu>
        <ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
            <MenuItem Header="删除" 
                      Command="{Binding DeleteCommand}"/>
        </ContextMenu>
    </Image.ContextMenu>
</Image>
在Windows Presentation Foundation (WPF)中创建一个美观的ContextMenu通常涉及到自定义模板、数据绑定和一些UI设计原则。以下是一个基本步骤: 1. 创建ContextMenu资源: ```xml <Window.Resources> <ControlTemplate x:Key="MenuItemTemplate"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Viewbox Stretch="UniformToFill"> <TextBlock Text="{TemplateBinding Header}" Foreground="{TemplateBinding Foreground}" FontFamily="{TemplateBinding FontFamily}" FontSize="{TemplateBinding FontSize}" Margin="{TemplateBinding Padding}" /> </Viewbox> <Rectangle Grid.Row="1" Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" /> </Grid> </Border> </ControlTemplate> </Window.Resources> ``` 2. 使用模板: ```xml <Button.ContextMenu> <ContextMenu ItemsSource="{Binding MyItems}" ItemTemplate="{StaticResource MenuItemTemplate}"> <ContextMenu.ItemContainerStyle> <Style TargetType="MenuItem"> <Setter Property="Width" Value="200" /> <Setter Property="Template" Value="{StaticResource MenuItemTemplate}" /> </Style> </ContextMenu.ItemContainerStyle> </ContextMenu> </Button.ContextMenu> ``` 在这里,`MyItems`是一个包含你要显示菜单项的数据源。 3. 风格化外观(可选): - 你可以添加鼠标悬停、点击状态改变效果,以及图标支持等。 - 可以通过`DataTrigger`s来根据不同的条件更改背景色、边框样式等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值