假如我要让Button的Background保持一致,我可以这样:
<Style TargetType="Button">
<Setter Property="Background" Value="Gray"/>
</Style>这样我以后使用的Button,就都是这个风格的了,尤其重要的是,我没有明确指定Style的x:Key,这使得我不需要在每个
<Button Style="..."
现在我希望所有的Control都是这个风格,而并非只是Button,于是我写了一个Style
<Style TargetType="Control">
<Setter Property="Background" Value="Gray"/>
</Style>虽然Button,Label,TextBox等都是Control的派生类,但是直接
<Button />
<Label/>
却不行必须:
<Button Style="{StaticResource {x:Type Control}}" ...
<Label Style="{StaticResource {x:Type Control}}" ...
<Button Style="{StaticResource {x:Type Control}}" ...我的控件很多,每个都这样写,相当麻烦
目前我只找到一种解决办法:<Style TargetType="Control">
<Setter Property="Background" Value="Gray"/>
</Style>
<Style TargetType="Control" BasedOn="{StaticResource {x:Type Control}}"/>
<Style TargetType="Label" BasedOn="{StaticResource {x:Type Control}}"/>
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type Control}}"/>这样我虽然不需要为每一个控件指定Style,但是还是必须为每一类控件指明,也比较麻烦这些控件都有继承关系,难道就没有什么简单的办法?
<Style TargetType="Button">
<Setter Property="Background" Value="Gray"/>
</Style>这样我以后使用的Button,就都是这个风格的了,尤其重要的是,我没有明确指定Style的x:Key,这使得我不需要在每个
<Button Style="..."
现在我希望所有的Control都是这个风格,而并非只是Button,于是我写了一个Style
<Style TargetType="Control">
<Setter Property="Background" Value="Gray"/>
</Style>虽然Button,Label,TextBox等都是Control的派生类,但是直接
<Button />
<Label/>
却不行必须:
<Button Style="{StaticResource {x:Type Control}}" ...
<Label Style="{StaticResource {x:Type Control}}" ...
<Button Style="{StaticResource {x:Type Control}}" ...我的控件很多,每个都这样写,相当麻烦
目前我只找到一种解决办法:<Style TargetType="Control">
<Setter Property="Background" Value="Gray"/>
</Style>
<Style TargetType="Control" BasedOn="{StaticResource {x:Type Control}}"/>
<Style TargetType="Label" BasedOn="{StaticResource {x:Type Control}}"/>
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type Control}}"/>这样我虽然不需要为每一个控件指定Style,但是还是必须为每一类控件指明,也比较麻烦这些控件都有继承关系,难道就没有什么简单的办法?
//默认控件颜色
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Gray"/>
//默认控件上的文本颜色
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Red"/>
有3个提示
1.app.xaml 的resources里面
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Gray"/>
对button是有效的 其他的估计要自己查一下
2.在app.cs里自定义app构造函数
public app()
{
System.Windows.Controls.Control.OverridesDefaultStyleProperty.OverrideMetadata(...)
}
3.如果以上的都不行
在app.xaml 的resources里面
你说的只找到一种解决办法 这个了
大哥,你玩呢?对Button有效?你回去试了再说,根本没用
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Blue"/>
</Application.Resources>
</Application>
<Window x:Class="Tri.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"> <Grid>
<Button>
</Button>
</Grid>
</Window>
1.因为并不是所有控件的Background属性默认的值是SystemColors.ControlBrush,比如Button就不是,所以这种方法不行;
2.System.Windows.Controls.Control.OverridesDefaultStyleProperty.OverrideMetadata(...)
你这个只是把Control的默认Style改写了一下,而没有把所有控件的默认Style改写。所以这种方法也不可行。比较可行的是遍历visualtree,然后改写所有控件的对应属性。不过我觉得没这个必要,楼主这个需求有点奇怪。如果说清楚为什么要这样做,或者有别的方法可以解决
别人好心帮你回答问题,你以为你是谁啊?你什么都回,还跑到这上面问什么问题。
你看看你对1楼什么态度
我对1楼什么态度?我不认为我有什么态度,你太敏感了吧........
"你要改字体就说改字体,说什么改Background。"==> 你也能算程序员?我说的改Background或者字体,都只是举个例,并没有限定特定的属性,你再看看标题,我用的是词是“属性”,你这么说,好像我是在强调字体似的......
你还说我要求奇怪,我倒看你思路太封锁,说一是一,不要说举一反三,反1.1都困难......比如我想限定程序的字体一律是“宋体 12号”,真的应该好好学学中文了,看看“比如”是举例的意思,还是特指的意思.........我就对你很无语了,现在我只能说:我很无奈.......