绑定到子控件内行为的依赖项属性

时间:2015-01-28 20:08:42

标签: c# wpf xaml data-binding

我有以下用户控件和自定义依赖项属性

ThumbnailListView UserControl

        <ListView 
            ItemsSource="{Binding}" 
            BorderThickness="0,0,0,0" 
            HorizontalAlignment="Center" 
            Background="White" 
            SelectionChanged="ListView_SelectionChanged"
            AllowDrop="True">
            <i:Interaction.Behaviors>
                <behaviors:DragDropBehavior OnDragDrop="{Binding Path=ItemDragDrop}"></behaviors:DragDropBehavior>
            </i:Interaction.Behaviors>
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource IsLastListItem}}" Value="False">
                            <Setter Property="Margin" Value="0,0,0,20"></Setter>
                        </DataTrigger>
                    </Style.Triggers>

                    <Setter Property="Background" Value="Gray"></Setter>
                    <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"></Setter>                      
                </Style>
            </ListView.ItemContainerStyle>

            <ListView.ItemTemplate>
                <DataTemplate>
                     <Image Source="{Binding Thumbnail, Converter={StaticResource ImageConverter}}"></Image>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

ThumbnailListView的依赖属性ItemDragDrop

    public static ICommand GetItemDragDrop(DependencyObject obj)
    {
        return (ICommand)obj.GetValue(ItemDragDropProperty);
    }

    public static void SetItemDragDrop(DependencyObject obj, ICommand value)
    {
        obj.SetValue(ItemDragDropProperty, value);
    }

    public static DependencyProperty ItemDragDropProperty = DependencyProperty.RegisterAttached("ItemDragDrop",
        typeof(ICommand), typeof(ThumbnailListView));

    public ICommand ItemDragDrop
    {
        get
        {
            return (ICommand)GetValue(ItemDragDropProperty);
        }

        set
        {
            SetValue(ItemDragDropProperty, value);
        }
    }

NewScansView UserControl

        <DockPanel Dock="Top">
            <ListView ItemsSource="{Binding Scans}" Width="500">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Caption}" Margin="5,0,0,0"></TextBlock>
                    </DataTemplate>
                </ListView.ItemTemplate>

                <ListView.ItemContainerStyle>
                    <Style TargetType="{x:Type ListViewItem}">
                        <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
                    </Style>
                </ListView.ItemContainerStyle>
            </ListView>
            <Views:ThumbnailListView DataContext="{Binding SelectedItem.Pages}" ItemDragDrop="{Binding SelectedItem.DragDropCommand}" VerticalAlignment="Stretch" Width="200" />
            <Views:PageListView DataContext="{Binding SelectedItem.Pages}" VerticalAlignment="Stretch" />
        </DockPanel>

NewScansView xaml包含一个ThumbnailListView控件,并将ThumbnailListView的依赖项属性ItemDragDrop绑定到SelectedItem类中的命令。

在ThumbnailListView用户控件中,我有一个行为DragDropBehavior,它有一个依赖属性OnDragDrop。

我正在尝试将OnDragDrop绑定到ItemDragDrop,以便在拖放操作完成时执行SelectedItem类中的命令。

问题是它似乎无法找到ThumbnailListView上的ItemDragDrop属性或所选项类的DragDropCommand。

我想知道我做错了什么以及如何设置它?

1 个答案:

答案 0 :(得分:1)

ThumbnailListView.DataContext设置为SelectedItem.Pages,我非常怀疑SelectedItem.Pages是否有一个名为SelectedItem.DragDropCommand的属性。

更改Source绑定的ItemDragDrop以指定它使用ThumnailListView.DataContext以外的内容作为绑定源。

<DockPanel x:Name="MyDockPanel" Dock="Top">
    ...
    <Views:ThumbnailListView DataContext="{Binding SelectedItem.Pages}" 
                             ItemDragDrop="{Binding SelectedItem.DragDropCommand, ElementName=MyDockPanel}" ... />
    ...
</DockPanel>       

您可能还必须对行为绑定执行相同的操作 - 更改其来源,使其指向ThumbnailListView而不是ThumbnailListView.DataContext

<i:Interaction.Behaviors>
    <behaviors:DragDropBehavior OnDragDrop="{Binding Path=ItemDragDrop, RelativeSource={RelativeSource AncestorType={x:Type local:ThumbnailListView}}}" />
</i:Interaction.Behaviors>

或者更好的是,为Pages创建DependencyProperty,这样就不依赖于特定的DataContext对象类型

<Views:ThumbnailListView PagesDependencyProperty="{Binding SelectedItem.Pages}" ItemDragDrop="{Binding SelectedItem.DragDropCommand}" .. />

或编辑您的控件,使其假定DataContext使用了特定类型,并使用隐式DataTemplate始终使用此控件绘制该类型的对象(对我来说更常见):

<ListView ItemsSource="{Binding Pages}" ...>
    <i:Interaction.Behaviors>
        <behaviors:DragDropBehavior OnDragDrop="{Binding DragDropCommand}" />
    </i:Interaction.Behaviors>
    ...
</ListView>

隐式DataTemplate:

<DataTemplate DataType="{x:Type local:WhateverSelectedItemDataTypeIs}}">
    <!-- DataContext will automatically set to WhateverSelectedItemDataTypeIs -->
    <Views:ThumbnailListView /> 
</DataTemplate>