powered by simpleCommunicator - 2.0.51     © 2025 Programmizd 02
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / MVVM - DataGrid - динамическое кол-во столбцов
1 сообщений из 26, страница 2 из 2
MVVM - DataGrid - динамическое кол-во столбцов
    #37648510
Sergio Kornelius
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
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; }

}
}

Модератор: Уберите ссылки из автоподписи!
...
Рейтинг: 0 / 0
1 сообщений из 26, страница 2 из 2
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / MVVM - DataGrid - динамическое кол-во столбцов
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]