积极答复者
为什么自定义窗体基类使用DefaultStyleKeyProperty设置Style窗体会变成黑的...

问题
-
请问各位 如题....
不管我在Style中写什么都一样...
使用这个设置默认样式会把基承的窗体内容部份全变成黑的.什么也看不到.
static MyWindow()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyWindow), new FrameworkPropertyMetadata(typeof(MyWindow)));
}使用下面的就没有问题...
public MyWindow()
{
Style = App.Current.FindResource("MyWindowStyle") as Style;}
并且这两个如果一起写出来的话窗体也是黑的..只要有上面那个存在就是黑的...
但我写别的控件时使用上面那个设置样式一点问题也没有为什么窗体不行呢????
Style内容是什么都没用..我不写都 一样
<Style x:Key="MyWindowStyle" TargetType="{x:Type MyWindow}">
</Style>
答案
-
这个是Window的默认样式,请参考这个修改你的样式:
<Style x:Key="{x:Type Window}" TargetType="{x:Type Window}"> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Window}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <AdornerDecorator> <ContentPresenter/> </AdornerDecorator> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="Window.ResizeMode" Value="CanResizeWithGrip"> <Setter Property="Template" Value="{StaticResource WindowTemplateKey}"/> </Trigger> </Style.Triggers> </Style>
加上:<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
就好了Bob Bao [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 Jie BaoModerator 2012年4月6日 8:41
全部回复
-
这个是Window的默认样式,请参考这个修改你的样式:
<Style x:Key="{x:Type Window}" TargetType="{x:Type Window}"> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Window}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <AdornerDecorator> <ContentPresenter/> </AdornerDecorator> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="Window.ResizeMode" Value="CanResizeWithGrip"> <Setter Property="Template" Value="{StaticResource WindowTemplateKey}"/> </Trigger> </Style.Triggers> </Style>
加上:<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
就好了Bob Bao [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 Jie BaoModerator 2012年4月6日 8:41