简体   繁体   中英

EventToCommand ancestor binding WPF

Problem occures when invoke's

 <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>

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='PhotoBrowser.List.ViewModel.ListViewModel', AncestorLevel='1''. BindingExpression:Path=Test; DataItem=null; target element is 'EventToCommand' (HashCode=37598799); target property is 'Command' (type 'ICommand')

How to fix it? ListViewModel:

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

<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>

Have a look at:

AncestorType={x:Type ViewModel:ListViewModel}

ViewModel is not the ancestor because ViewModel is no Control in your View. Correct would be:

  • ListView (Parent)
    • ListViewItem (Child)

Instead of this you should bind to the DataContext of the ListView (because this is your ViewModel ):

<Command:EventToCommand Command="{Binding Path=DataContext.Test, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM