0
votes

I have a View that is of type telerikDocking:RadDocumentPane. The first visual element on this view, and the container for all other visual elements, is a Grid with x:Name="MainGrid".

If I bind a property of the Grid to a public property on the ViewModel which is its DataContext, it works and sees it.

Inside this grid is a telerik:RadGridView and one of its columns is a Delete button whose visibility property is bound to the IsSelected property of the GridviewRow. That works fine as well.

I need to be able to override that Visibility of the button based on other criteria that is handled in a public property on the ViewModel. To accomplish this, I wrapped the button in a StackPanel and am trying to bind its Visibility to the ViewModel's public property and no matter what RelativeSource syntax I use, I cannot get to that property on the VieModel.

Here is the XAML for delete button:

<telerik:GridViewDataColumn>
            <telerik:GridViewDataColumn.CellTemplate>
              <DataTemplate>
                <StackPanel Visibility="{Binding ???}">

                  <telerik:RadButton Width="70"
                                     Command="telerik:RadGridViewCommands.Delete"
                                     CommandParameter="{Binding}"
                                     Content="Delete"
                                     IsEnabled="{Binding UserHasCofundingRole}"
                                     Visibility="{Binding IsSelected,
                                                          RelativeSource={RelativeSource AncestorType={x:Type telerik:GridViewRow}},
                                                          Converter={StaticResource booleanToVisibilityConverter}}" />
                </StackPanel>
              </DataTemplate>
            </telerik:GridViewDataColumn.CellTemplate>
          </telerik:GridViewDataColumn>

Here are all the binding syntax attempts that I have tried for the StackPanel's Visibility but none ever hit the ViewModel:

{Binding DataContext.DeleteButtonVisible, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerikDocking:RadDocumentPane}}, Converter={StaticResource booleanToVisibilityConverter}}
                    {Binding ViewModel.DeleteButtonVisible, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerikDocking:RadDocumentPane}}, Converter={StaticResource booleanToVisibilityConverter}}
                    {Binding ElementName=MainGrid, Path=DataContext.DeleteButtonVisible, Converter={StaticResource booleanToVisibilityConverter}}
                    {Binding ElementName=LayoutRoot, Path=ViewModel.DeleteButtonVisible, Converter={StaticResource booleanToVisibilityConverter}}
                    {Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=DataContext.DeleteButtonVisible, Converter={StaticResource booleanToVisibilityConverter}}
1
Have you checked your Output pane for binding errors? Useful first step to rule out which of your attempts are valid bindings and which are just bunk.goobering
It's also, I suspect, going to be worthwhile looking at multibinding. Rather than wrapping the button in a StackPanel, just replace its current binding with a multibinding to IsSelected and whatever your other conditions are, then implement a converter to figure out the Visibility logic.goobering
Thanks for the reply! I have looked at the Output. This is one output from a syntax that really should work: System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Telerik.Windows.Controls.RadDocumentPane', AncestorLevel='1''. BindingExpression:Path=DataContext.DeleteButtonVisible; DataItem=null; target element is 'StackPanel' (Name=''); target property is 'Visibility' (type 'Visibility')SteveW
We have looked into MultiBinding but have not been able to get one going successfully with the syntax.SteveW
Perhaps worthwhile opening a new question to get some suggestions on the multibinding?goobering

1 Answers

1
votes

Try to bind like this :

{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path= DataContext.YourPublicProp}

if you are in a Window or Page, or smt else, replace "UserControl" with the right type.