|
MVVM - DataGrid - динамическое кол-во столбцов
|
|||
---|---|---|---|
#18+
MainWindow.xaml: <Window x:Class="DataGridDisplayNameTest.MainWindow" xmlns=" http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x=" http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:dg="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"> <Grid> <dg:DataGrid x:Name="theGrid" AutoGenerateColumns="True" AutoGeneratingColumn="dg_AutoGeneratingColumn"> </dg:DataGrid> </Grid> </Window> MainWindow.cs: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.ComponentModel; using System.Collections.ObjectModel; using System.Reflection; namespace DataGridDisplayNameTest { /// <summary> /// This test app demonstrates how to set the Column Headers in the WPF DataGrid /// to the display name attribute of the bound property. /// </summary> public partial class MainWindow : Window { public MainWindow() { this.Initialized += new EventHandler(MainWindow_Initialized); InitializeComponent(); } /// <summary> /// Add data to controls after they have been initialized /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void MainWindow_Initialized(object sender, EventArgs e) { this.theGrid.ItemsSource = new ObservableCollection<TestItem>{ new TestItem { Value1 = 11, Value2 = 12, Value3 = 13 }, new TestItem { Value1 = 21, Value2 = 22, Value3 = 23 }, new TestItem { Value1 = 31, Value2 = 32, Value3 = 33 } }; } /// <summary> /// Event handler for when columns are added to the data grid /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void dg_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { string displayName = GetPropertyDisplayName(e.PropertyDescriptor); if (!string.IsNullOrEmpty(displayName)) { e.Column.Header = displayName; } } /// <summary> /// Gets the Display Name for the property descriptor passed in /// </summary> /// <param name="descriptor"></param> /// <returns></returns> public static string GetPropertyDisplayName(object descriptor) { PropertyDescriptor pd = descriptor as PropertyDescriptor; if (pd != null) { // Check for DisplayName attribute and set the column header accordingly DisplayNameAttribute displayName = pd.Attributes[typeof(DisplayNameAttribute)] as DisplayNameAttribute; if (displayName != null && displayName != DisplayNameAttribute.Default) { return displayName.DisplayName; } } else { PropertyInfo pi = descriptor as PropertyInfo; if (pi != null) { // Check for DisplayName attribute and set the column header accordingly Object[] attributes = pi.GetCustomAttributes(typeof(DisplayNameAttribute), true); for (int i = 0; i < attributes.Length; ++i) { DisplayNameAttribute displayName = attributes[i] as DisplayNameAttribute; if (displayName != null && displayName != DisplayNameAttribute.Default) { return displayName.DisplayName; } } } } return null; } } public class TestItem { [DisplayName("Display Name 1")] public int Value1 { get; set; } [DisplayName("Display Name 2")] public int Value2 { get; set; } [DisplayName("Display Name 3")] public int Value3 { get; set; } } } Модератор: Уберите ссылки из автоподписи! ... |
|||
:
Нравится:
Не нравится:
|
|||
06.02.2012, 13:05 |
|
|
start [/forum/topic.php?fid=21&gotonew=1&tid=1441951]: |
0ms |
get settings: |
10ms |
get forum list: |
14ms |
check forum access: |
4ms |
check topic access: |
4ms |
track hit: |
36ms |
get topic data: |
9ms |
get first new msg: |
9ms |
get forum data: |
2ms |
get page messages: |
38ms |
get tp. blocked users: |
1ms |
others: | 13ms |
total: | 140ms |
0 / 0 |