Monday, April 25, 2011

Declaratively set property of MyUserControl in MyUserControl.xaml

Assuming we have such control:

public partial class MyUserControl : UserControl
{
    public MyUserControl() {
        InitializeComponent();
    }

    public string Foo { get; set; }
}

How can I set "Foo" property value declaratively in MyUserControl.xaml?

<UserControl x:Class="Test.MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!-- Looking for some thing like this -->
    <Foo>Hola</Foo>

</UserControl>

To be more clear: How can I set in XAML a value for the property defined in code-behind.

From stackoverflow
  • In your control set it in the constructor

    public MyUserControl{ this.Foo = "Hola";}
    

    If you're using the control somewhere else:

    <Window xmlns:mycontrol="Test"     ....
    

    Intellisense will help you here with proper syntax of xmlns

    <mycontrol:MyUserControl       Foo="Hola"/>
    

    I'm not sure, but Foo may probably need to be a DependencyProperty.

    alex2k8 : If I declare Foo as the attribute it is looking inside UserControl class for it instead of the MyUserControl.
    : Yes, that's right. I corrected the code just before your comment. In your control set Foo in constructor.
    alex2k8 : Thank you wwosik, thought I am not actually try to set it, just try to figure out the XAML posibilities :-) So was curious, if we can do this on the XAML side in the control related xaml.
  • It seems like what you really want is a default value for properties on your UserControl. Unfortunately you can't set it in a Style unless it's a DependencyProperty. It also can't be the target of a Binding.

    If Foo were a DependencyProperty, you could set it in the PropertyMetadata when you declare your DependencyProperty:

    public static readonly DependencyProperty FooProperty = DependencyProperty.Register("Foo", typeof(String), typeof(MyUserControl), new PropertyMetadata("Hola"));
    

    Otherwise you're probably better off setting the default value in code-behind.

    alex2k8 : Thank you for answering, but the point was to do this in XAML
  • This can only be achieved in xaml by inheritance:

    You are providing an implementation for your control. So the only way to achieve a value in xaml for your control is through inheritance.

    Your second UserControl will look like this:

    <Test:MyUserControl x:Class="Test.MyUserControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:Test="clr-namespace:Test"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
        <Test:MyUserControl.Foo>Hola</Test:MyUserControl.Foo>
    
    </Test:MyUserControl>
    

    or:

    <Test:MyUserControl x:Class="Test.MyUserControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:Test="clr-namespace:Test"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Foo="Hola">
    
    </Test:MyUserControl>
    
    alex2k8 : 1 - is not a XAML solution, 2-3 would not work, 4 - is not what was asked ;-)
    Arcturus : Oops, my bad.. it cant be achieved in the same control.. Inheritance is the only way..
  • I've found in the past that you can use a style to set properties on a UserControl from xaml without inheritance. Try something like this:

    <UserControl x:Class="Test.MyUserControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:c="clr-namespace:Test.MyUserControl" >
    
        <UserControl.Style>
            <Style>
                <Setter Property="c:MyUserControl.Foo" Value="Hola" />
            </Style>
        </UserControl.Style>
    
    </UserControl>
    
    alex2k8 : Thank you for this finding! We just need to declare Foo as a Dependency Property for this to work.
  • What about a non string property. e.g. MyCustomObject=

0 comments:

Post a Comment