How can you reference a fully defined control as a resource in WPF?

I know that I can refer to styles and templates from the resource dictionary, and I use them very much. But what about total control?

I can declare a fully defined WPF control, such as a button, in the app.xaml file as a resource. For example, the app.xaml file that defines the button will contain the following:

<Button x:Key="HelpButton" Content="?" /> 

But how can I refer to it in the xaml markup of a user control so that the button displays?

EDIT:
Answering the question "Why do you need this?" I agree that the button is not a great example (it is just described).

What about the polygon (not control, I know) that you declare in app.xaml and want to use in multiple xaml files?

+4
source share
3 answers

You cannot and, frankly, I'm not sure why you want to. This button is one button, which means that it can only be in one place (at a time); given that it makes sense to define a new button anywhere you need it. As you have already figured out, what template resources are for.

(When I say that you cannot, I mean that it is not supported in plain XAML, perhaps you could implement an IValueConverter in a class that returns a button and bind it in XAML to the contents of the content and, of course, you can use the code to add and remove buttons programmatically from different containers as needed. None of them seem like a great option.)

+3
source

As a side note for Ben M.'s answer:

Some visual effects can be used in this way - things inherited from ToolTip, for example, but this only works when used in the ToolTip property:

 <UserControl.Resources> <local:MyToolTip x:Key="ToolTip"/> <UserControl.Resources> <Button ToolTip="{StaticResource ToolTip}"/> 

This sorting works, but you really have to use a style to do such things.

0
source

The only possibility that makes sense is to include some controls in the DataTemplate and use this. DataTemplate "dresses" any object and can include full control; in fact, it is quite common to create custom controls and build them using DataTemplates; for example, you can define a DataTemplate as follows:

 <DataTemplate TargetType="{x:Type viewmodels:MyViewModel}"> <Button Command={Binding MyCommand} Content={Binding MyCaption} /> </DataTemplate> 

Assuming that the MyViewModel parameter contains the string property MyCaption and the ICommand property called MyCommand, the DataTemplate will automatically be used, if defined within the scope, to visually display an instance of the MyViewModel class if:

  • an instance is set as the Content property of a ContentControl or derived control, or
  • An instance is one of many items in a collection that is displayed using the ItemsControl element.

When I say β€œin scope,” what I really mean is a DataTemplate defined in a ResourceDictionary somewhere in an accessible hierarchy of controls; i.e. Application.Resources, Window.Resources, etc., up to the Grid.Resources level, if the target control (ContentControl or ItemsControl) is placed in it.

Of course, you can also specify a DataTemplate without a TargetType, but use x: Key instead, and use it manually, referring to that key; for example myListBox.ItemTemplate={StaticResource myKey} .

0
source

All Articles