如何从后面的代码中 'call' 我的样式按钮 (xaml)

How to 'call' my styled button(xaml) from code behind

我在 XAML 中有一个按钮,我按照我需要的方式设置了样式。这是该按钮的 XAML 代码:

        <Button x:Name="btnAddNewItem"
            Grid.Column="0" Grid.Row="0"
            FontSize="15"
            BorderThickness="1.5"
            HorizontalContentAlignment="Center"
            VerticalContentAlignment="Center"
            Foreground="White"
            Background="White"
            BorderBrush="#0091EA" Margin="5,0,0,0" Height="90" Width="90">
            <Button.Template>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border BorderThickness="{TemplateBinding BorderThickness}"
            BorderBrush="{TemplateBinding BorderBrush}"
            Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </Border>
                </ControlTemplate>
            </Button.Template>

我想实现这样的目标,所以我想删除 xaml 按钮,并在需要时以编程方式创建相同的按钮,具有相同的样式和我在 [=52= 中所做的一切],例如我想如何创建它:

 private void AddSubmenuButton(string name)
 {
            MyButton button = new MyButton();
            button.ButtonText = name;
 }

这可能吗?如果可能:怎么做?

P.S 我试过经典方式,像这样:

  Button a = new Button();
            a.BorderThickness = new Thickness(1);
            a.Foreground = new SolidColorBrush(Colors.Black);
            a.BorderBrush = mySolidColorBrush;
            a.Content = "Button1";
            a.Width = 90;
            a.Height = 90;

但我明白了:

可能会注意到厚度根本不是 1,它有一些奇怪的值 而且我不知道如何更改它。这就是我在 xaml 中创建按钮的原因,它看起来更漂亮,我想从代码后面调用 it/create 它。

编辑:

@mm8 非常感谢! 就是这样!

这太棒了,但是那又如何呢?如果我想在我的按钮中放置图标 + 文本,我通常会 添加这样的内容,对我来说很好:

<Grid>
    <Grid.RowDefinitions>
    <RowDefinition Height="80*">
    <RowDefinition Height="20*"/>
</Grid.RowDefinitions>
    <Image Source="/MyProject.Wpf;component/Icons/customer-icon.png" Margin="10" Grid.Row="0" Width="Auto"/>
    <TextBlock Foreground="Black" Margin="0,0,0,3" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" >Izlaz</TextBlock>
</Grid>

如您所见,我会将网格添加到我的按钮,它看起来像这样:

<Button x:Name="btnAddNewItem"
                    Grid.Column="0" Grid.Row="0"
                    FontSize="15"
                    ToolTip="Podaci"
                    BorderThickness="1.5"
                    HorizontalContentAlignment="Center"
                    VerticalContentAlignment="Center"
                    Foreground="White"
                    Background="White"
                    BorderBrush="#0091EA" Margin="5,0,0,0" Height="90" Width="90">
                    <Button.Template>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border BorderThickness="{TemplateBinding BorderThickness}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    Background="{TemplateBinding Background}">
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                            </Border>
                        </ControlTemplate>
                    </Button.Template>
        <Grid>
         <Grid.RowDefinitions>
            <RowDefinition Height="80*">
            <RowDefinition Height="20*"/>
        </Grid.RowDefinitions>
            <Image Source="/MyProject.Wpf;component/Icons/customer-icon.png" Margin="10" Grid.Row="0" Width="Auto"/>
            <TextBlock Foreground="Black" Margin="0,0,0,3" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" >Izlaz</TextBlock>
        </Grid>
                    </Button>

那么我能否以某种方式实现这个(图像 + 我的按钮中的文本)也以编程方式创建它。

在您的 App.xaml 中定义样式:

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApplication1"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style x:Key="theStyle" TargetType="Button">
            <Setter Property="FontSize" Value="15" />
            <Setter Property="BorderThickness" Value="1.5" />
            <Setter Property="HorizontalContentAlignment" Value="Center" />
            <!-- and so on for each property...-->
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border BorderThickness="{TemplateBinding BorderThickness}"
            BorderBrush="{TemplateBinding BorderBrush}"
            Background="{TemplateBinding Background}">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>
</Application>

然后您可以将此样式应用到应用程序中的任何按钮,可以是 XAML:

<Button x:Name="btnAddNewItem" Style="{StaticResource myStyle}"/>

...或以编程方式:

Button button = new Button();
button.Style = Application.Current.Resources["myStyle"] as Style;