开发者

How to format numbers or text in Silverlight DataGrid columns?

Hi I got DataGr开发者_运维问答id with columns auto generated. Some Columns are strings some are int and some are double. How do I set display format on each column? (Silverlight 4) btw I read somwhere suggestions to catch AutoGenerating event and to put something like this

private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
      e.Column.Format="N0";
}

but e.Column.Format does not exists!


Refering to this site, I think this is what you are looking for, it should work with the Toolkit control instead of the telerik control. Cheers

http://www.telerik.com/community/forums/silverlight/gridview/how-to-format-autogenerated-columns.aspx

void gridView_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyType == typeof(DateTime))
    {
         DataGridBoundColumn obj = e.Column as DataGridBoundColumn;
         if (obj != null && obj.Binding != null)
              obj.Binding.StringFormat = "{0:d}";
    }
}


UPDATED:* using MVVM, bind your list, prevent autogeneratecolumns then customize celltemplate with a datatemplate (also will provide you with more flexibility with what and how to show stuff on a column) then format the textblock text.

public partial class MainPage : UserControl, INotifyPropertyChanged
    {
        private ObservableCollection<CustomClass> _myList;

        public event PropertyChangedEventHandler PropertyChanged;

        public ObservableCollection<CustomClass> MyList
        {
            get { return _myList ?? (_myList = new ObservableCollection<CustomClass>()); }
            set
            {
                _myList = value;
                RaisePropertyChanged("MyList");
            }
        }

        protected void RaisePropertyChanged(string propertyname)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }

        public MainPage()
        {
            InitializeComponent();
            this.DataContext = this;

            MyList.Add(new CustomClass() { PropertyToBeWatched = "12"});
            MyList.Add(new CustomClass() { PropertyToBeWatched = "23" });
            MyList.Add(new CustomClass() { PropertyToBeWatched = "24" });
            MyList.Add(new CustomClass() { PropertyToBeWatched = "25" });

        }

}

XAML:

<sdk:DataGrid ItemsSource="{Binding MyList}" RowStyle="{StaticResource Style1}" AutoGenerateColumns="False">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTemplateColumn>
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding SomeEntity, StringFormat=c}"  />
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>
                <sdk:DataGridTextColumn Binding="{Binding PropertyToBeWatched}" Header="Property1"/>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

Hope this helps.


I don't know if know MVVM pattern. Using an approach from it you can create additional class (ViewModel) for items that you're binding to the DataGrid.

For example let assume you have a class:

public class MyObject
{
  public int Value1 { get; set; }
  public double Value2 { get; set; }
  public string Value3 { get; set; }
}

so you can create a class MyObjectViewModel like this:

public class MyObjectViewModel 
{
  private MyObject _object;

  public MyObjectViewModel(MyObject obj)
  {
    _object = obj;
  }

  public string Value1 
  { 
    get
    {
      return _object.Value1.ToString() //format for int
    }
  }

  public string Value2
  { 
    get
    {
      return _object.Value2.ToString() //format for double 
    }
  }

  public string Value3
  { 
    get
    {
      return _object.Value3
    }
  }
}

and then before data binding you can transform collection of MyObject to the collection of MyObjectViewModel and bind this collection. This way you can format your properties whatever you like.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