WPF入门8:模板(Template)

WPF入门8:什么是模板(Template)

什么是模板(Template)?

模板应用在View层,它的主要作用是修改控件的样式、交互、数据展示。
模板主要分为:DataTemplate 和 ControlTemplate。

  • ControlTemplate它决定了控件“长成什么样子”,并让开发者有机会在控件原有的内部逻辑基础上扩展自己的逻辑。
  • DataTemplate是数据内容的展示方式,一条数据显示成什么样子,是简单的文本还是直观的图形就由它来决定了。
    在这里插入图片描述
  <Window.Resources>
        <DataTemplate x:Key="songSheetDataTemplate">
            <StackPanel Orientation="Horizontal">
                <Image
                    Width="30"
                    Height="30"
                    Source="{Binding Path=Icon}" />
                <TextBlock
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    FontSize="14"
                    FontWeight="Bold"
                    Text="{Binding Path=Name}" />
            </StackPanel>
        </DataTemplate>
        <ControlTemplate x:Key="ButtonTemplate" TargetType="Button">
            <!--  定义视觉树  -->
            <Grid>
                <Ellipse
                    Name="faceEllipse"
                    Width="{TemplateBinding Button.Width}"
                    Height="{TemplateBinding Control.Height}"
                    Fill="{TemplateBinding Button.Background}" />
                <TextBlock
                    Name="txtBlock"
                    Margin="{TemplateBinding Button.Padding}"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    Text="{TemplateBinding Button.Content}" />
            </Grid>
            <!--  定义视觉树_end  -->
            <!--  定义触发器  -->
            <ControlTemplate.Triggers>
                <Trigger Property="Button.IsMouseOver" Value="True">
                    <Setter Property="Button.Foreground" Value="Red" />
                </Trigger>
            </ControlTemplate.Triggers>
            <!--  定义触发器_End  -->
        </ControlTemplate>
    </Window.Resources>
 <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="2*" />
            <RowDefinition Height="6*" />
            <RowDefinition Height="2*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*" />
            <ColumnDefinition Width="8*" />
        </Grid.ColumnDefinitions>
        <ListBox
            x:Name="LstSongSheets"
            Grid.Row="1"
            ItemTemplate="{StaticResource songSheetDataTemplate}"
            ItemsSource="{Binding Path=SongSheets}" />
        <DataGrid
            Grid.Row="1"
            Grid.Column="1"
            AutoGenerateColumns="False"
            ItemsSource="{Binding ElementName=LstSongSheets, Path=SelectedItem.Songs}"
            RowHeaderWidth="0">
            <DataGrid.Columns>
                <DataGridTextColumn
                    Width="150"
                    Binding="{Binding Path=Name}"
                    Header="歌名" />
                <DataGridTextColumn
                    Width="150"
                    Binding="{Binding Path=Singer}"
                    Header="歌手" />
                <DataGridTextColumn
                    Width="150"
                    Binding="{Binding Path=AlbumTitle}"
                    Header="专辑" />
                <DataGridTextColumn
                    Width="150"
                    Binding="{Binding Path=Lenght}"
                    Header="时长" />
            </DataGrid.Columns>
        </DataGrid>
        <StackPanel
            Grid.Row="2"
            Grid.Column="1"
            Width="195"
            Height="40"
            Orientation="Horizontal">
            <Button
                Width="55"
                Height="25"
                Margin="5"
                Content="上一曲"
                Template="{StaticResource ButtonTemplate}" />
            <Button
                Grid.Row="2"
                Width="55"
                Height="25"
                Margin="5"
                Content="播放"
                Template="{StaticResource ButtonTemplate}" />
            <Button
                Grid.Row="2"
                Width="55"
                Height="25"
                Margin="5"
                Content="下一曲"
                Template="{StaticResource ButtonTemplate}" />
        </StackPanel>
    </Grid>
