2014-11-03 81 views
3

我是新來的c#世界,並有一個關於使用xaml UserControl對象的參數的問題。C#XAML WPF在UserControl上渲染之前使用參數

我已經在XAML我MainGrid /主窗口中定義的UserControll 「的ImageButton」:

<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" /> 

在另一邊,我有我的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(); 
} 
..... 

現在我想知道在哪一點我可以通過我在xaml中定義的參數值進行訪問。

我試過幾種方法(包括構造函數本身),但我只在後面的c#代碼中獲得一個空值。 現在我嘗試了幾種方法,現在我使用「OnRender」方法 - 在此方法中,我可以從xaml訪問我的參數值。

但我真的不能確定這是否是正確的方式..

五月有人知道Usercontroll,在那裏我可以訪問XAML的parameterValues和處理一些事情的抽籤之前的另一種方法?

親切關於你的UserControl自己的XAML中聲明

+1

您通常會在屬性元數據中註冊一個[PropertyChangedCallback](http://msdn.microsoft.com/en-us/library/system.windows.propertychangedcallback.aspx)。 – Clemens 2014-11-03 13:22:53

+1

在c'tor附加一個處理程序到'loaded'事件,他們應該在這個時候初始化。 – user2697817 2014-11-03 13:36:18

回答

2

屬性值將在InitializeComponent()進行處理。在您的UserControl的Xaml使用站點中提供的值將在Initialized被引發後可用。

綁定有點不同;綁定將被應用並且準備好接收基於上述規則的值,但是在調度員有機會處理優先級爲DataBind的項目之前,源值可能不會被轉移到目標。這將發生在控制爲Loaded時。