This is a migrated thread and some comments may be shown as answers.

DraggedItemTemplate that contains bindings with ElementName or RelativeSource won't work

3 Answers 67 Views
DragAndDrop
This is a migrated thread and some comments may be shown as answers.
Aleksandar
Top achievements
Rank 1
Iron
Aleksandar asked on 08 Jul 2013, 08:01 PM
I'm using the DragVisualProvider from the documentation to create a custom drag visual based on a DraggedItemTemplate. The DraggedItemTemplate is bound to a DataTemplate located in the ListBox.Resources. However, my bindings within the DataTemplate fail if I try to bind using ElementName or RelativeSource to access data outside the DataTemplate.

<ListBox Grid.Row="1"
         ItemsSource="{Binding VehiclesCollectionView}"
         Style="{StaticResource DispatchListBoxStyle}"
         Height="Auto" AllowDrop="True">
  <ListBox.Resources>
    <DataTemplate x:Key="VehicleDragTemplate">
      <Grid HorizontalAlignment="Stretch"
            Margin="6">
        <TextBlock Width="16" VerticalAlignment="Center"
                   TextAlignment="Center" FontWeight="Bold" FontSize="10"
                   Text="{Binding DataContext.DriverName, ElementName=LayoutRoot}" />
          </Grid>
    </DataTemplate>
  </ListBox.Resources>
  <telerik:ListBoxDragDrop.Behavior>
    <telerik:ListBoxDragDropBehavior/>
  </telerik:ListBoxDragDrop.Behavior>
  <telerik:ListBoxDragDrop.DragVisualProvider>
    <my:DragVisualProvider DraggedItemTemplate="{StaticResource VehicleDragTemplate}" />
  </telerik:ListBoxDragDrop.DragVisualProvider>

3 Answers, 1 is accepted

Sort by
0
Nick
Telerik team
answered on 11 Jul 2013, 10:33 AM
Hi Aleksander,

This won't work since the drag cue and the Relative source you are giving it are not in the same visual tree, hence the drag cue cannot locate the element you are giving it.
You can extend your DragVisual provider with an additional property for the DataContext.

Hope this makes sense! 

Regards,
Nik
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Mark
Top achievements
Rank 1
answered on 27 Jul 2013, 11:41 PM
Can you please provide an example for your advisement, it appears I'm having the same difficulty in migrating to DragAndDrop.
Specifically a DataTemplate could pick up a binding given:

<UserControl xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"  x:Class="RoasterWerks.DataTemplates.InvItemTemplate"
      
    <Border BorderThickness="1" BorderBrush="Black" CornerRadius="5">
    <Grid x:Name="LayoutRoot" Margin="5" >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="20"/>
            <ColumnDefinition Width="50*" MinWidth="50"/>
            <ColumnDefinition Width="30*" MinWidth="30"/>
            <ColumnDefinition Width="50*" MinWidth="50"/>
            <ColumnDefinition Width="30*" MinWidth="30"/>
            <ColumnDefinition Width="50*" MinWidth="50"/>
            <ColumnDefinition Width="30*" MinWidth="30"/>
            <ColumnDefinition Width="50*" MinWidth="50"/>
            <ColumnDefinition Width="30*" MinWidth="30"/>
        </Grid.ColumnDefinitions>
  
        <TextBlock Text="{Binding quantity}" Grid.RowSpan="2" Grid.Row="0" Grid.Column="0"  HorizontalAlignment="Center" 
                   VerticalAlignment="Center" FontSize="20" Foreground="Black"/>
            <TextBlock Text="{Binding packDesc}" Grid.Row="2" Grid.Column="0" Foreground="Black"/>
  
        <Border Grid.Column="1" Grid.Row="0" Grid.RowSpan="3" Width="1" Background="#FFA0AFC3" VerticalAlignment="Stretch" HorizontalAlignment="Center" Margin="0,10" />
  
  
            <TextBlock Text="{Binding varietal}" Grid.ColumnSpan="8" Grid.Row="0" Grid.Column="2" Foreground="Black" FontWeight="Bold" HorizontalAlignment="Center" FontSize="14"/>
            <TextBlock Text="{Binding gradeName}" Grid.Row="1"  Grid.Column="2" Foreground="Black"/>
  
            <StackPanel Orientation="Horizontal" Grid.Column="2" Grid.Row="2" Grid.ColumnSpan="2">
                <TextBlock Text="BF: " Foreground="Black"/>
                <TextBlock Text="{Binding isBirdFriendly, Converter={StaticResource BoolConverter}, ConverterParameter='3'}" Foreground="DarkGreen" FontWeight="Bold"/>
            </StackPanel>
              
  
            <TextBlock Text="{Binding LengthName}" Grid.Column="4" Grid.Row="1" Foreground="Black"/>
            <StackPanel Orientation="Horizontal" Grid.Column="4" Grid.Row="2" Grid.ColumnSpan="2">
                <TextBlock Text="FT: " Foreground="Black"/>
                <TextBlock Text="{Binding isFairTrade, Converter={StaticResource BoolConverter}, ConverterParameter='3'}" Foreground="DarkGreen" FontWeight="Bold"/>
            </StackPanel>
                  
  
            <TextBlock Text="{Binding DensityShort}" Grid.Row="1" Grid.Column="6" Foreground="Black"/>
            <StackPanel Orientation="Horizontal" Grid.Column="6" Grid.Row="2" Grid.ColumnSpan="2">
                <TextBlock Text="Org: " Grid.Column="6" Grid.Row="2" Foreground="Black"/>
                <TextBlock Text="{Binding isOrganic, Converter={StaticResource BoolConverter}, ConverterParameter='3'}" Grid.Column="7" Grid.Row="2" Foreground="DarkGreen" FontWeight="Bold"/>
            </StackPanel>
              
            <TextBlock Text="{Binding ScreenName}" Grid.Column="8" Grid.Row="1" Foreground="Black"/>
            <StackPanel Orientation="Horizontal" Grid.Column="8" Grid.Row="2" Grid.ColumnSpan="2">
                <TextBlock Text="RF: " Foreground="Black"/>
                <TextBlock Text="{Binding isRainForest, Converter={StaticResource BoolConverter}, ConverterParameter='3'}" Foreground="DarkGreen" FontWeight="Bold"/>
            </StackPanel>
              
  
    </Grid>
    </Border>
