I have created a custom ListBoxTemplate for ListBox Item which displays the Image and other information (Example: Employee). I need a scenario in which the listbox item should be draggable /not draggable based on some conditions(Ex: If FirstName is null then not draggable). How can I achieve this?
The xaml-code snippet that i am using right now is as follows:
-------------------------------------
<DataTemplate> <StackPanel Orientation="Horizontal" > <StackPanel.Background> <Binding Path="Background" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}"/> </StackPanel.Background> <Border x:Name="ImageBorder" BorderThickness="1" BorderBrush="Gray" Margin="5,5,0,5"> <Image Source="{Binding ImagePath}" Width="64" Height="64" /> </Border> <StackPanel Orientation="Vertical" Margin="5,10,5,0"> <TextBlock Text="{Binding JobTitle}" FontWeight="Bold" /> <TextBlock Text="{Binding FirstName}" VerticalAlignment="Center" /> <TextBlock Text="{Binding LastName}" /> </StackPanel> <!--allow an item to be dragged by setting the DragSource object’s IsDraggable property to True.--> <ig:DragDropManager.DragSource > <ig:DragSource x:Name="ListItemBoxDragSource" IsDraggable="True" DragChannels="ChannelA" DragStart="DragSource_DragStart" Drop="DragSource_Drop" > </ig:DragSource> </ig:DragDropManager.DragSource> <ig:DragDropManager.DropTarget> <ig:DropTarget IsDropTarget="True" DropChannels="ChannelA"> </ig:DropTarget> </ig:DragDropManager.DropTarget> </StackPanel> <DataTemplate.Triggers> <DataTrigger Binding="{Binding Path=FirstName}" Value="{x:Null}"> <Setter TargetName="ImageBorder" Property="BorderBrush" Value="Red" /> <Setter TargetName="ListItemBoxDragSource" Property="IsDraggable" Value="False"/> </DataTrigger> </DataTemplate.Triggers> </DataTemplate>
---------------------------------------------
Since, the present xaml gives error of "TargetName" not defined because it is not a valid visual component. How can I achieve set/reset of draggable property of DropSource.
Help..
Thank you in advance...
Hello Ram,
Thank you for your post. I have been looking into it and I can suggest you remove the DataTemplate Trigger and instead of it you can cancel the Dragging in the DragStart event handler. I can suggest you set the Tag Property of the root StackPanle of the DataTemplate like this Tag = “{Binding}”. After doing this you can use this code in order to check if the FisrtName is null or not and decide whether to cancel or not the event:
if (((e.DragSource as StackPanel).Tag as Employee).FirstName == null) { e.Cancel = true; }
Please let me know if this helps you or you have further questions on this matter.
Looking forward for your reply.
Thank you Stefan.
I followed your suggestion and It worked. I had to make a little change as
(e.DragSource as StackPanel).Tag resulted in Null Value.
Instead I did like following:
StackPanel sp = e.DragSource as StackPanel;Employee emp = sp.DataContext as Employee; if (emp.FirstName == null) { e.Cancel = true; }
It worked. Thank you once again. Have a good weekend.
Thank you for your feedback. I am glad that you resolved your issue and I believe that other community members may benefit from this as well.
Thanks again.