WPF中的模板(一)- DataTemplate

        WPF中DataTemplate和ControlTemplate的一直感觉无法区分明白,后面看了两遍书,第二遍时才感觉有一点点明白这两个之间的区别。
这里先介绍下我对DataTemplate的理解:
一、先说定义
DataTemplate,顾名思义是数据模板,从字面上理解是数据的表现形式,书上也都这么解释,并以此作为和ControlTemplate区分的标准。但是个人感觉这种解释太过于抽象,其实数据的表现形式和控件的外在表现(ControlTemplate)很不容易区分。比如对于ListBoxItem中嵌入CheckBox,通过DataTemplate和ControlTemplate都可以实现,这时候DataTemplate和ControlTemplate至少在用户看来是无法区分的。
要区分DataTemplate和ControlTemplate,我觉得应该还是从使用的角度入手。
二、DataTenpate最常用的地方。
1、ContentControl的ContentTemplate属性。
凡是继承自ContentControl的控件都带有ContentTemplate属性(常见的控件包括各类Button、ScrollViewer等),而此类控件都有一个ContentTemplate属性,可以用我们自定义的DataTenpate对其进行赋值。以下例子是定义一个包含CheckBox和TextBlock的DataTenpate,将其赋值给Button
、Label、ScrollViewer三个继承自ContentControl的控件。

XAML代码如下:

<Window x:Class="_11_4.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">
    <Window.Resources>
        <DataTemplate x:Key="datatemplate1">
            <StackPanel Orientation="Horizontal">
                <CheckBox/>
                <TextBlock Text=" Hello World"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="153,26,0,0" Name="button1" VerticalAlignment="Top" Width="130" 
                ContentTemplate="{StaticResource datatemplate1}"/>
        <Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="153,68,0,0" Name="label1" VerticalAlignment="Top" Width="130" 
               ContentTemplate="{StaticResource datatemplate1}"/>
        <ScrollViewer Height="23" HorizontalAlignment="Left" Margin="153,127,0,0" Name="scrollViewer1" VerticalAlignment="Top" Width="120" 
                ContentTemplate="{StaticResource datatemplate1}"/>
        
    </Grid>
</Window>

运行效果如下:

2、ItemsControl的ItemTemplate属性
包括ListView、TreeView、ListBox在内,很多继承自ItemsControl的Collection<T>型控件都包含ItemTemplate属性,我们可以直接通过给ItemTemplate赋值的方式控制其item数据的显示。

<Window x:Class="_11_4.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">
    <Window.Resources>
        <DataTemplate x:Key="datatemplate1">
            <StackPanel Orientation="Horizontal">
                <CheckBox/>
                <TextBlock Text=" Hello World"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ListView Height="100" HorizontalAlignment="Left" Margin="127,12,0,0" Name="listView1" VerticalAlignment="Top" Width="120"
                  >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox/>
                        <TextBlock Text=" Hello World"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListItem/>
            <ListItem/>
        </ListView>
        <ListBox Height="100" HorizontalAlignment="Left" Margin="127,139,0,0" Name="listBox1" VerticalAlignment="Top" Width="120"
                 ItemTemplate="{StaticResource datatemplate1}">
            <ListItem/>
            <ListItem/>
        </ListBox>
        <TreeView Height="100" HorizontalAlignment="Left" Margin="366,160,0,0" Name="listView2" VerticalAlignment="Top" Width="120"
                  ItemTemplate="{StaticResource datatemplate1}">
            <ListItem/>
        </TreeView>
    </Grid>
</Window>


以上代码,展示了给ItemTemplate赋值的两种方式,一种是引用静态资源,另一种是直接对ItemTemplate用DataTemplate进行赋值。两种形式的实现效果是一样的。


3、GridViewColumn的CellTemplate属性。

<Window x:Class="_11_4.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">
    <Window.Resources>
        <DataTemplate x:Key="datatemplate1">
            <StackPanel Orientation="Horizontal">
                <CheckBox/>
                <TextBlock Text=" Hello World"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ListView>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Id" CellTemplate="{StaticResource datatemplate1}"/>
                </GridView>
            </ListView.View>
            <ListItem/>
        </ListView>
    </Grid>
</Window>

以上代码,将一个GridView对象赋值给ListView对象的View属性,并在GridView对象的GridViewColumn属性中,将CellTemplate绑定静态资源datatemplate1,运行效果如下:


4、DataGridTemplateColumn的CellTemplate属性。
该种应用同上面的类似,只不过是在DataGrid控件上的使用:

