如何从后面的代码中“调用”我的样式按钮(xaml)

时间:2017-01-11 08:06:25

标签: c# wpf button styles programmatically

我在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按钮,并在需要时以编程方式创建相同的按钮,具有相同的样式和我在xaml中所做的一切,示例我想如何创建它:

 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;

但我明白了:

enter image description here

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

修改

@ mm8谢谢你! 就是这样!

这是很棒的伴侣,但那又怎么样如果我想在我的按钮中放置图标+文字,我会ussualy 添加这样的东西,对我来说很好:

<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>

你可以看到我会将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>

所以我可以以某种方式实现这一点(图像+文本在我的按钮)也以编程方式创建它。

1 个答案:

答案 0 :(得分:2)

在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:

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

...或以编程方式:

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