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

RadDropDownButton.DropDownContent property breaks logical tree inheritance

5 Answers 380 Views
Buttons
This is a migrated thread and some comments may be shown as answers.
Yavor
Top achievements
Rank 1
Yavor asked on 12 May 2010, 09:31 AM

I stumbled upon a adding a content to the DropDownContent property of the RadDropDownButton. Particularly I have a problem binding a dependency property of the content element to a property up the logical tree hierarchy. I managed to reproduce the issue in small piece of XAML. It contains a window with a local value “Test” set to its Tag property. There is a TextBlock inside the DropDown button with its property Text bound to the Tag property of the window. When I run the sample I get the following error:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:Path=Tag; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

Another identical TextBlock is placed outside of the DropDown button and its Binding works fine.

I guess that the RadDropDownButton control fails to preserve the logical tree inheritance by not calling
FrameworkElement.
AddLogicalChild method on the DropDownContent element (provided that the content inherits from FrameworkElement). Here is the XAML code:

 

<Window x:Class="WpfApplication14.MainWindow" Title="MainWindow" SizeToContent="WidthAndHeight" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"   
        Tag="Test" > 
      
    <Grid Width="200" Height="200">  
        <StackPanel HorizontalAlignment="Left">  
            <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Tag}" Height="22"/>  
            <telerik:RadDropDownButton Content="Drop Down" Height="22" Width="200" DropDownWidth="200">  
                <telerik:RadDropDownButton.DropDownContent> 
                    <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Tag}"/>  
                </telerik:RadDropDownButton.DropDownContent> 
            </telerik:RadDropDownButton> 
        </StackPanel> 
    </Grid> 
</Window> 
 

5 Answers, 1 is accepted

Sort by
0
Miro Miroslavov
Telerik team
answered on 15 May 2010, 08:44 AM
Hello Yavor,

Since DropDownContent is placed in Popup "over" the other elements, it's not living in the same visual tree. Which means it doesn't get the DataContext so you can't bind to it. Also you can't find Relative parents like the Window, because they are not relatives. In DropDownContent you're in different visual tree.
For example you can bind DropDownContent to a static context that you can modify according to your needs. For reference can find the attached project.
If you have further questions please ask us.

All the best,
Miro Miroslavov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
tzuhsun
Top achievements
Rank 1
answered on 04 Apr 2011, 09:37 AM
I have exact problem here, but now I not taking a string, instead I need to bind a control outside of RadDropDownButton, says Window. I'll take Yavor example to illustrate:

<Window x:Class="WpfApplication14.MainWindow" Title="MainWindow" SizeToContent="WidthAndHeight"
        mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"  
        Tag="Test" >
       
    <Grid Width="200" Height="200"
        <StackPanel HorizontalAlignment="Left"
            <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Tag}" Height="22"/> 
            <telerik:RadDropDownButton Content="Drop Down" Height="22" Width="200" DropDownWidth="200"
                <telerik:RadDropDownButton.DropDownContent>
                    <!-- Skip some command binding syntax -->
                    <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Window}}" />
                    <!-- End command binding syntax -->
                </telerik:RadDropDownButton.DropDownContent>
            </telerik:RadDropDownButton>
        </StackPanel>
    </Grid>
</Window>

Have any workaround for this?
0
tzuhsun
Top achievements
Rank 1
answered on 07 Apr 2011, 04:25 AM
Has a workaround so far.

Put the binding of object at outside of RadDropDownButton, with Loaded event, then call back that object later in ViewModel code.
0
Inger Marie
Top achievements
Rank 1
answered on 03 May 2011, 08:09 AM
I have a custom raddropdownbutton (it inherits from raddropdownbutton). It has two dependency properties which are both commands. The name (x:name) of the control is ucThis in XAML.

The first one is used inside an itemtemplate in a ListView in the dropdowncontent. I can bind to the dependency property on ucThis.
The second one (SendReminderByPhoneCommand) is used as command on a button in the dropdown content. Binding to the dependency property on ucThis does not work.

When I read this post, I found out what the problem was. I am still a bit new to XAML and havent quite mastered the logical/visual tree stuff, but I figured that if the visual tree does not work the templates must be in the logical tree which seems to work.

I solved it by setting the second in the buttons template, such as this:
 <Button Margin="2">
                    <Button.Template>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Button Command="{Binding SendReminderByPhoneCommand, ElementName=ucThis}"
                                    Style="{StaticResource MyButtonStyle}">
                                <ContentPresenter Content="{TemplateBinding Content}"
                                                  />
                            </Button>
                        </ControlTemplate>
                    </Button.Template>
0
Allen
Top achievements
Rank 2
Iron
Veteran
answered on 17 Jun 2011, 03:09 PM
???? So how does it update the underlying viewmodel property if you pick one of the values?????
Tags
Buttons
Asked by
Yavor
Top achievements
Rank 1
Answers by
Miro Miroslavov
Telerik team
tzuhsun
Top achievements
Rank 1
Inger Marie
Top achievements
Rank 1
Allen
Top achievements
Rank 2
Iron
Veteran
Share this question
or