关于wpf设置属性的几种方式
来源:互联网 发布:社会调查数据方法 编辑:程序博客网 时间:2024/06/06 05:20
注意:这个设置没有优先级,同一个属性只能设置一次。
1 Local control specific style
这种方式仅仅只是对这个控件有效!
==================================code
<Grid Margin="10">
<TextBlock Text="Style test">
<TextBlock.Style>
<Style>
<Setter Property="TextBlock.FontSize" Value="36" />
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
======================================
2 Local child control style
这种做法会对Resources的子控件或者子控件的子控件起作用。这对于更多的本地需求是很好的,你只需要一组控件看起来相同,而不是在每个
对象上设计单独属性
===================================code
<StackPanel Margin="10">
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Gray" />
<Setter Property="FontSize" Value="24" />
</Style>
</StackPanel.Resources>
<TextBlock>Header 1</TextBlock>
<TextBlock>Header 2</TextBlock>
<TextBlock Foreground="Blue">Header 3</TextBlock>
</StackPanel>
3 Window-wide styles
定义windows中的资源样式,在上级别的范围上有增强了范围
===========================================
<Window x:Class="WpfTutorialSamples.Styles.WindowWideStyleSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowWideStyleSample" Height="200" Width="300">
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Gray" />
<Setter Property="FontSize" Value="24" />
</Style>
</Window.Resources>
<StackPanel Margin="10">
<TextBlock>Header 1</TextBlock>
<TextBlock>Header 2</TextBlock>
<TextBlock Foreground="Blue">Header 3</TextBlock>
</StackPanel>
</Window>
4 Application-wide styles
如果您希望您的样式在整个应用程序中在不同的窗口中使用,您可以为整个应用程序定义它。
这是在Visual Studio可能为您创建的App.xaml文件中完成的
==============================code
<Application x:Class="WpfTutorialSamples.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Styles/WindowWideStyleSample.xaml">
<Application.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Gray" />
<Setter Property="FontSize" Value="24" />
</Style>
</Application.Resources>
</Application>
======================
<Window x:Class="WpfTutorialSamples.Styles.WindowWideStyleSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ApplicationWideStyleSample" Height="200" Width="300">
<StackPanel Margin="10">
<TextBlock>Header 1</TextBlock>
<TextBlock>Header 2</TextBlock>
<TextBlock Foreground="Blue">Header 3</TextBlock>
</StackPanel>
</Window>
5 Explicitly using styles
这种方式只有同时满足key和targetType的时候,样式才生效。
===============================================================
<Window x:Class="WpfTutorialSamples.Styles.ExplicitStyleSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ExplicitStyleSample" Height="150" Width="300">
<Window.Resources>
<Style x:Key="HeaderStyle" TargetType="TextBlock">
<Setter Property="Foreground" Value="Gray" />
<Setter Property="FontSize" Value="24" />
</Style>
</Window.Resources>
<StackPanel Margin="10">
<TextBlock>Header 1</TextBlock>
<TextBlock Style="{StaticResource HeaderStyle}">Header 2</TextBlock>
<TextBlock>Header 3</TextBlock>
</StackPanel>
</Window>
1 Local control specific style
这种方式仅仅只是对这个控件有效!
==================================code
<Grid Margin="10">
<TextBlock Text="Style test">
<TextBlock.Style>
<Style>
<Setter Property="TextBlock.FontSize" Value="36" />
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
======================================
2 Local child control style
这种做法会对Resources的子控件或者子控件的子控件起作用。这对于更多的本地需求是很好的,你只需要一组控件看起来相同,而不是在每个
对象上设计单独属性
===================================code
<StackPanel Margin="10">
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Gray" />
<Setter Property="FontSize" Value="24" />
</Style>
</StackPanel.Resources>
<TextBlock>Header 1</TextBlock>
<TextBlock>Header 2</TextBlock>
<TextBlock Foreground="Blue">Header 3</TextBlock>
</StackPanel>
3 Window-wide styles
定义windows中的资源样式,在上级别的范围上有增强了范围
===========================================
<Window x:Class="WpfTutorialSamples.Styles.WindowWideStyleSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowWideStyleSample" Height="200" Width="300">
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Gray" />
<Setter Property="FontSize" Value="24" />
</Style>
</Window.Resources>
<StackPanel Margin="10">
<TextBlock>Header 1</TextBlock>
<TextBlock>Header 2</TextBlock>
<TextBlock Foreground="Blue">Header 3</TextBlock>
</StackPanel>
</Window>
4 Application-wide styles
如果您希望您的样式在整个应用程序中在不同的窗口中使用,您可以为整个应用程序定义它。
这是在Visual Studio可能为您创建的App.xaml文件中完成的
==============================code
<Application x:Class="WpfTutorialSamples.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Styles/WindowWideStyleSample.xaml">
<Application.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Gray" />
<Setter Property="FontSize" Value="24" />
</Style>
</Application.Resources>
</Application>
======================
<Window x:Class="WpfTutorialSamples.Styles.WindowWideStyleSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ApplicationWideStyleSample" Height="200" Width="300">
<StackPanel Margin="10">
<TextBlock>Header 1</TextBlock>
<TextBlock>Header 2</TextBlock>
<TextBlock Foreground="Blue">Header 3</TextBlock>
</StackPanel>
</Window>
5 Explicitly using styles
这种方式只有同时满足key和targetType的时候,样式才生效。
===============================================================
<Window x:Class="WpfTutorialSamples.Styles.ExplicitStyleSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ExplicitStyleSample" Height="150" Width="300">
<Window.Resources>
<Style x:Key="HeaderStyle" TargetType="TextBlock">
<Setter Property="Foreground" Value="Gray" />
<Setter Property="FontSize" Value="24" />
</Style>
</Window.Resources>
<StackPanel Margin="10">
<TextBlock>Header 1</TextBlock>
<TextBlock Style="{StaticResource HeaderStyle}">Header 2</TextBlock>
<TextBlock>Header 3</TextBlock>
</StackPanel>
</Window>
阅读全文
0 0
- 关于wpf设置属性的几种方式
- WPF : ListBox的几种Template属性
- WPF-ListBox的几种Template属性
- WPF应用程序设置启动时自动打开的第一个窗口的几种方式
- WPF应用程序设置启动时自动打开的第一个窗口的几种方式
- WPF中使用cs代码设置附加属性的方式
- WPF中使用文件浏览对话框的几种方式
- WPF中的几种处理线程的工作方式:
- WPF MVVM之INotifyPropertyChanged接口的几种实现方式
- WPF中使用文件浏览对话框的几种方式
- WPF中使用文件浏览对话框的几种方式
- 操作元素属性的几种方式
- 检测对象属性的几种方式
- 属性动画的几种实现方式
- 设置元素浮动的几种方式
- 设置监听器的几种方式
- 设置CSS的几种方式
- PHP COOKIE的几种设置方式
- 阿里杨勇:浅谈 Linux 高负载的系统化分析
- 辅助类
- NVIDIA TK1 学习笔记(8):自动连接wifi
- 关于用spring-Mybatis用maven构建框架
- 距离和相似度度量方法
- 关于wpf设置属性的几种方式
- PXE全自动远程安装
- JAVA四大域对象总结
- adb命令
- 小图标组件
- pyCharm2017.3 激活码 注册码 最新
- 关于虚拟网络和tomcat配置的问题
- 作业整理12.2
- C# imgage图片转base64字符/base64字符串转图片