美文网首页
WPF DataGird 使用进阶(4)

WPF DataGird 使用进阶(4)

作者: Ritchie_Li | 来源:发表于2022-05-26 21:04 被阅读0次

效果如下图:

创建Widnows资源:

<Style x:Key="AltColBackground" TargetType="DataGridCell">

            <Setter Property="Background" Value="LightGreen"/>

            <Setter Property="BorderBrush" Value="LightSkyBlue"/>

   </Style>

日期字段增加修饰器:

<DataGridTemplateColumn>

                    <DataGridTemplateColumn.CellTemplate>

                        <DataTemplate>

                            <TextBlock Text="{Binding ReleaseDateTime,StringFormat=yyyy}" TextDecorations="Underline"/>

                        </DataTemplate>

                    </DataGridTemplateColumn.CellTemplate>

                    <DataGridTemplateColumn.CellEditingTemplate>

                        <DataTemplate>

                            <DatePicker SelectedDate="{Binding ReleaseDateTime}"/>

                        </DataTemplate>

                    </DataGridTemplateColumn.CellEditingTemplate>

                </DataGridTemplateColumn>

URL 字段添加单元格风格:

<DataGridHyperlinkColumn Header="URL" Binding="{Binding URL}"

                                        CellStyle="{StaticResource AltColBackground}"

 ></DataGridHyperlinkColumn>

通过操作界面,添加行,将行数据写入到文本文件中

添加一行测试数据

添加编辑行事件

RowEditEnding="GRD_RowEditEnding"

每个行字段添加属性触发 

UpdateSourceTrigger=PropertyChanged

后台添加

void AddNewSongs(Song s)

{

     var file = System.IO.Path.Combine($"{AppDomain.CurrentDomain.BaseDirectory}", "songs.txt");

     File.AppendAllText(file, $"{s.Id},{s.Title},{s.Genre},{s.Artist},{s.MovieTitle},{s.ReleaseDateTime},{s.URL}\n");

 }

private void GRD_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)

 {

            Song newSong = e.Row.DataContext as Song;

            AddNewSongs(newSong);

 }

这样在UI添加行后,就自动将字段值写入到了后台文件中。

相关文章

  • WPF DataGird 使用进阶(4)

    效果如下图: 创建Widnows资源: 日期字段增加修饰器: ...

  • WPF DataGird 使用进阶(2)

    实现效果一: DataGrid索引列显示小圆点,且文本可以编辑。 修改ID字段的数据绑定,创建数据模板: ...

  • WPF DataGird 使用进阶(3)

    使用数据模板,效果如下图所示: XMAL添加代码如下: ...

  • WPF DataGrid 使用进阶

    数据模型和数据模型创建的数据集代码和上一篇代码一样。 只是UI创建的绑定方式不一样,不再默认绑定方式。 效果如下:...

  • Log4j2进阶使用(更多高级特性)

    1.高级进阶说明 本文介绍Log4j2高级进阶使用,基于Log4j2进阶使用(按大小时间备份日志),介绍更多的高级...

  • Log4j2进阶使用(按大小时间备份日志)

    1.进阶说明 本文介绍Log4j2进阶使用,基本使用请参考Log4j2基本使用入门。本文基于上面的基本使用入门,主...

  • WPF承载winform控件

    主要是最近的WPF项目中,用到了别的公司的东西,需要调用dll来使用该公司的控件。 在WPF中使用WinForm主...

  • WPF简介

    目录 什么是WPF? WPF的历史? 为什么要用WPF及WPF作用 WPF与winForm区别? 什么是WPF? ...

  • 【WPF】wpf怎么使用WindowsFormsHost

    使用方法: 1、首先,我们需要向项目中的引用(reference)中添加两个动态库dll,一个是.NET库中的Sy...

  • WPF+调用控制台输出信息

    WPF + 控制台输出信息 需求: 有时候我们需要在WPF项目调试的时候使用 console输出log ,输出错...

网友评论

      本文标题:WPF DataGird 使用进阶(4)

      本文链接:https://www.haomeiwen.com/subject/lzrpprtx.html