amespace Juster.Music.Models
{
    /// <summary>
    /// 歌单
    /// </summary>
    public class SongSheetModel
    {
        /// <summary>
        /// 歌单名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 歌单图标
        /// </summary>
        public string Icon { get; set; }

        /// <summary>
        /// 歌单里所包含的歌曲
        /// </summary>
        public List<SongModel> Songs { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }
}

 /// <summary>
    /// 歌曲模型
    /// </summary>
    public class SongModel
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 歌曲存放地址
        /// </summary>
        public string Url { get; set; }

        /// <summary>
        /// 歌手
        /// </summary>
        public string Singer { get; set; }

        /// <summary>
        /// 歌曲时长(s)
        /// </summary>
        public int Lenght { get; set; }

        /// <summary>
        /// 专辑名称
        /// </summary>
        public string AlbumTitle { get; set; }
    }
public class DataService
    {
        public static List<SongSheetDTO> GetSongSheet() 
        {
            List<SongSheetDTO> songSheets = new List<SongSheetDTO>();

            var songSheetJay = new SongSheetDTO();
            songSheetJay.Name = "周杰伦的歌单";
            songSheetJay.Icon = "/Juster.Common;component/imgs/music.png";
            songSheetJay.Songs = new List<SongDTO>() 
            {
                new SongDTO{ Name = "七里香" , AlbumTitle = "叶惠美" , Lenght = 800 , Singer = "周杰伦" , Url = "七里香.mp3" },
                new SongDTO{ Name = "外婆" , AlbumTitle = "叶惠美" , Lenght = 800 , Singer = "周杰伦" , Url = "外婆.mp3" },
                new SongDTO{ Name = "将军" , AlbumTitle = "叶惠美" , Lenght = 800 , Singer = "周杰伦" , Url = "将军.mp3" },
                new SongDTO{ Name = "搁浅" , AlbumTitle = "叶惠美" , Lenght = 800 , Singer = "周杰伦" , Url = "搁浅.mp3" }
            };
            songSheets.Add(songSheetJay);

            var songSheetJolin = new SongSheetDTO();
            songSheetJolin.Name = "蔡依林的歌单";
            songSheetJolin.Icon = "/Juster.Common;component/imgs/music.png";
            songSheetJolin.Songs = new List<SongDTO>()
            {
                new SongDTO{ Name = "倒带" , AlbumTitle = "城堡" , Lenght = 800 , Singer = "蔡依林" , Url = "倒带.mp3" },
                new SongDTO{ Name = "爱情36计" , AlbumTitle = "城堡" , Lenght = 800 , Singer = "蔡依林" , Url = "爱情36计.mp3" },
                new SongDTO{ Name = "海盗" , AlbumTitle = "城堡" , Lenght = 800 , Singer = "蔡依林" , Url = "海盗.mp3" },
                new SongDTO{ Name = "柠檬草的味道" , AlbumTitle = "城堡" , Lenght = 800 , Singer = "蔡依林" , Url = "柠檬草的味道.mp3" }
            };
            songSheets.Add(songSheetJolin);

            return songSheets;
        }
    }

    /// <summary>
    /// 歌曲模型
    /// </summary>
    public class SongDTO
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 歌曲存放地址
        /// </summary>
        public string Url { get; set; }

        /// <summary>
        /// 歌手
        /// </summary>
        public string Singer { get; set; }

        /// <summary>
        /// 歌曲时长(s)
        /// </summary>
        public int Lenght { get; set; }

        /// <summary>
        /// 专辑名称
        /// </summary>
        public string AlbumTitle { get; set; }
    }

    /// <summary>
    /// 歌单
    /// </summary>
    public class SongSheetDTO
    {
        /// <summary>
        /// 歌单名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 歌单图标
        /// </summary>
        public string Icon { get; set; }

        /// <summary>
        /// 歌单里所包含的歌曲
        /// </summary>
        public List<SongDTO> Songs { get; set; }
    }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是刘彦宏吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值