使用价值转换器
public partial class MainWindow : Window
{
public ObservableCollection<Employee> EmpList { get; set; }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
EmpList = new ObservableCollection<Employee> {
new Employee(1, "a"),
new Employee(2, "b"),
new Employee(3, "c") };
}
}
public class NumValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int v = int.Parse(value.ToString());
if (v < 3)
return "YES";
else
return "NO";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class Employee
{
public int Salary { get; set; }
public string Name { get; set; }
public Employee(int s, string n)
{
Salary = s;
Name = n;
}
}
XAML
<Window x:Class="garbagestack.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cv="clr-namespace:garbagestack"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<cv:NumValueConverter x:Key="cv1"/>
</Grid.Resources>
<DataGrid AutoGenerateColumns="False" Margin="12,99,12,12" Name="dg1" ItemsSource="{Binding EmpList}" >
<DataGrid.Columns>
<DataGridTextColumn Header="NewValue" Binding="{Binding Salary}"/>
<DataGridTextColumn Header="NewValue" Binding="{Binding Name}"/>
<DataGridTextColumn Header="NewValue" Binding="{Binding Salary,Converter={StaticResource cv1}}">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>