2011-04-15 72 views
0

我在XAML中的ControlTemplate,目標是ToogleButton與X:關鍵=「NewBtn」WPF添加動態資源programmically C#

請幫我使用模板從控制模板 如果建立在C#代碼的toogle按鈕在XAML代碼這是代碼:

<ToggleButton Template="{DynamicResource NewBtn}" 
        Margin="12,21,0,0" HorizontalAlignment="Left" Width="151" Height="29" VerticalAlignment="Top" 
        x:Name="newBtn" 
       Checked = newBtn_Checked Unchekcked = newBtn_Unchecked 
        /> 

請幫我如何在C#創建

回答

2
var button = new ToggleButton 
{ 
    Margin = new Thickness(12, 21, 0, 0), 
    HorizontalAlignment = HorizontalAlignment.Left, 
    Width = 151, 
    Height = 29, 
    VerticalAlignment = VerticalAlignment.Top, 
    Name = "newBtn", 
}; 
button.SetResourceReference(Button.TemplateProperty, "NewBtn"); 

ToggleButton button = new ToggleButton(); 
button.Margin = new Thickness(12, 21, 0, 0); 
button.HorizontalAlignment = HorizontalAlignment.Left; 
button.Width = 151; 
button.Height = 29; 
button.VerticalAlignment = VerticalAlignment.Top; 
button.Name = "newBtn"; 
button.SetResourceReference(Button.TemplateProperty, "NewBtn");