Parham!

Hi,

I have made a UserControl, Now i want to make another UserControl Inherited from the first UserControl. How to do it in Blend or VS2005 What changes should I made to xaml or .vb

Regards,

Parham



Re: Windows Presentation Foundation (WPF) How to make an inherited UserControl

Parham!

Any idea ! How to inherit a UserControl from another UserControl with Blend and VS2005




Re: Windows Presentation Foundation (WPF) How to make an inherited UserControl

Wei Zhou - MSFT

Hi Parham

Do you mean that you don't know how to create a user control inherited from another user control If so, the following answer may helps.

The most different point in creating a user control inherited from another user control with creating a user control inherited from a normal UserControl is that only one control we created can have a XAML file associated with it. The following example shows how to do this.

In first example, only UserControlB have XAML associated with it. In second example, only UserControlA have XAML associated with it.

Code Snippet

UserControlA

Public Class UserControlA

Inherits UserControl

End Class

UserControlB

<local:UserControlA x:Class="LearningSolution.UserControlB"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:local="clr-namespace:LearningSolution"

Height="300" Width="300">

</local:UserControlA>

OR:

UserControlA

<UserControl x:Class="LearningSolution.UserControlA"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Height="300" Width="300">

</UserControl>

UserControlB

Public Class UserControlB

Inherits UserControlA

End Class

How to use controls:

<Window x:Class="LearningSolution.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:local="clr-namespace:LearningSolution"

Title="MainWindow" Width="1100" Height="800">

<StackPanel>

<local:UserControlA BorderBrush="AliceBlue" BorderThickness="1"/>

<local:UserControlB BorderBrush="AliceBlue" BorderThickness="1"/>

</StackPanel>

</Window>

Best Regards

Wei Zhou






Re: Windows Presentation Foundation (WPF) How to make an inherited UserControl

Bragi

If you want to change the content of the control derived from your userControl, consider creating a custom control an providing a Template for it. This way, you can change the Template of your derived class. Offcourse, using UI designers becomes a little more tricky.




Re: Windows Presentation Foundation (WPF) How to make an inherited UserControl

Parham!

Wei Zhou - MSFT wrote:

The most different point in creating a user control inherited from another user control with creating a user control inherited from a normal UserControl is that only one control we created can have a XAML file associated with it. The following example shows how to do this.



Thank you Wie!

This is the point that I didn't know and have problem with it. but Why Is there any workaround to have XAML files associted with both of them (for example do that with three class, one with XAML, second drived from first without XAML, and third drived from second with XAML!)

Regards,
Parham.