繁体   English   中英

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

[英]Binding to a dependency property of a behavior inside a child control

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

ThumbnailListView用户控件

        <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用户控件

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

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

ThumbnailListView.DataContext设置为SelectedItem.Pages ,我高度怀疑SelectedItem.Pages是否具有名为SelectedItem.DragDropCommand的属性。

更改ItemDragDrop绑定的Source ,以指定它使用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 DataType="{x:Type local:WhateverSelectedItemDataTypeIs}}">
    <!-- DataContext will automatically set to WhateverSelectedItemDataTypeIs -->
    <Views:ThumbnailListView /> 
</DataTemplate>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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