簡體   English   中英

如何從后面的代碼中“調用”我的樣式按鈕(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按鈕,並在需要時以編程方式創建相同的按鈕,並使用相同的樣式以及在xaml中所做的所有操作,舉例說明如何創建該按鈕:

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

這是否可能,如果可以:如何?

PS我嘗試了經典的方式,像這樣:

  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中創建了看起來更漂亮的按鈕的原因,我想稱之為/從后面的代碼創建它。

編輯:

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

如您所見,我會將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>

因此,我也可以通過編程方式實現此目的(圖片+文本輸入MY按鈕)。

在您的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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM