2009-02-20 91 views
6

單擊此按鈕啓動方法稱爲「(的AssemblyName).Reports」當內容的按鈕「報告」點擊窗口:如何向標準XAML元素添加其他屬性?

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    Button button = (Button)e.OriginalSource; 
    Type type = this.GetType(); 
    Assembly assembly = type.Assembly; 
    Window window = (Window)assembly.CreateInstance(String.Format("{0}.{1}", type.Namespace, button.Content)); 
    window.ShowDialog(); 
} 

但我希望按鈕的內容屬性值可以改變,例如它可能會變成「股票報告」,但我仍然希望點擊按鈕來啓動「(assemblyname).Reports」。

有沒有辦法給按鈕標籤添加屬性,例如: 「TheWindowFileName」?

<Button x:Name="btnReports" Content="Stock Reports" TheWindowFileName="Reports"/> 

如果沒有,怎麼我還可以添加額外的信息,我可以在後面的代碼讀取和處理我的按鈕元素?

回答

10

當然,你可以使用attached properties額外的屬性添加到XAML元素,但你需要什麼,你很可能只是利用現有的Tag屬性:

<Button x:Name="btnReports" Content="Stock Reports" Tag="Reports"/> 
+0

非常有趣,適合我需要的東西,可以添加多個這些嗎?這個MSDN頁面不是那麼清楚:http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.tag.aspx – 2009-02-20 09:12:40

1

這裏使用附加屬性可以是矯枉過正,而是你可以嘗試將您的按鈕行爲封裝在Command中,並將您要在命令中使用的數據作爲CommandParameter傳遞。這應該夠了吧。