<Window x:Class="_11_4.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>
        <DataGrid Name="datagrid">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="ID">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <CheckBox/>
                                <TextBlock Text=" Hello World"/>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MVVM(Model-View-ViewModel)是一种经典的WPF编程模式,它将应用程序分为三个部分:模型(Model)、视图(View)和视图模型(ViewModel)。其,模型表示业务数据和规则,视图显示用户界面,视图模型则充当模型和视图之间的介,负责协调数据和业务逻辑与视图的交互。 下面我将通过一个简单的示例来讲解MVVM模式在WPF的应用。 首先,我们需要创建一个WPF应用程序,并添加一个MainWindow.xaml文件作为主窗口。 然后,我们定义一个模型类,表示一个人员信息,包括姓名和年龄: ```csharp public class Person { public string Name { get; set; } public int Age { get; set; } } ``` 接着,我们创建一个视图模型类,负责管理人员信息的增删改查操作,并与视图进行交互。在这个示例,我们只实现了添加和删除功能: ```csharp public class ViewModel : INotifyPropertyChanged { private ObservableCollection<Person> _persons; public ObservableCollection<Person> Persons { get { return _persons; } set { _persons = value; OnPropertyChanged(nameof(Persons)); } } private string _name; public string Name { get { return _name; } set { _name = value; OnPropertyChanged(nameof(Name)); } } private int _age; public int Age { get { return _age; } set { _age = value; OnPropertyChanged(nameof(Age)); } } public RelayCommand AddCommand { get; set; } public RelayCommand<Person> RemoveCommand { get; set; } public ViewModel() { Persons = new ObservableCollection<Person>(); AddCommand = new RelayCommand(AddPerson); RemoveCommand = new RelayCommand<Person>(RemovePerson); } private void AddPerson() { Persons.Add(new Person { Name = Name, Age = Age }); Name = ""; Age = 0; } private void RemovePerson(Person person) { Persons.Remove(person); } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } public class RelayCommand : ICommand { private readonly Action _execute; private readonly Func<bool> _canExecute; public RelayCommand(Action execute, Func<bool> canExecute = null) { _execute = execute; _canExecute = canExecute; } public bool CanExecute(object parameter) { return _canExecute == null || _canExecute(); } public void Execute(object parameter) { _execute(); } public event EventHandler CanExecuteChanged; public void RaiseCanExecuteChanged() { CanExecuteChanged?.Invoke(this, EventArgs.Empty); } } public class RelayCommand<T> : ICommand { private readonly Action<T> _execute; private readonly Func<T, bool> _canExecute; public RelayCommand(Action<T> execute, Func<T, bool> canExecute = null) { _execute = execute; _canExecute = canExecute; } public bool CanExecute(object parameter) { return _canExecute == null || _canExecute((T)parameter); } public void Execute(object parameter) { _execute((T)parameter); } public event EventHandler CanExecuteChanged; public void RaiseCanExecuteChanged() { CanExecuteChanged?.Invoke(this, EventArgs.Empty); } } ``` 注意,在这个视图模型类,我们实现了INotifyPropertyChanged接口,以便在属性值发生变化时通知视图进行更新。此外,我们还定义了两个命令AddCommand和RemoveCommand,分别用于添加和删除人员信息,这里使用了一个简单的ICommand实现RelayCommand。 最后,我们在MainWindow.xaml定义视图,并将其绑定到视图模型属性和命令: ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp1" Title="MainWindow" Height="350" Width="525"> <Window.DataContext> <local:ViewModel/> </Window.DataContext> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition/> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10"> <Label Content="Name:" VerticalAlignment="Center"/> <TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Width="100" Margin="5"/> <Label Content="Age:" VerticalAlignment="Center" Margin="10,0,0,0"/> <TextBox Text="{Binding Age, UpdateSourceTrigger=PropertyChanged}" Width="50" Margin="5"/> <Button Content="Add" Command="{Binding AddCommand}" Margin="10,0"/> </StackPanel> <ListBox Grid.Row="1" ItemsSource="{Binding Persons}" Margin="10" SelectedItem="{Binding SelectedPerson}" SelectionMode="Single"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Name}" Margin="5"/> <TextBlock Text="{Binding Age}" Margin="5"/> <Button Content="Remove" Command="{Binding DataContext.RemoveCommand, RelativeSource={RelativeSource AncestorType=ListBox}}" CommandParameter="{Binding}" Margin="10,0"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Window> ``` 在这个视图,我们使用了DataTemplate来定义ListBox的项模板,其包含一个文本块和一个删除按钮。注意,在删除按钮的命令绑定,我们使用了RelativeSource来指定数据上下文为ListBox的视图模型,并将选的人员信息作为参数传递给RemoveCommand。 到此为止,一个简单的MVVM示例就完成了。通过这个示例,我们可以看到MVVM模式在WPF的应用,它能够有效地将业务逻辑和用户界面进行分离,使得应用程序更加清晰、易于维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值