积极答复者
在DataGrid中能否控制数据绑定的显示方式

问题
答案
-
用Convert,去转换:大概像这样
Code Snippet
public class UserConverter : IValueConverter
{#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string sex_str = (string)value;if (sex_str == null || "".Equals(sex_str.Trim()))
{
return "male";
}if ("男".Equals(sex_str.Trim()))
{
return sex_str = "male";
}
else
{
return sex_str = "famale";
}
}public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}#endregion
}// end for class ,Converter以下为全部源码
Code Snippetusing System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;namespace ConvertValue
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
List<User> userList = new List<User>();
userList.Add(new User("c1","21","男"));
userList.Add(new User("c2", "26", "女"));
userList.Add(new User("c3", "31", "男"));
userList.Add(new User("c4", "21", "女"));
userList.Add(new User("c5", "11", "男"));
userList.Add(new User("c6", "27", "男"));
this.lstUser.DataContext = userList;
}
}
public class User
{
#region
public User()
{}
public User(string userName, string age, string sex)
{
this.UserName = userName;
this.Age = age;
this.Sex = sex;
}
public string UserName { get; set; }
public string Age { get; set; }
public string Sex { get; set; }#endregion
}// end for class ,this is Userpublic class UserConverter : IValueConverter
{#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string sex_str = (string)value;if (sex_str == null || "".Equals(sex_str.Trim()))
{
return "male";
}if ("男".Equals(sex_str.Trim()))
{
return sex_str = "male";
}
else
{
return sex_str = "famale";
}
}public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}#endregion
}// end for class ,Converter
}<UserControl x:Class="ConvertValue.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300" xmlns:cx="clr-namespace:ConvertValue">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<cx:UserConverter x:Key="userConverter"/>
</Grid.Resources>
<ListBox x:Name="lstUser" ItemsSource="{Binding}" Height="150">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding UserName}"/>
<TextBlock Text="{Binding Age}"/>
</StackPanel>
<TextBlock Text="{Binding Sex,Converter={StaticResource userConverter}}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>
如果你需要把日期以一定日期格式来显示,那你就 用一个Converter去把你的日期转换过来!
加油!呵呵!
现在我在上班,你自己先想想,中午休息的时候我再给你写一个.
全部回复
-
用Convert,去转换:大概像这样
Code Snippet
public class UserConverter : IValueConverter
{#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string sex_str = (string)value;if (sex_str == null || "".Equals(sex_str.Trim()))
{
return "male";
}if ("男".Equals(sex_str.Trim()))
{
return sex_str = "male";
}
else
{
return sex_str = "famale";
}
}public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}#endregion
}// end for class ,Converter以下为全部源码
Code Snippetusing System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;namespace ConvertValue
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
List<User> userList = new List<User>();
userList.Add(new User("c1","21","男"));
userList.Add(new User("c2", "26", "女"));
userList.Add(new User("c3", "31", "男"));
userList.Add(new User("c4", "21", "女"));
userList.Add(new User("c5", "11", "男"));
userList.Add(new User("c6", "27", "男"));
this.lstUser.DataContext = userList;
}
}
public class User
{
#region
public User()
{}
public User(string userName, string age, string sex)
{
this.UserName = userName;
this.Age = age;
this.Sex = sex;
}
public string UserName { get; set; }
public string Age { get; set; }
public string Sex { get; set; }#endregion
}// end for class ,this is Userpublic class UserConverter : IValueConverter
{#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string sex_str = (string)value;if (sex_str == null || "".Equals(sex_str.Trim()))
{
return "male";
}if ("男".Equals(sex_str.Trim()))
{
return sex_str = "male";
}
else
{
return sex_str = "famale";
}
}public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}#endregion
}// end for class ,Converter
}<UserControl x:Class="ConvertValue.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300" xmlns:cx="clr-namespace:ConvertValue">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<cx:UserConverter x:Key="userConverter"/>
</Grid.Resources>
<ListBox x:Name="lstUser" ItemsSource="{Binding}" Height="150">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding UserName}"/>
<TextBlock Text="{Binding Age}"/>
</StackPanel>
<TextBlock Text="{Binding Sex,Converter={StaticResource userConverter}}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>
如果你需要把日期以一定日期格式来显示,那你就 用一个Converter去把你的日期转换过来!
加油!呵呵!
现在我在上班,你自己先想想,中午休息的时候我再给你写一个. -
这办法不错额 我试了下可以的 看来还是得多看看silverlight的帮助文档 虽然英文让人头疼 呵呵
顺便问一下 如果这个实现了IValueConverter的类改成定义在User类中 例如
Code Snippetpublic class User
{
public class UserConverter:IValueConverter
{
}
}
这样的话在static resouce中能否引用?
我写了
ystem.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="TestDataGrid.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:TestDataGrid="clr-namespace:TestDataGrid"
Width="600" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<TestDataGridtudent.DateConverter x:Key="DateConverter"/>
</Grid.Resources> 是不是写法不对?还是不支持内部类?