Expose UserControl property to XAML

Posted by Jared on Stack Overflow See other posts from Stack Overflow or by Jared
Published on 2010-03-08T19:27:09Z Indexed on 2010/03/08 20:21 UTC
Read the original article Hit count: 1562

Filed under:
|
|

WPF controls have certain properties (UserControl.Resources, UserControl.CommandBindings) that can have items added to them from the XAML of a user control declaration. 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 items to this list like 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 type 'ArchetypeControl'. "

Suggestions?

-

Given the comments, I've created a test control to show the entire code and reproduce the problem. I'm 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 the error I get is "Error 2 The attached property 'TestControl.TestObject' is not defined on 'UserControl' or one of its base classes."

© Stack Overflow or respective owner

Related posts about wpf

Related posts about property