RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1283216
Accepted
Vladimir
Vladimir
Asked:2022-05-18 17:11:44 +0000 UTC2022-05-18 17:11:44 +0000 UTC 2022-05-18 17:11:44 +0000 UTC

如何为 DataGrid MVVM 的行着色

  • 772

您需要根据IsValidName(). 对于绘画,一个转换器是由 RowToBrushConverter继承自 base one 的转换器 的名称创建的ConverterBase。在属性 DataGrid.RowStyle中,我设置了DataGridRow更改线条颜色的样式。

问题是它仅在滚动DataGrid时部分绘制。

它应该如何工作:如果条件为真IsValidName(),则涂成白色,否则,则涂成红色。

数据网格.RowStyle

           <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <StackPanel>
                                <TextBlock Text="{Binding ToolTip}" />
                            </StackPanel>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Background" Value="{Binding Converter={StaticResource RowToBrush}}"/>
                </Style>
            </DataGrid.RowStyle>

转换器基础

    abstract class ConverterBase : MarkupExtension, IValueConverter
    {
        public abstract object Convert(object value, Type targetType, object parameter, CultureInfo culture);
        public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
        public override object ProvideValue(IServiceProvider serviceProvider) => this;
    }

RowToBrush 转换器

    class RowToBrushConverter : ConverterBase
    {
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(Brush))
            {
                return null;
            }
            var FileName = (FileName)value;
            return !FileName.IsValidName() ? Brushes.Red : Brushes.White;
        }
    }

文件名

public class FileName:ModelBase
    {
        private System.IO.FileInfo Info;
        private string Directory;
        

        private string original;
        public string Original
        {
            get { return original; }
            set 
            { 
                original = value;
                OriginalImage = GetBitmapSource(GetIcon());
                OnPropertyChanged("Original");
            }
        }
        private BitmapSource originalImage;
        public BitmapSource OriginalImage
        {
            get { return originalImage; }
            set
            {
                originalImage = value;
                OnPropertyChanged("OriginalImage");
            }
        }


        private string modified;
        public string Modified
        {
            get { return modified; }
            set
            {
                modified = value;
                ModifiedImage = GetBitmapSource(GetIconModified());
                OnPropertyChanged("Modified");
                
                
            }
        }

        public BitmapSource modifiedImage;
        public BitmapSource ModifiedImage
        {
            get { return modifiedImage; }
            set
            {
                modifiedImage = value;
                OnPropertyChanged("ModifiedImage");
            }
        }

        public string toolTip { get; set; }
        public string ToolTip
        {
            get { return toolTip; }
            set
            {
                toolTip = value;
                OnPropertyChanged("ToolTip");
            }
        }


       



        public BitmapSource GetBitmapSource(Bitmap bitmap)
        {
            BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap
            (
                bitmap.GetHbitmap(),
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions()
            );

            return bitmapSource;
        }



        //Название каталога не должно заканчиваться\
        public FileName(string directory, string original, string modified)
        {
            Directory = directory.TrimEnd('\\');

            Original = original;
            Modified = modified;
            ToolTip= ParentDirectory() + @"\"+ Original;
           
        }
        

        public FileName(FileName name, bool swap = false)
        {
            Directory = name.Directory.TrimEnd('\\');

            Original = name.Original;
            Modified = name.Modified;

            if (swap) Swap();
        }

        public void Reset()
        {
            Modified = Original;
        }

        public bool IsValidName()
        {
            if (string.IsNullOrEmpty(Modified))
            {
                return false;
            }

            bool result = Modified.IndexOfAny(Path.GetInvalidFileNameChars()) == -1;
            return result;
        }

        public void Swap()
        {
            var temp = Original;
            Original = Modified;
            Modified = temp;
        }

        public string GetExtension()
        {
            return Path.GetExtension(Original);
        }

        public string GetModifiedNameWithoutExtension()
        {
            return Path.GetFileNameWithoutExtension(Modified);
        }

        public string ParentDirectory()
        {
            var parts = Directory.Split('\\');
            return parts[parts.Length - 1];
        }

        public string FullPath()
        {
            return Directory + "\\" + Original;
        }

        public string FullPathModified(string newDirectory = null)
        {
            if (newDirectory != null) return newDirectory + "\\" + Modified;
            return Directory + "\\" + Modified;
        }

       

        public Bitmap GetIcon()
        {
            return Common.ShellIcon.GetBitmapIcon(FullPath());
        }

        public Bitmap GetIconModified()
        {
            return Common.ShellIcon.GetBitmapIcon(FullPathModified());
        }

        private void LoadFileInfo()
        {
            if (Info == null)
                Info = new System.IO.FileInfo(this.FullPath());
        }

        public string CreationDate()
        {
            LoadFileInfo();

            return Info.CreationTime.ToShortDateString() + " " + Info.CreationTime.ToShortTimeString();
        }

        public string LastWriteDate()
        {
            LoadFileInfo();

            return Info.LastWriteTime.ToShortDateString() + " " + Info.LastWriteTime.ToShortTimeString();
        }

        //trim file length to fit a unit
        private double TrimSize(double length, int n = 0)
        {
            for (int i = 0; i < n; i++) length /= 1024.0;         //divide n times by 1024            
            return Math.Round(Convert.ToDouble(length), 2);     //convert to double and round to 2 decimal places
        }

        public string ReadableFileSize()
        {
            LoadFileInfo();

            long length = Info.Length;

            if (length < Math.Pow(1024, 1)) return length + " B";               //lower than 1kb
            if (length < Math.Pow(1024, 2)) return TrimSize(length, 1) + " KB"; //lower than 1mb
            if (length < Math.Pow(1024, 3)) return TrimSize(length, 2) + " MB"; //lower than 1gb
            if (length < Math.Pow(1024, 4)) return TrimSize(length, 3) + " GB"; //lower than 1tb

            return length + " TB";                                              //return size in tb    
        }
    }

