原文參考以下連結

http://www.redmondpie.com/dynamic-controls-creation-in-a-c-wpf-application-part-1/

 

做法大概為

新建一個ResourceDictionary檔,名為style.xaml

將在主程式xaml中style設定copy至style.xaml

在專案中加入PresentationFramework.Aero 參考

如在style.xaml中有以下代碼

<Style x:Key="ComboBoxStyle4" TargetType="{x:Type ComboBox}">

.....

</Style>

於C#檔中寫以下代碼

ResourceDictionary res = (ResourceDictionary)System.Windows.Application.LoadComponent(new Uri("Style.xaml", UriKind.Relative));

Style style = (Style)res["ComboBoxStyle4"];

CBx_telnumber.Style = style;

以上為從Style.xaml中找出style key為ComboBoxStyle4的設定並將此設定給CBx_telnumber的style

arrow
arrow
    全站熱搜

    狼翔月影 發表在 痞客邦 留言(1) 人氣()