0

I was able to create a neat context menu that controls visibility of columns and wanted to show values of hidden ones in a tooltip, but I'm unable to, because somehow the very same binding is returning DependencyProperty.UnsetValue. Why? What makes the difference and how to fix that?

 <telerik:RadGridView x:Name="radVideosGridView" ItemsSource="{Binding Customers}"> <telerik:RadGridView.RowStyle> <Style TargetType="telerik:GridViewRow"> <Setter Property="ToolTip"> <Setter.Value> <StackPanel Orientation="Horizontal"> <TextBlock> <TextBlock.Text> <MultiBinding Converter="{StaticResource HiddenColumnsToTooltipConverter}" UpdateSourceTrigger="PropertyChanged" NotifyOnSourceUpdated="False"> <Binding Path="PersonalData" /> <!-- Passes {{DependencyProperty.UnsetValue}} --> <Binding Path="Columns" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:RadGridView}}}" /> </MultiBinding> </TextBlock.Text> </TextBlock> </StackPanel> </Setter.Value> </Setter> </Style> </telerik:RadGridView.RowStyle> <telerik:RadGridView.Columns> <telerik:GridViewDataColumn DataMemberBinding="{Binding PersonalData.FirstName}" Header="Last Name"/> <telerik:GridViewDataColumn DataMemberBinding="{Binding PersonalData.SecondName}" Header="Second Name" IsVisible="False"/> <telerik:GridViewDataColumn DataMemberBinding="{Binding PersonalData.LastName}" Header="Last Name"/> </telerik:RadGridView.Columns> <!-- Works fine --> <telerik:RadContextMenu.ContextMenu> <telerik:RadContextMenu> <telerik:RadContextMenu.Template> <ControlTemplate> <Grid> <telerik:RadListBox ItemsSource="{Binding Columns, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:RadGridView}}}"> <telerik:RadListBox.ItemTemplate> <DataTemplate> <CheckBox Content="{Binding Header}" IsChecked="{Binding IsVisible, Mode=TwoWay}" /> </DataTemplate> </telerik:RadListBox.ItemTemplate> </telerik:RadListBox> </Grid> </ControlTemplate> </telerik:RadContextMenu.Template> </telerik:RadContextMenu> </telerik:RadContextMenu.ContextMenu> </telerik:RadGridView> 

1 Answer 1

1

I've found out the reason behind difference in behavior. RadContextMenu is a child of RadGridView while ToolTip is in a different branch build around PopUp primite so we access columns by Ancestor source. I've write a work-around based on this code:

https://stackoverflow.com/a/1759923

public class HiddenColumnsToTooltipConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value != null && value is Camera camera) { try { var window = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive); var columns = ((RadGridView)FindChild(window, $"{parameter}", typeof(RadGridView))).Columns; ... return result; } catch { } } return "Nothing to show"; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return Binding.DoNothing; } public static DependencyObject FindChild(DependencyObject reference, string childName, Type childType) { DependencyObject foundChild = null; if (reference != null) { int childrenCount = VisualTreeHelper.GetChildrenCount(reference); for (int i = 0; i < childrenCount; i++) { var child = VisualTreeHelper.GetChild(reference, i); if (child.GetType() != childType) { foundChild = FindChild(child, childName, childType); if (foundChild != null) break; } else if (!string.IsNullOrEmpty(childName)) { var frameworkElement = child as FrameworkElement; if (frameworkElement != null && frameworkElement.Name == childName) { foundChild = child; break; } } else { foundChild = child; break; } } } return foundChild; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.