Windows phone 7 如何通过代码设置按钮样式

Windows phone 7 如何通过代码设置按钮样式,windows-phone-7,Windows Phone 7,我试图将我在blend中制作的样式赋予代码中生成的按钮。我尝试了以下方法,但似乎不起作用。我也无法在网上找到任何解决方案 这是代码 private void createBtn() { Button btn = new Button(); btn.Name = buttonName; btn.Style = popUpStyle; btn.Margin = new Thickness(20); btn.Con

我试图将我在blend中制作的样式赋予代码中生成的按钮。我尝试了以下方法,但似乎不起作用。我也无法在网上找到任何解决方案

这是代码

private void createBtn()
{
        Button btn = new Button();

        btn.Name = buttonName;

        btn.Style = popUpStyle;

        btn.Margin = new Thickness(20);

        btn.Content = "Close";

        btn.Click += new RoutedEventHandler(btn_Click);

        btn.Visibility = System.Windows.Visibility.Visible;

        Canvas.Children.Add(btn);
}
还有xaml

<Style x:Name="popUpStyle" TargetType="Button">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
        <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/>
        <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>
        <Setter Property="Padding" Value="10,3,10,5"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate x:Name="popUpStyleCT" TargetType="Button">
                    <Grid Background="Transparent" Height="150" Width="200">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed"/>
                                <VisualState x:Name="Disabled"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Rectangle Fill="#FFC2C2C2" Margin="0,15,0,0"/>
                        <Rectangle Fill="#FF313197" Height="15" Stroke="Black" VerticalAlignment="Top" Opacity="0.25"/>
                        <Rectangle Fill="#FFE20303" HorizontalAlignment="Right" Height="15" Stroke="Black" VerticalAlignment="Top" Width="30" Opacity="0.25"/>
                        <TextBlock HorizontalAlignment="Right" Height="15" TextWrapping="Wrap" Text="X" VerticalAlignment="Top" Width="14" FontSize="13.333" Margin="0,0,5,0"/>
                        <Rectangle Fill="#FF4D4A4A" Height="22" Margin="50,0,44,8" VerticalAlignment="Bottom" Stroke="Black"/>
                        <Border x:Name="ButtonBackground" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" Margin="0,12,0,34">
                            <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" Margin="-3,2,73,0" Foreground="Black" FontSize="13.333" VerticalAlignment="Top" HorizontalAlignment="Left"/>
                        </Border>
                        <TextBlock Height="15" TextWrapping="Wrap" Text="Close" VerticalAlignment="Bottom" FontSize="13.333" Margin="85,0,81,12" Foreground="Black"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我认为SL论坛是您一直在搜索的内容。

我认为SL论坛是您一直在搜索的内容