您需要根据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
}
}
它是如何工作的
当我创建文件名“”时,仍然有些行没有着色,如果我不滚动着色根本不会发生。
特别感谢 EvgeniyZ的指导。
数据网格.RowStyle
文件名