诸位老师好。我有一个需求:
   在wpf中窗体的title居中显示。目前是居左显示的。
  如图效果。
     如何能让其居中显示呢。
   多谢了。

解决方案 »

  1.   

    楼主可以把wpf window的  windowstyle 设为none,然后用border来绘制类似windowstyle的效果 这样就可以轻而易举的实现文字居中效果:
    例如<Window x:Class="STLogisticsView.STLMain"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            WindowStyle="None" ResizeMode="NoResize" Background="#00000000" x:Name="MainWindow" AllowsTransparency="True" >
    <Border style="{Binding ....}">
    <Grid>
    </Grid>
    </Border>
    </Window>网上搜一下Border的style设置,很多的,也很简单
      

  2.   

    修改window的Style吧   给你截其中一段代码  你只修改对应标题的地方就行了 <ControlTemplate x:Key="CustomMainWindowControlTemplate"  TargetType="{x:Type Window}">
            <Border Background="Transparent"  BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0" CornerRadius="10,10,0,0">
                <Grid Width="Auto" Height="Auto" VerticalAlignment="Stretch">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="16"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Border x:Name="topborder" Background="#2B2B2B" Margin="0" Grid.Column="0" BorderBrush="{x:Null}" BorderThickness="0"  Grid.ColumnSpan="1" VerticalAlignment="Bottom">
                        <Grid HorizontalAlignment="Stretch" Margin="0" VerticalAlignment="Stretch" Width="Auto" Height="Auto">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="9.27"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="28.207"/>
                                <ColumnDefinition Width="30.016"/>
                                <ColumnDefinition Width="27.398"/>
                                <ColumnDefinition Width="7.586"/>
                            </Grid.ColumnDefinitions>
                            <Label Background="Transparent" Grid.ColumnSpan="7" x:Name="rMouseEvent"/>
                            <TextBlock Margin="5.332,4,0,0" Grid.Column="1" Text="{TemplateBinding Title}" HorizontalAlignment="Right" Width="Auto" FontFamily="微软雅黑" Height="20" FontSize="13"  Foreground="White" VerticalAlignment="Bottom"/>
                            <Gs:CGsLogoButton   x:Name="btnMin" Margin="2.668,0,2.777,0"  Grid.Column="3" HorizontalAlignment="Right"  MouseUpImagePath="Gs/Images/MaxMinButton/MinNormal.png" VerticalAlignment="Bottom" MouseOverImagePath="Gs/Images/MaxMinButton/MinMouseOver.png" MouseDownImagePath="Gs/Images/MaxMinButton/MinSelected.png"  />
                            <Gs:CGsLogoButton   x:Name="btnMax" Margin="3.891,0,3.363,0"  Grid.Column="4" HorizontalAlignment="Right"  MouseUpImagePath="Gs/Images/MaxMinButton/MaxNormal.png" VerticalAlignment="Bottom" MouseOverImagePath="Gs/Images/MaxMinButton/MaxMouseOver.png" MouseDownImagePath="Gs/Images/MaxMinButton/MaxSelected.png" Visibility="Visible" />
                            <Gs:CGsLogoButton   x:Name="btnRecover" Margin="3.891,0,3.363,0"  Grid.Column="4" HorizontalAlignment="Right"  MouseUpImagePath="Gs/Images/MaxMinButton/RecoverNormal.png" VerticalAlignment="Bottom" MouseOverImagePath="Gs/Images/MaxMinButton/RecoverOver.png" MouseDownImagePath="Gs/Images/MaxMinButton/RecoverSelected.png" Visibility="Collapsed" />
                            <Gs:CGsLogoButton   x:Name="btnClose" Margin="2.668,0,1.968,0"  Grid.Column="5" HorizontalAlignment="Right" MouseUpImagePath="Gs/Images/MaxMinButton/CloseNormal1.png" VerticalAlignment="Bottom" MouseOverImagePath="Gs/Images/MaxMinButton/CloseMouseOver.png" MouseDownImagePath="Gs/Images/MaxMinButton/CloseSelected.png"  />                      
                        </Grid>
                    </Border>
      

  3.   

     <ControlTemplate x:Key="CustomMainWindowControlTemplate"  TargetType="{x:Type Window}">
            <Border Background="Transparent"  BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0" CornerRadius="10,10,0,0">
                <Grid Width="Auto" Height="Auto" VerticalAlignment="Stretch">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="16"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Border x:Name="topborder" Background="#2B2B2B" Margin="0" Grid.Column="0" BorderBrush="{x:Null}" BorderThickness="0"  Grid.ColumnSpan="1" VerticalAlignment="Bottom">
                        <Grid HorizontalAlignment="Stretch" Margin="0" VerticalAlignment="Stretch" Width="Auto" Height="Auto">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="9.27"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="28.207"/>
                                <ColumnDefinition Width="30.016"/>
                                <ColumnDefinition Width="27.398"/>
                                <ColumnDefinition Width="7.586"/>
                            </Grid.ColumnDefinitions>
                            <Label Background="Transparent" Grid.ColumnSpan="7" x:Name="rMouseEvent"/>
                            <TextBlock Margin="5.332,4,0,0" Grid.Column="1" Text="{TemplateBinding Title}" HorizontalAlignment="Right" Width="Auto" FontFamily="微软雅黑" Height="20" FontSize="13"  Foreground="White" VerticalAlignment="Bottom"/>
                            <Gs:CGsLogoButton   x:Name="btnMin" Margin="2.668,0,2.777,0"  Grid.Column="3" HorizontalAlignment="Right"  MouseUpImagePath="Gs/Images/MaxMinButton/MinNormal.png" VerticalAlignment="Bottom" MouseOverImagePath="Gs/Images/MaxMinButton/MinMouseOver.png" MouseDownImagePath="Gs/Images/MaxMinButton/MinSelected.png"  />
                            <Gs:CGsLogoButton   x:Name="btnMax" Margin="3.891,0,3.363,0"  Grid.Column="4" HorizontalAlignment="Right"  MouseUpImagePath="Gs/Images/MaxMinButton/MaxNormal.png" VerticalAlignment="Bottom" MouseOverImagePath="Gs/Images/MaxMinButton/MaxMouseOver.png" MouseDownImagePath="Gs/Images/MaxMinButton/MaxSelected.png" Visibility="Visible" />
                            <Gs:CGsLogoButton   x:Name="btnRecover" Margin="3.891,0,3.363,0"  Grid.Column="4" HorizontalAlignment="Right"  MouseUpImagePath="Gs/Images/MaxMinButton/RecoverNormal.png" VerticalAlignment="Bottom" MouseOverImagePath="Gs/Images/MaxMinButton/RecoverOver.png" MouseDownImagePath="Gs/Images/MaxMinButton/RecoverSelected.png" Visibility="Collapsed" />
                            <Gs:CGsLogoButton   x:Name="btnClose" Margin="2.668,0,1.968,0"  Grid.Column="5" HorizontalAlignment="Right" MouseUpImagePath="Gs/Images/MaxMinButton/CloseNormal1.png" VerticalAlignment="Bottom" MouseOverImagePath="Gs/Images/MaxMinButton/CloseMouseOver.png" MouseDownImagePath="Gs/Images/MaxMinButton/CloseSelected.png"  />                      
                        </Grid>
                    </Border>照着上面的代码修改window的Style吧  红色的部分就是修改window 标题栏的代码
      

  4.   

    只贴了一部分代码  用Blend只需要修改你需要的部分