它是如何工作的

当我创建文件名“”时,仍然有些行没有着色,如果我不滚动着色根本不会发生。 在此处输入图像描述

它应该如何工作

如果文件名不是“” 在此处输入图像描述

如果文件名是“” 在此处输入图像描述

wpf
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Vladimir
    2022-05-18T23:51:28Z2022-05-18T23:51:28Z

    特别感谢 EvgeniyZ的指导。

    数据网格.RowStyle

       <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Setter Property="ToolTip">
                    <Setter.Value>
                        <StackPanel>
                            <TextBlock Text="{Binding ToolTip}" />
                        </StackPanel>
                    </Setter.Value>
                </Setter>
                <Setter Property="Background" Value="White"/>
                <Style.Triggers>
                    <DataTrigger Binding = "{Binding IsValidNameFile}" Value = "true">
                        <Setter Property="Background" Value="Red"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
    

    文件名

    public class FileName:ModelBase
        {
            private System.IO.FileInfo Info;
            private string Directory;
            
    
            private string original;
            public string Original
            {
                get { return original; }
                set 
                { 
                    original = value;
                    OriginalImage = GetBitmapSource(GetIcon());
                    OnPropertyChanged("Original");
                }
            }
            private BitmapSource originalImage;
            public BitmapSource OriginalImage
            {
                get { return originalImage; }
                set
                {
                    originalImage = value;
                    OnPropertyChanged("OriginalImage");
                }
            }
    
    
            private string modified;
            public string Modified
            {
                get { return modified; }
                set
                {
                    modified = value;
                    ModifiedImage = GetBitmapSource(GetIconModified());
                    OnPropertyChanged("Modified");
                    IsValidNameFile = !IsValidName();
                }
            }
    
            private BitmapSource modifiedImage;
            public BitmapSource ModifiedImage
            {
                get { return modifiedImage; }
                set
                {
                    modifiedImage = value;
    
    
                    OnPropertyChanged("ModifiedImage");
                }
            }
    
            private string toolTip { get; set; }
            public string ToolTip
            {
                get { return toolTip; }
                set
                {
                    toolTip = value;
                    OnPropertyChanged("ToolTip");
                }
            }
    
    
            private bool isValidNamefile;
            public bool IsValidNameFile
            {
                get
                {
                    return isValidNamefile;
                }
                set
                {
                    isValidNamefile = value;
                    OnPropertyChanged("IsValidNameFile");
                }
            }
    
    
    
    
    
            public BitmapSource GetBitmapSource(Bitmap bitmap)
            {
                BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap
                (
                    bitmap.GetHbitmap(),
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions()
                );
    
                return bitmapSource;
            }
    
    
    
            //Название каталога не должно заканчиваться\
            public FileName(string directory, string original, string modified)
            {
                Directory = directory.TrimEnd('\\');
    
                Original = original;
                Modified = modified;
                ToolTip= ParentDirectory() + @"\"+ Original;
               
            }
            
    
            public FileName(FileName name, bool swap = false)
            {
                Directory = name.Directory.TrimEnd('\\');
    
                Original = name.Original;
                Modified = name.Modified;
    
                if (swap) Swap();
            }
    
            public void Reset()
            {
                Modified = Original;
            }
    
            public bool IsValidName()
            {
                if (string.IsNullOrEmpty(Modified)|| string.IsNullOrWhiteSpace(Modified)) return false;
                return Modified.IndexOfAny(Path.GetInvalidFileNameChars()) == -1;
            }
    
            public void Swap()
            {
                var temp = Original;
                Original = Modified;
                Modified = temp;
            }
    
            public string GetExtension()
            {
                return Path.GetExtension(Original);
            }
    
            public string GetModifiedNameWithoutExtension()
            {
                return Path.GetFileNameWithoutExtension(Modified);
            }
    
            public string ParentDirectory()
            {
                var parts = Directory.Split('\\');
                return parts[parts.Length - 1];
            }
    
            public string FullPath()
            {
                return Directory + "\\" + Original;
            }
    
            public string FullPathModified(string newDirectory = null)
            {
                if (newDirectory != null) return newDirectory + "\\" + Modified;
                return Directory + "\\" + Modified;
            }
    
           
    
            public Bitmap GetIcon()
            {
                return Common.ShellIcon.GetBitmapIcon(FullPath());
            }
    
            public Bitmap GetIconModified()
            {
                return Common.ShellIcon.GetBitmapIcon(FullPathModified());
            }
    
            private void LoadFileInfo()
            {
                if (Info == null)
                    Info = new System.IO.FileInfo(this.FullPath());
            }
    
            public string CreationDate()
            {
                LoadFileInfo();
    
                return Info.CreationTime.ToShortDateString() + " " + Info.CreationTime.ToShortTimeString();
            }
    
            public string LastWriteDate()
            {
                LoadFileInfo();
    
                return Info.LastWriteTime.ToShortDateString() + " " + Info.LastWriteTime.ToShortTimeString();
            }
    
            //trim file length to fit a unit
            private double TrimSize(double length, int n = 0)
            {
                for (int i = 0; i < n; i++) length /= 1024.0;         //divide n times by 1024            
                return Math.Round(Convert.ToDouble(length), 2);     //convert to double and round to 2 decimal places
            }
    
            public string ReadableFileSize()
            {
                LoadFileInfo();
    
                long length = Info.Length;
    
                if (length < Math.Pow(1024, 1)) return length + " B";               //lower than 1kb
                if (length < Math.Pow(1024, 2)) return TrimSize(length, 1) + " KB"; //lower than 1mb
                if (length < Math.Pow(1024, 3)) return TrimSize(length, 2) + " MB"; //lower than 1gb
                if (length < Math.Pow(1024, 4)) return TrimSize(length, 3) + " GB"; //lower than 1tb
    
                return length + " TB";                                              //return size in tb    
            }
        }
    
    • 0

相关问题

  • WPF 数据网格,DataGridCheckBoxColumn 单元格依赖于 DataGridComboBoxColumn

  • 基于数据的 WPF ItemsControl 模板

  • 转换器的问题

  • 在哪里可以查看可在 XAML 中使用的所有可用 WIRED 命名空间?

  • MVVM 数据收集的问题

  • 按钮样式。WPF

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5