Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# XAML WPF Use Parameters before Render on UserControl

I'm new to the c# world and have an Question about the parameters use of xaml UserControl Objects.

i have defined a UserControll "ImageButton" in my MainGrid / Main Window in xaml:

<local:ImageButton HorizontalAlignment="Left" Height="100" Margin="49,122,0,0" VerticalAlignment="Top" Width="100" sText="SomeText" sType="main_button" Source="Resources/main_button.png" />

On the other side i have my ImageBButton.xmal.cs

public partial class ImageButton : UserControl
.....
public ImageSource Source
{
    get { 
        return (ImageSource)GetValue(SourceProperty); }
        set { SetValue(SourceProperty, value); }
    }
.....
public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(default(ImageSource)));
.....
public ImageButton()
{
    InitializeComponent();
}
.....

Now i would like to know at which point i have access thru the parameter values which i defined in the xaml.

I tried on several methods (including the Constructor itself) but i only get an empty value in c# code behind. As i tried several methods now, i use the "OnRender" Method now - in this method i can access my parametervalues from the xaml.

But i'm really unsure if this is the right way..

May someone knows another Method before the draw of the Usercontroll, where i can access the xaml parametervalues and handle some things?

kindly regards

like image 764
eXe Avatar asked Dec 09 '25 21:12

eXe


1 Answers

Property values declared in your UserControl's own Xaml will be processed in InitializeComponent(). Values provided in Xaml usage sites of your UserControl will be available after Initialized is raised.

Bindings are a little different; the bindings will be applied and ready to receive values based on the rules above, but the source values may not be transferred to the targets until the dispatcher has had the opportunity to process items with DataBind priority. This will happen by the time the control is Loaded.

like image 132
Mike Strobel Avatar answered Dec 11 '25 11:12

Mike Strobel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!