Tutorial :WPF FrameworkElement



Question:

I am new to WPF programming model. I have few queries:

  1. Does Page tag in XAML (in an XBAP application) is of type FrameworkElement?

  2. if i have a child element lets say Frame inside a Page. I pass that frame element in a function in another assembly. Now i try to navigate back to the Page from Frame control's Parent property i am not able to get a reference to Page? Any thoughts?


The function which I am using in another assembly to get the parent Page of child control

Page getTopParent(FrameworkElement f)   {       FrameworkElement ct = f;         while (true)       {           if (ct is Page)           {               break;           }             ct = (FrameworkElement)          ct.Parent;       }       return ct as Page;   }  

Please find example XAML  

</DataTrigger> </DataTemplate.Triggers>-->

        </TabControl.ItemTemplate>          <TabControl.ContentTemplate>              <DataTemplate>                  <StackPanel HorizontalAlignment="Left">                      <Frame Name="tabContent" Height="520" Width="820" local:WebBrowserBehavior.Source="{Binding Path=CurrentPage}" ContentRendered="tabItem_contentRendered"/>                  </StackPanel>              </DataTemplate>          </TabControl.ContentTemplate>      </TabControl>      <StackPanel Orientation="Horizontal" Name="taskBar" Height="34" VerticalAlignment="Bottom">          <Button Height="23" Name="helpButton" Width="43" Content="Help" Click="helpButton_Click"/>      </StackPanel>  </StackPanel>  


Solution:1

  1. Yes, Page inherits from FrameworkElement.
  2. Sometimes going up the tree is not as easy as simply checking the parent, especially if you are using Templates. If you are using templates, you need to go up the tree using the Parent property until it is null. Then, use the TemplatedParent property. You can walk up the logical tree using a combination of both. That being said, WPF provides helper methods for this very scenario. Check out LogicalTreeHelper.GetParent.


Solution:2

  1. Yes, just like everything else in XAML. Go here for the official documentation.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »