Expand UserControl property for XAML

WPF controls have certain properties (UserControl.Resources, UserControl.CommandBindings) that can have elements added to them from the XAML declaration of a user control. Example:

<UserControl ... > <UserControl.CommandBindings> ... </UserControl.CommandBindings> <UserControl.Resources> ... </UserControl.Resources> </UserControl> 

I have a new list property defined in my user control:

 public partial class ArchetypeControl : UserControl { ... public List<Object> UICommands { get; set; } 

I want to add elements to this list as I can, with resources and CommandBindings, but when I do this:

 <c:ArchetypeControl.UICommands> </c:ArchetypeControl.UICommands> 

I get the error "Error 4. The attachable property" UICommands "was not found in the type" ArchetypeControl ".

Suggestions?

-

Given the comments, I created a test control to show all the code and reproduce the problem. I am using visual studio 2010.

 <UserControl x:Class="ArchetypesUI.TestControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:c="clr-namespace:ArchetypesUI" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <c:TestControl.TestObject> </c:TestControl.TestObject> <Grid> </Grid> </UserControl> 

-

 namespace ArchetypesUI { /// <summary> /// Interaction logic for TestControl.xaml /// </summary> public partial class TestControl : UserControl { public Object TestObject { get; set; } public TestControl() { InitializeComponent(); } } } 

Now I get the error "Error 2 Nested property TestControl.TestObject" is not defined in "UserControl" or in one of its base classes.

+4
source share
2 answers

Take a look at your XAML:

 <UserControl> ^^^^^^^^^^^ <c:TestControl.TestObject> ^^^^^^^^^^^ </c:TestControl.TestObject> </UserControl> 

Here you declare a UserControl, and then try to set the TestControl property on it. Since UserControl does not have the TestControl.TestObject, WPF property, it cannot set this property in the UserControl object. You can say: "But I am declaring a UserControl of type TestControl. My UserControl is TestControl!" But it is not so. The above declaration declares the TestControl class: it does not create an instance of TestControl, so it cannot have instance properties on it.

Rather, the TestObject property is available for TestControl users to install in separate instances of TestControl:

 <local:TestControl> <local:TestControl.TestObject> <!-- Now it will work --> </local:TestControl.TestObject> </local:TestControl> 

If you want to set the default / initial value for the TestObject property, you can do this either in the TestControl constructor, or (if TestObject is a dependency property) through the default TestControl style (although this is more suitable for custom than for custom controls).

+7
source

I'm not quite able to recreate your problem ... the case that I created seems to work. I had to initialize the list in the constructor.

However, from your example, I think the ViewModel object would have a more appropriate place for your list source. If you expose commands having IEnumerable of some ICommand shell, which also encapsulates the necessary display elements (for example, Caption, URI, etc.).

ViewModels, of course, are not a panacea, but in this case, I think that this will allow you to put all the knowledge about the commands that you want to use in the same place (for example, which are available and what they do).

+1
source

Source: https://habr.com/ru/post/1303486/