none
在DataGrid中能否控制数据绑定的显示方式 RRS feed

  • 问题

  • 比如我有一个DateTime类型的字段 绑定到DataGrid的一列上 如果自己控制绑定以后的显示方式

    Code Snippet

    <my:DataGridCheckBoxColumn Header="男" DisplayMemberBinding="{Binding BirthDay}"/>

     

     

    有什么办法能让绑定显示的是BirthDay.ToShortDateString()呢?
    2008年5月18日 9:16

答案

  • 用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 Snippet

     

    using 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 User

        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
    }

     

    <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去把你的日期转换过来!
    加油!呵呵!
    现在我在上班,你自己先想想,中午休息的时候我再给你写一个.

    2008年5月19日 1:18
    版主

全部回复

  • 用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 Snippet

     

    using 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 User

        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
    }

     

    <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去把你的日期转换过来!
    加油!呵呵!
    现在我在上班,你自己先想想,中午休息的时候我再给你写一个.

    2008年5月19日 1:18
    版主
  • 这办法不错额 我试了下可以的 看来还是得多看看silverlight的帮助文档 虽然英文让人头疼 呵呵

    顺便问一下 如果这个实现了IValueConverter的类改成定义在User类中 例如

    Code Snippet

    public class User

    {

    public class UserConverter:IValueConverter

    {

     

    }

    }

     

     

     

    这样的话在static resouce中能否引用?

    我写了

    <UserControl xmlns:my="clr-namespaceTongue Tiedystem.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>
                <TestDataGridTongue Tiedtudent.DateConverter x:Key="DateConverter"/>
            </Grid.Resources>

    是不是写法不对?还是不支持内部类?
    2008年5月19日 5:10
  • 内部类,我没有试过,有空就去试试!

    你的内部类声明为静态的,你试试看行不行.呵呵!

    中午忙着给别人弄去了,没给你写,对不起啊!!

    2008年5月19日 5:51
    版主
  • 呵呵 你的例子已经给我很好的启发了 谢谢

    加了static不行 也许不支持内部类吧

    2008年5月19日 8:56