Home > other >  Consult the WPF IValueConverter usage
Consult the WPF IValueConverter usage

Time:10-02

Consulting understand WPF under this piece of technology,

I have this code, here are a few of parameters exactly how to do?
Type typeTarget, object param, CultureInfo culture?
Public object Convert (object value, Type typeTarget, object param, CultureInfo culture)
{
Int iLen=0;
String sCells=(string) value;
ILen=sCells. Length;
If (iLen & gt; 5)
{
Return new SolidColorBrush (Color. FromRgb (200, 150, 100));
}
The else
{
Return new SolidColorBrush (Color. FromRgb (0, 0, 0));
}
}
Public object ConvertBack (object value, Type typeTarget, object param, CultureInfo culture)
{
return null;
}

Now I do is a function as shown in figure shows the DataGrid


I at the front desk XAML writing is as follows:
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: local="CLR namespace: WPF_DataGrid_Demo"
XMLNS: CVT="CLR namespace: WPF_DataGrid_Demo"
XMLNS: setFonts="CLR namespace: WPF_DataGrid_Demo"
Title="MainWindow" Height="1024" Width="1286" WindowStartupLocation="CenterScreen SizeToContent=" "Width" the Loaded="fmMain_Loaded & gt;"
& lt; Window. Resources>



& lt; DataGridTemplateColumn>


FontSize="{Binding s01, the Converter={StaticResource setupFont}}"/& gt;




















Front desk in the code mark red is to give the DataGrid column 1 cells that meet the conditions set the font size is 32 and foreground color is brown,

The background code is as follows:
Using System;
using System.Collections.Generic;
Using System. Linq;
Using System. The Text;
Using System. The Threading. The Tasks;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. The Data;
Using System. Windows. The Documents;
Using System. Windows. Input;
Using System. Windows. Media;
Using System. Windows. Media. Imaging;
Using System. Windows. Navigation;
Using System. Windows. Shapes;
Using System. Collections. ObjectModel;
Using System. Diagnostics;
Using System. Windows. Media. TextFormatting;
Using System. Globalization;//1. Refer to the unit

The namespace WPF_DataGrid_Demo
{
///& lt; Summary>
///MainWindow. Xaml interaction logic
///& lt;/summary>
///
Public class DataContent
{
Public string s01 {get; The set; }
Public string s02 {get; The set; }
Public SolidColorBrush ForeCol01CellsBrush {get; The set; }//column 1 foreground background colors and font size
Public SolidColorBrush BackCol01CellsBrush {get; The set; }
Public int iCol01FontSize {get; The set; }
}

Public class ConvertColor: IValueConverter//2. To define a class interface IValueConverter covering the Convert method such as
{
Public object Convert (object value, Type typeTarget, object param, CultureInfo culture)
{
Int iLen=0;
String sCells=(string) value;
ILen=sCells. Length;
If (iLen & gt; 5)
{
Return new SolidColorBrush (Color. FromRgb (200, 150, 100));
}
The else
{
Return new SolidColorBrush (Color. FromRgb (0, 0, 0));
}
}
Public object ConvertBack (object value, Type typeTarget, object param, CultureInfo culture)
{
return null;
}
}

Public class SetUpFont: IValueConverter//2. To define a class interface IValueConverter covering the Convert method such as
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related