首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EventToCommand祖先绑定WPF

EventToCommand祖先绑定WPF
EN

Stack Overflow用户
提问于 2016-12-11 19:27:30
回答 1查看 769关注 0票数 0

调用时出现的问题

代码语言:javascript
复制
 <i:Interaction.Triggers>
 <i:EventTrigger EventName="MouseDoubleClick">
 <Command:EventToCommand Command="{Binding Path=Test, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ViewModel:ListViewModel}}}" />
  </i:EventTrigger>
  </i:Interaction.Triggers>

AncestorType='PhotoBrowser.List.ViewModel.ListViewModel',System.Windows.Data错误:4:无法找到引用'RelativeSource FindAncestor,RelativeSource AncestorLevel=‘1’绑定的源代码。BindingExpression:Path=Test;DataItem=null;目标元素为“EventToCommand”(HashCode=37598799);目标属性为“命令”(键入“ICommand”)

怎么修呢?ListViewModel:

代码语言:javascript
复制
namespace PhotoBrowser.List.ViewModel
{
    public class ListViewModel : ViewModelBase
    {

        public ObservableCollection<Item> Items { get; set; }

        public ListViewModel()
        {
            Items = new ObservableCollection<Item>();
        }
        public RelayCommand Test
        {
            get;
            set;
        }
        public RelayCommand DoubleClickOnItem
        {
            get
            {
                return new RelayCommand(() => OpenEdit());
            }
        }

        private void OpenEdit()
        {
            Messenger.Default.Send(new NotificationMessage("Edit"));
        }

        public RelayCommand<DragEventArgs> Drop
        {
            get
            {
                return new RelayCommand<DragEventArgs>(CheckInstanceCertificate);
            }
        }

        private void CheckInstanceCertificate(DragEventArgs args)
        {
            var path = string.Join("", (string[])args.Data.GetData(DataFormats.FileDrop, true));
            Items.Add(new Item { ImagePath = path });
        }
    }

}

ListView.xaml

代码语言:javascript
复制
<UserControl x:Class="PhotoBrowser.List.View.ListView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
             xmlns:ViewModel="clr-namespace:PhotoBrowser.List.ViewModel"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             DataContext="{Binding Source={StaticResource Locator}, Path=List}"
             >
    <UserControl.Resources>
        <Style x:Key="FileItemStyle" TargetType="{x:Type ListViewItem}">
            <Setter Property="Margin" Value="5,5,5,5"/>
            <Setter Property="Padding" Value="0,0,0,0"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate  TargetType="{x:Type ListViewItem}">
                        <Grid HorizontalAlignment="Left" VerticalAlignment="Top" Height="50" >
                            <Border x:Name="border" BorderBrush="{x:Null}" BorderThickness="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CornerRadius="2.5"/>
                            <StackPanel HorizontalAlignment="Stretch"  VerticalAlignment="Stretch">
                                <ContentPresenter/>
                            </StackPanel>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <Grid>
        <ListView ItemsSource="{Binding Items}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
              ItemContainerStyle="{StaticResource FileItemStyle}" AllowDrop="True">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Drop">
                    <Command:EventToCommand Command="{Binding Drop}" PassEventArgsToCommand="True" />
                </i:EventTrigger>
            </i:Interaction.Triggers>

            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel/>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>

            <ListView.ItemTemplate>
                <DataTemplate>
                    <DockPanel>
                        <Image Source="{Binding ImagePath}" Height="64" Width="64">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="MouseDoubleClick">
                                    <Command:EventToCommand Command="{Binding Path=Test, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ViewModel:ListViewModel}}}" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </Image>
                    </DockPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</UserControl>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-11 19:37:49

看一下:

代码语言:javascript
复制
AncestorType={x:Type ViewModel:ListViewModel}

ViewModel不是祖先,因为ViewModel在您的视图中不是控件。正确的做法是:

  • ListView (父)
    • ListViewItem (儿童)

而不是这样,您应该绑定到DataContext of ListView (因为这是您的ViewModel):

代码语言:javascript
复制
<Command:EventToCommand Command="{Binding Path=DataContext.Test, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}}"/>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41090188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档