Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting XAML property value to user control

I have a user control in WPF which i want the text of one of it's labels to be read from the XAML where it is used. Hence..

My User Control:

 <UserControl x:Class="muc">
        <Label Foreground="#FF7800" FontSize="20" FontWeight="Bold">          
             <Label.Content>
                <Binding ElementName="TestName" Path="." />
             </Label.Content>
        </Label>
 </UserControl>

Then using it:

 <mycontorls:muc TestName="This is a test" />

But it doesn't works ... How can i read the properties ?

like image 249
aviv Avatar asked Jul 05 '10 23:07

aviv


People also ask

How do I add user control to XAML?

Using a user control in XAML is pretty simple. I use a WPF Application to test the control. Create a WPF application project and copy the control code files to your project. After that, you need to add namespace of the library in which the user control is defined.

How do I set property in XAML?

Here is the XAML file in which the TextBlock is defined as a user control and the Text property will be assigned to it by the SetText dependency property. The following XAML code creates a user control with initializing its SetText dependency property and some other properties.

What is user control in XAML?

User controls, in WPF represented by the UserControl class, is the concept of grouping markup and code into a reusable container, so that the same interface, with the same functionality, can be used in several different places and even across several applications.

What is the difference between user control and custom control in WPF?

A customControl can be styled and templated and best suited for a situation when you are building a Control Library. On the contrary, a UserControl gives you an easy way to define reusable chunk of XAML which can be reused widely in your application and when you don't need to use it as a Control Library .


1 Answers

I tried the first two answers and what I got worked in code but not on XAML (also doesn't let you see changes in the design view when using the control).

To get a property working like any other native one, here is the full process: (The sample adds a dependency property of type Nullable to show in the control as text or a default if null)

  1. In the code file:

    1.a Define a dependency property:

    public static readonly DependencyProperty MyNumberProperty = DependencyProperty.Register("MyNumber", typeof(Nullable<int>), typeof(MyUserControl), new PropertyMetadata(null, new PropertyChangedCallback(OnMyNumberChanged)));
    

    1.b Implement the OnMyNumberChanged Callback:

    private static void OnMyNumberChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args){
        // When the color changes, set the icon color PlayButton
        MyUserControl muc = (MyUserControl)obj;
        Nullable<int> value = (Nullable<int>)args.NewValue;
        if (value != null)
        {
            muc.MyNumberTextBlock.Text = value.ToString();
        }
        else
        {
            muc.MyNumberTextBlock.Text = "N/A";
        }
    }
    

    1.c implement the MyNumber property (not dependency) to use the dependency property for easy in code use:

    public Nullable<int> MyNumber{
        get
        {
            return (Nullable<int>)GetValue(MyNumberProperty);
        }
        set
        {
            SetValue(MyNumberProperty, value);
            OnTargetPowerChanged(this, new DependencyPropertyChangedEventArgs(TargetPowerProperty, value, value));    // Old value irrelevant.
        }
    }
    
  2. In the XAML file bind the TextBlock control's text to the property (not dependency) to get the default value of the dependency property in case it is not set by the user of the control (assuming you called your root element of the user control "RootElement"):

This code:

 < TextBlock Name="MyNumberTextBlock" Text="{Binding MyNumber, ElementName=RootElement}"/>
like image 200
sprite Avatar answered Nov 15 '22 22:11

sprite