</UserControl>

When I look at your limited information and examples and examine the code, I have the new function:

private void OnUnfixedDragInitialize(object sender, DragInitializeEventArgs e)
        {
            DropIndicationDetails details = new DropIndicationDetails();
            var row = e.OriginalSource as GridViewRow ?? (e.OriginalSource as FrameworkElement).ParentOfType<GridViewRow>();
  
            var item = row != null ? row.Item : (sender as RadGridView).SelectedItem;
            details.CurrentDraggedItem = item;
  
            IDragPayload dragPayload = DragDropPayloadManager.GeneratePayload(null);
  
            dragPayload.SetData("DraggedData", item);
            dragPayload.SetData("DropDetails", details);
  
            e.Data = dragPayload;
  
            e.DragVisual = new Telerik.Windows.DragDrop.DragVisual()
            {
                Content = details, ContentTemplate = this.Resources["InvItemTemplate"] as DataTemplate
            };
            e.DragVisualOffset = e.RelativeStartPoint;
            e.AllowedEffects = DragDropEffects.All;
        }

... when I examine variable "item" and details.CurrentDraggedItem they correctly picks up the information but the binding to the template doesn't work.

### IMMEDIATE WINDOW ###
  
item
{qryUnfixedDiffPurch : {False,False,False,False,False,144,188,Arabica}}
    base {System.ServiceModel.DomainServices.Client.Entity}: {qryUnfixedDiffPurch : {False,False,False,False,False,144,188,Arabica}}
    _basePrice: 3
    _convPounds: 2.2046226
    _densityShort: null
    _diffPrice: null
    _earlyFD: null
    _effPrice: null
    _gradeName: null
    _isBirdFriendly: false
    _isDecaf: false
    _isFairTrade: false
    _isOrganic: false
    _isRainForest: false
    _lateFD: null
    _lengthName: null
    _origin: "Belize"
    _packDesc: "Bulk (Kilos)"
    _poLedgerID: 144
    _poLineID: 188
    _quantity: 4
    _screenName: null
    _speciesID: "Arabica"
    _transDesc: ""
    _varietal: "Caturra"
    basePrice: 3
    convPounds: 2.2046226
    DensityShort: null
    diffPrice: null
    earlyFD: null
    effPrice: null
    gradeName: null
    isBirdFriendly: false
    isDecaf: false
    isFairTrade: false
    isOrganic: false
    isRainForest: false
    lateFD: null
    LengthName: null
    origin: "Belize"
    packDesc: "Bulk (Kilos)"
    POLedgerID: 144
    POLineID: 188
    quantity: 4
    ScreenName: null
    speciesID: "Arabica"
    transDesc: ""
    varietal: "Caturra"
  
  
details.CurrentDraggedItem
{qryUnfixedDiffPurch : {False,False,False,False,False,144,188,Arabica}}
    base {System.ServiceModel.DomainServices.Client.Entity}: {qryUnfixedDiffPurch : {False,False,False,False,False,144,188,Arabica}}
    _basePrice: 3
    _convPounds: 2.2046226
    _densityShort: null
    _diffPrice: null
    _earlyFD: null
    _effPrice: null
    _gradeName: null
    _isBirdFriendly: false
    _isDecaf: false
    _isFairTrade: false
    _isOrganic: false
    _isRainForest: false
    _lateFD: null
    _lengthName: null
    _origin: "Belize"
    _packDesc: "Bulk (Kilos)"
    _poLedgerID: 144
    _poLineID: 188
    _quantity: 4
    _screenName: null
    _speciesID: "Arabica"
    _transDesc: ""
    _varietal: "Caturra"
    basePrice: 3
    convPounds: 2.2046226
    DensityShort: null
    diffPrice: null
    earlyFD: null
    effPrice: null
    gradeName: null
    isBirdFriendly: false
    isDecaf: false
    isFairTrade: false
    isOrganic: false
    isRainForest: false
    lateFD: null
    LengthName: null
    origin: "Belize"
    packDesc: "Bulk (Kilos)"
    POLedgerID: 144
    POLineID: 188
    quantity: 4
    ScreenName: null
    speciesID: "Arabica"
    transDesc: ""
    varietal: "Caturra"

with the old Manager I was able to use

if (e.Options.ParticipatingVisualRoots.Contains(this) == false)
                e.Options.ParticipatingVisualRoots.Add(this);
 but there is no such "Options" in DragAndDrop.

Thanks,
Mark

0
Nick
Telerik team
answered on 31 Jul 2013, 06:49 AM
Hello Aleksandar,

The idea is to create a DependencyProperty in your DragVisualProvider that specifies the DataContext of the DragVisuals ot creates. That way you will be able to access the data context of your main window/ visual and bind to the values you are looking for.

Hope this helps! 

Regards,
Nik
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
DragAndDrop
Asked by
Aleksandar
Top achievements
Rank 1
Iron
Answers by
Nick
Telerik team
Mark
Top achievements
Rank 1
Share this question
or