相关文章推荐
另类的甜瓜  ·  element-ui表头合并 - ...·  8 月前    · 
淡定的槟榔  ·  python string 前面加f ...·  1 年前    · 
稳重的佛珠  ·  @Transactional ...·  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
                return _image;
                _image = value;
                base.OnPropertyChanged("Image");
        public ObservableCollection<BitmapImage> Images
                return _images;
                _images = value;
                base.OnPropertyChanged("Images");
        public BitmapImage SelectedImage
                return _selectedImage;
                _selectedImage = value;
                base.OnPropertyChanged("SelectedImage");
        public ICommand DeleteCommand
                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>
出现的问题xaml代码: &amp;lt;Window.DataContext&amp;gt; &amp;lt;vm:MainWindowViewModel/&amp;gt; &amp;lt;/Window.DataContext&amp;gt; &amp;lt;ListView x:Name=&quot;ImageList&quot; Grid.Column=&quot;0&quot; Gri...
当使用以上代码的时候,右键窗口弹出的菜单不会显示“右键菜单”,提示“Cannot find source for binding with reference 'ElementName=tbl'”,这是由于ContextMenu不在Window的可视树中,所以找不到TextBlock。 要解决以上问题可以使用代码来进行绑定: public MainWindow()
  由于ContextMenu不继承父级的DataContext,所以如果要绑定父级的DataContext,直接DataContext=“{Binding}”是行不通的。因此可通过以下两个方式解决绑定问题。 1、使用资源的方式 1 <Button Content="Test" Command="{Binding TestCommand}">
WPF控件右侧状态栏实现ContextMenu1、在Resources中声明,在控件中使用2、在控件中直接声明 ContextMenu WPFContextMenu表示鼠标右键菜单栏,适用于WPF任何控件。ContextMenu属性默认实现,鼠标右键点击后显示菜单栏的功能,故使用ContextMenu属性时只需要安心实现菜单栏的样式和功能即可。 常用的通过ContextMenu实现右键菜单栏的方式有两种。 1、在Resources中声明,在控件中使用 在Window.Resources中声明具体的Con
WPF ContextMenu绑定失效问题 之前在尝试做右键菜单时部分菜单需要在运行时进行禁用,于是就想到了使用ElementName 来绑定运行按钮,因为运行按钮在运行时是禁用的,但是无论如何也绑定不上,后来经过查询才知道: 绑定失败的原因,是 Grid.ContextMenu 属性中赋值的 ContextMenu 不在可视化树中,而 ContextMenu 又不是一个默认建立 ScopeName 的控件,此时既没有自己指定 NameScope,有没有通过可视化树寻找上层设置的 NameScope,所以在
1.ContextMenu 控件介绍 简介:父类:MenuBase MenuItem (HeaderedItemsControl) ItemsControl 特定于某个元素之上的功能菜单。(右键菜单) 上下文菜单 属性:HorizontalOffset、VerticalOffset 右键菜单控件相对于点击位置的水平、垂直距离点 Label(右键菜单的目标元素) 快捷键响应:与命令或事件处理程序关联起来 应用:不独立存在,依赖于某个元素(目标元素) 2.具体案例 <Window x:Clas
变更通知是WPF一个精髓,它使得MVVM成为WPF的标准架构!在数据绑定中,除了正常的数据模版绑定,还会涉及到模板内控件的事件绑定,以及对parent内容的绑定!接下来的示例将会展示大部分常用的绑定场景。       示例实现的是一个便签软件,主要功能有新增、删除、修改、切换日期、读取便签列表、设置是否完成。       下面先说下几种绑定方式:        继承于ICommand和IN
ContextMenu无论定义在.cs或.xaml文件中,都不继承父级的DataContext,所以如果要绑定父级的DataContext,直接DataContext=“{Binding}”是行不通的 不能绑父级,但是能绑资源 第一步:定义一个中间类用来做资源对象 1 public class BindingProxy : Freezable 2 { 3 ...
在ListBox中的Item上点击右键时弹出菜单,由于使用MVVM架构,需要把为MenuItem绑定Command,并能把选择的ListBoxItem传递到ViewModel。 1、只对被点击的ListItem弹出ContextMenu,只需要建立一个DataTemplate即可,这个比较简单。 2、绑定Command,这个是比较麻烦的,查了许多资料,就是利用PlacementTa...