WPF 继承窗口样式的问题

ilikeff8 2017-05-25 06:04:35

<Style TargetType="{x:Type Window}" x:Key="BaseWindow">
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="AllowsTransparency" Value="True" />
<Setter Property="Width" Value="800"/>
<Setter Property="Height" Value="600"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Background="White">
<DockPanel LastChildFill="True">
<Grid Width="auto" Height="80" VerticalAlignment="Top" HorizontalAlignment="Stretch" DockPanel.Dock="Top">
<Viewbox Stretch="Fill">
<Canvas Width="790" Height="80">
<StackPanel>
<DockPanel Name="window" MouseMove="window_MouseMove" MouseDown="window_MouseDown" Background="#4C9CFB" Width="790" Height="40" >
<TextBlock Name="txtTitle" Text=" AAA" FontSize="18" FontFamily="微软雅黑" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Button Name="btnClose" Content="X" Width="25" Height="25" DockPanel.Dock="Right" HorizontalAlignment="Right" Margin="3,0" Click="btnClose_Click"/>
</DockPanel>
</StackPanel>
</Canvas>
</Viewbox>
</Grid>
<AdornerDecorator>
<ContentPresenter />
</AdornerDecorator>
</DockPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


把样式指定给一个窗口

<Window x:Class="ApiDemo2.View.BaseWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ApiDemo2.View"
mc:Ignorable="d" Style="{StaticResource BaseWindow}"
Title="BaseWindow" WindowStartupLocation="CenterScreen">
<Grid>
<Button>a</Button>
</Grid>
</Window>


但按钮根本没有显示出来,
<AdornerDecorator>
<ContentPresenter />
</AdornerDecorator>

没发挥作用


...全文
153 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
exception92 2017-05-26
  • 打赏
  • 举报
回复
txtTitle 也可以这样写:
Text="{Binding Title}"
exception92 2017-05-26
  • 打赏
  • 举报
回复
 <ContentPresenter />
-》改为
<ContentPresenter Content="{Binding Content}" />
  • 打赏
  • 举报
回复
你在ContentPresenter 里面加上 <ContentPresenter Content="{TemplateBinding ContentControl.Content}" />试试,你只放了一个ContentPresenter ,但是没有给他的内容指定值。上面那句话的意思是模板绑定,将窗口的内容显示在ContentPresenter 中。
ilikeff8 2017-05-25
  • 打赏
  • 举报
回复
这个例子是简化了的,还是一样的问题,具体窗口有很多的内容,全都没有显示

110,555

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