簡體   English   中英

app.xaml中的WPF按鈕模板如何在MainWindow.xaml中設置文本

[英]WPF button template in app.xaml how to set the text in MainWindow.xaml

我已經在app.xaml文件中創建了一個玻璃按鈕(本文的代碼底部)。 我在mainwindox.xaml中有一些引用此模板的按鈕。 我不知道該怎么辦,是在MainWindow中設置按鈕顯示的文本。 下面的代碼的第一部分是其中一個按鈕的示例。 我需要在某個地方添加一些代碼,以便按鈕上帶有文本“ Correlation”。

下面的主窗口代碼

 <Button Grid.Column="0" Grid.Row="1" Style="{StaticResource buttBasicTemplateReport}" Command="{Binding CommandButtReportsCorrel}" Height="50" Width="120" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,0"/>                    

下面的App.xaml代碼

         <!-- Glass Button empty template Report -->
    <Style x:Key="buttBasicTemplateReport" TargetType="Button">
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="FontSize" Value="12" />
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="ButtonBorder" 
                              CornerRadius="15,15,15,15" 
                              BorderThickness="3,3,3,3" 
                              Background="#AA000000"  
                              BorderBrush="#99FFFFFF"
                              RenderTransformOrigin="0.5,0.5">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="1.7*"/>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <Border Grid.Row="0" Grid.ColumnSpan="2" CornerRadius="23,23,0,0">
                                <Border.Background>
                                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                        <GradientStop Color="#08FFFFFF" Offset="0"/>
                                        <GradientStop Color="#88FFFFFF" Offset="1"/>
                                    </LinearGradientBrush>
                                </Border.Background>
                            </Border>
                            <ContentPresenter x:Name="ButtonContentPresenter"
                            VerticalAlignment="Center"  
                            Grid.RowSpan="2" 
                            HorizontalAlignment="Center"/>
                            <Rectangle x:Name="recGlow"  Style="{StaticResource recSecurity}"
                                       Grid.Row="0" Grid.RowSpan="2" Grid.Column="0" Grid.ColumnSpan="2"/>
                            <TextBlock Grid.Row="0" Grid.RowSpan="2" Grid.ColumnSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center" Style="{StaticResource txtSecurity}"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver"  Value="True">
                            <Setter TargetName="recGlow" Property="Opacity" Value="0.8"/>
                            <Setter Property="RenderTransform" TargetName="ButtonBorder">
                                <Setter.Value>
                                    <TransformGroup>
                                        <ScaleTransform ScaleX="1" ScaleY="1"/>
                                    </TransformGroup>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

ButtonContentControl 如我所見,您的Template有一個ContentPresenter 因此,您可以將Content屬性添加到Button或添加Contnet元素。

按屬性:

<Button Content="Correlation"/>

按元素:

<Button>Correlation</Button>

制作自定義按鈕時,如果要使用onClick效果,則需要使用ContentControl和Triggers。 另外,要回答來自comentar的問題,當您制作自定義按鈕時,需要將邊距綁定到padding屬性以使按鈕內容居中。

您可以看到以下鏈接:

WPF:為什么我不應該在ControlTemplate中使用{TemplateBinding Margin}-Margin僅用於元素的容器?

您還可以看到以下內容:

http://sshumakov.com/2013/02/19/how-to-bind-to-control-properties-from-controltemplate/

暫無
暫無

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

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