WPF通过DataTable绑定主从表及显示修改进度条值

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了WPF通过DataTable绑定主从表及显示修改进度条值脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

WPF在做这方面其实倒是比WINFORM要简单太多

以下为代码

界面代码

<Window x:Class="MainViews.MatsterGrid"
        XMlns="http://schemas.microsoft.COM/winfx/2006/xaml/PResentation"
        xMLns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/exPression/blend/2008"
        xmlns:mc="http://schemas.oPEnxmlformats.org/markup-compatibilITy/2006"
        xmlns:g="clr-namespace:GeneralTool.General.WPFHelper.extensions;asSEMbly=GeneralTool.General"
        mc:Ignorable="d"
        Title="MatsterGrid"
        Height="450"
        Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition />
        </Grid.RowDefinitions>

        <;menu>
            <MenuItem Header="Random"
                      Command="{Binding RandomCommand}"
                      CommandParameter="{Binding ElementName=Dg,Path=SelectedValue}"/>
        </Menu>
        <DataGrid x:Name="Dg"
                  g:NameDependency.Name="{g:Name ViewGrid}"
                  CanUserDeleteRows="False"
                  AutoGenerateColumns="False"
                  CanUserAddRows="False"
                  DataContext="{Binding}"
                  ItemsSource="{Binding Table}"
                  Grid.Row="1">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="{Binding Icon}"
                                    Command="{Binding ElementName=Dg,Path=DataContext.RowDetailCommand}" 
                                    CommandParameter="{Binding}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTextColumn Header="Value"
                                    Binding="{Binding Value}" 
                                    IsReadOnly="True"/>

                <DataGridTemplateColumn Header="Progress">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ProgressBar Value="{Binding Progress}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

            </DataGrid.Columns>

            <DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <DataGrid ItemsSource="{Binding SuBTable}"
                              AutoGenerateColumns="False"
                              CanUserAddRows="False">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="ID"
                                                Binding="{Binding ID}" 
                                                IsReadOnly="True"/>
                            <DataGridTextColumn Header="Description"
                                                Binding="{Binding Desc}"
                                                IsReadOnly="True"/>
                            <DataGridTemplateColumn Header="Progress">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <ProgressBar Value="{Binding Value}" />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>
                </DataTemplate>
            </DataGrid.RowDetailsTemplate>
        </DataGrid>
    </Grid>
</Window>

  这里用到了我自己的一个类库:GeneralTool.General,可以通过Nuget下载,

支持将界面上的控件绑定到ViewModel中,同时也支持将View层的界面事件也可以绑定过去,

当然,还有一些其它的特性,这里不多说

&nbsp;

ViewModel层代码

using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using GeneralTool.General.WPFHelper;

namespace MainViews
{
    public class MasterVIewModel : BaseNotifyModel
    {
        private DataTable table = new DataTable();
        /// <summary>
        /// 主表
        /// </summary>
        public DataTable Table
        {
            get => this.table;
            set => this.RegisterProperty(ref this.table, value);
        }

        /// <summary>
        /// 展开
        /// </summary>
        public ICommand RowDetailCommand { get; set; }

        /// <summary>
        /// 随机更新Progress
        /// </summary>
        public ICommand RandomCommand { get; set; }

        /// <summary>
        /// 绑定界面上的主DataGrid(当然你也可以通过View在绑定DataContext时传递进来)
        /// </summary>
        public DataGrid ViewGrid { get; set; }

        public MasterVIewModel()
        {
            this.RowDetailCommand = new SimpleCommand<DataRowView>(RowDetailsMethod);
            this.RandomCommand = new SimpleCommand<DataRowView>(RandomMethod);
            inittable();
        }

        /// <summary>
        /// 初始化主表
        /// </summary>
        private void InitTable()
        {
            this.Table.Columns.Add("Icon");
            this.Table.Columns.Add("ID");
            this.Table.Columns.Add("Value");
            this.Table.Columns.Add("Progress");
            this.Table.Columns.Add("SubTable", typeof(DataTable));

            this.Table.Rows.Add("+", 1, 50, 50, DBNull.Value);
            this.Table.Rows.Add("+", 2, 70, 70, DBNull.Value);
        }

        /// <summary>
        /// 随机更新子表的进度值
        /// </summary>
        /// <param name="arg"></param>
        private void RandomMethod(DataRowView arg)
        {
            if (arg == null) return;
            //随机更新当前行的Progress
            VAR dt = arg.Row["SubTable"] as DataTable;
            if (dt == null)
                return;

            //将当前行子表的进度条值随机更新一个数据
            dt.Rows[0]["Value"] = new Random().Next(0, 101);
        }

        /// <summary>
        /// 点击主表的+ - 号时的处理方法
        /// </summary>
        /// <param name="rowView"></param>
        private void RowDetailsMethod(DataRowView rowView)
        {
            var row = rowView.Row;
            var content = row["Icon"] + "";
            if (content == "+")
            {
                //显示出来
                row["SubTable"] = this.GetSubTable(row["ID"]);
                this.ViewGrid.SetDetailsVisibilityForItem(rowView, Visibility.Visible);
                row["Icon"] = "-";
            }
            else
            {
                //隐藏
                this.ViewGrid.SetDetailsVisibilityForItem(rowView, Visibility.Collapsed);
                row["Icon"] = "+";
                row["SubTable"] = null;
            }
        }

        /// <summary>
        /// 获取一个子Table
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private DataTable GetSubTable(object id)
        {
            var table = new DataTable();
            table.Columns.Add("ID");
            table.Columns.Add("Desc");
            table.Columns.Add("Value");
            table.Rows.Add(id, Guid.NewGuid(), 80);
            return table;
        }
    }
}

  效果图

WPF通过DataTable绑定主从表及显示修改进度条值

 

脚本宝典总结

以上是脚本宝典为你收集整理的WPF通过DataTable绑定主从表及显示修改进度条值全部内容,希望文章能够帮你解决WPF通过DataTable绑定主从表及显示修改进度条值所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。