图标资源下载 iconfont
简单样例
<Window.Resources>
<Style x:Key="DefaultText" TargetType="TextBlock">
"FontSize"
Value="50" />
Style>
Window.Resources>
用于设置控件属性
<Style x:Key="DefaultText" TargetType="TextBlock">
"FontSize"
Value="50" />
Style>
<Style x:Key="DefaultText" TargetType="TextBlock">
"FontSize"
Value="50" />
Style>
使用属性使用Style=“{StaticResource 样式名}”
<TextBlock Text="文字" Style="{StaticResource DefaultText}" />
但满足一个条件时动态触发,对属性进行修改
<Style x:Key="DefaultText" TargetType="TextBlock">
"FontSize" Value="60" />
"Foreground" Value="Green" />
"IsMouseOver" Value="True">
"FontSize" Value="100"/>
"Foreground" Value="Red"/>
"FontWeight" Value="Bold"/>
Style>
Property是触发属性,Value是值。即Property=value的时候触发
常用的触发属性
事例
namespace WpfApp2
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
private MainWindowViewModel ViewModel { get; set; }
public MainWindow()
{
ViewModel = new MainWindowViewModel();
this.DataContext = ViewModel;
InitializeComponent();
}
}
public class MainWindowViewModel
{
public string Title { get; set; }
public Person Person { get; set; }
public List<Person> Persons { get; set; }
public MainWindowViewModel()
{
Title = "我是标题";
Person = new Person()
{
Name = "小刘",
Age = 26
};
Persons = new List<Person> {
new Person()
{
Name = "小明",
Age = 26
},
new Person()
{
Name = "小红",
Age = 26
},
new Person()
{
Name = "小兰",
Age = 26
},
};
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}
绑定使用{Binding value}的形式
<TextBlock Text="{Binding Title}" FontSize="50" />
<UniformGrid Columns="2"
DataContext="{Binding Person}">
<TextBlock Text="{Binding Name}"
FontSize="50" />
<TextBlock Text="{Binding Age}"
FontSize="50" />
UniformGrid>
用于绑定集合类型的数据
<ItemsControl ItemsSource="{Binding Persons}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
ItemsPanelTemplate>
ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<UniformGrid Columns="2">
<TextBlock Text="{Binding Name}"
FontSize="50" />
<TextBlock Text="{Binding Age}"
FontSize="50" />
UniformGrid>
DataTemplate>
ItemsControl.ItemTemplate>
ItemsControl>