views:

397

answers:

5

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"&gt;

    <!-- 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.

+1  A: 

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.

If I declare Foo as the <UserControl /> attribute it is looking inside UserControl class for it instead of the MyUserControl.
alex2k8
Yes, that's right. I corrected the code just before your comment. In your control set Foo in constructor.
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.
alex2k8
A: 

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.

Robert Macnee
Thank you for answering, but the point was to do this in XAML
alex2k8
+1  A: 

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"&gt;

    <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>
Arcturus
1 - is not a XAML solution, 2-3 would not work, 4 - is not what was asked ;-)
alex2k8
Oops, my bad.. it cant be achieved in the same control.. Inheritance is the only way..
Arcturus
+3  A: 

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>
Andrew Jackson
Thank you for this finding! We just need to declare Foo as a Dependency Property for this to work.
alex2k8
A: 

What about a non string property. e.g. MyCustomObject=