Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF XAML Global Reference

Tags:

c#

wpf

xaml

Is there a way to reuse a 3rd party control reference?

For example, I have this referenced in my App.xaml

xmlns:cust="clr-namespace:ThirdParty.Controls;assembly=ThirdParty.Controls"

I don't want to repeat this 3rd party control xml namespace on each page/control that needs a control from the library.

Is there anyway to centralize these references and use the prefix defined here? The possibility of each control having a different prefix is also worrisome. In asp.net you would put a reference in the web.config and it was available globally, I'm just looking to see if there is a similar method in WPF.

like image 792
Jab Avatar asked Apr 28 '26 01:04

Jab


1 Answers

Two options I am thinking

1) Wrap that control into a UserControl and then use your UserControl in all the places.

2) Declare the third party control as a Resource somewhere and then use DynamicResource reference to that on your other places.

The second option can be implemented as bellow.

Where ever you want the third party control put a ContentControl like bellow

<ContentControl Template="{DynamicResource thirdPartyControlTemplate}" />

The ControlTemplate will be in the Resource file or at App.Xaml as bellow.

  xmlns:thridParty="clr-namespace:WpfCustomControlLibrary1;assembly=WpfCustomControlLibrary1"                >
<Application.Resources>
    <ControlTemplate x:Key="thirdPartyControlTemplate" TargetType="{x:Type ContentControl}">
        <thridParty:ThirdPartyControl />
    </ControlTemplate>
</Application.Resources>

You can see that the namespace declaration will be always on this resource file and you will be able to use that Control control from any place

like image 89
Jobi Joy Avatar answered Apr 29 '26 20:04

Jobi Joy