| 

.NET C# Java Javascript Exception

2
Hallo zusammen

Durch ein Projekt das mehrsprachig und nach MVVM-Richtlienen konzipiert ist, bin ich auf das Problem der DataGridHeaderBinding gestossen. Nach Regergen im Netz bin ich auf diesen vielverspechenden Artikel gestossen:

http://www.benbarefield.com/blog/2011/03/09/bindable-datagrid-columns/

Da der C# code herunter geladen werden muss, werde ich ihn der eingfachheit halber hier anzeigen.

public static class DataGridColumns
{
public static readonly DependencyProperty DisplayColumnsProperty = DependencyProperty.RegisterAttached("DisplayColumns", typeof(IList<DataGridColumn>),
typeof(DataGridColumns), new PropertyMetadata(null, DisplayColumnsPropertyChanged));

private static readonly DependencyProperty DataGridColumnSettingsProperty = DependencyProperty.RegisterAttached("DataGridColumnSettings", typeof(DataGridColumnSettings),
typeof(DataGridColumn));

public static readonly DependencyProperty ColumnSettingsTemplateProperty = DependencyProperty.RegisterAttached("ColumnSetupTemplate", typeof(DataTemplate),
typeof(DataGridColumns), new PropertyMetadata(null, ColumnSettingsTemplateChanged));

static DataGridColumns()
{
FrameworkElement.DataContextProperty.AddOwner(typeof(DataGridTextColumn));
}


private static void SetDataGridColumnSettings(DataGridColumn column, DataGridColumnSettings settings)
{
column.SetValue(DataGridColumnSettingsProperty, settings);
}

private static DataGridColumnSettings GetDataGridColumnSettings(DataGridColumn column)
{
return column.GetValue(DataGridColumnSettingsProperty) as DataGridColumnSettings;
}


public static void SetDisplayColumns(DataGrid dataGrid, IList<DataGridColumn> columns)
{
dataGrid.SetValue(DisplayColumnsProperty, columns);
}

public static IList<DataGridColumn> GetDisplayColumns(DataGrid dataGrid)
{
return dataGrid.GetValue(DisplayColumnsProperty) as IList<DataGridColumn>;
}
private static void DisplayColumnsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = d as DataGrid;
var columns = e.NewValue as IList<DataGridColumn>;
var template = GetColumnSettingsTemplate(target);

CreateColumns(target, columns, template);
}

public static void SetColumnSettingsTemplate(DataGrid dataGrid, DataTemplate columnSetupTemplate)
{
dataGrid.SetValue(ColumnSettingsTemplateProperty, columnSetupTemplate);
}

public static DataTemplate GetColumnSettingsTemplate(DataGrid dataGrid)
{
return dataGrid.GetValue(ColumnSettingsTemplateProperty) as DataTemplate;
}

private static void ColumnSettingsTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = d as DataGrid;
var columns = GetDisplayColumns(target);
var template = e.NewValue as DataTemplate;

CreateColumns(target, columns, template);
}

private static void CreateColumns(DataGrid dataGrid, IList<DataGridColumn> columnViewModels, DataTemplate columnSettings)
{
if (dataGrid == null)
return;
dataGrid.Columns.Clear();

if (columnViewModels == null)
return;

foreach (var column in columnViewModels)
{
var newColumn = new DataGridTextColumn();
newColumn.SetValue(FrameworkElement.DataContextProperty, column);
if (columnSettings != null)
{
var settings = columnSettings.LoadContent() as DataGridColumnSettings;
if (settings != null)
{
settings.Setup(newColumn, column);
SetDataGridColumnSettings(newColumn, settings);
}
}
dataGrid.Columns.Add(newColumn);
}
}
}

public class DataGridColumnSettings : FrameworkElement
{
public static readonly DependencyProperty ColumnBindingPathProperty = DependencyProperty.Register("ColumnBindingPath", typeof(string),
typeof(DataGridColumnSettings), new PropertyMetadata(null, ColumnBindingPathChanged));

public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(object),
typeof(DataGridColumnSettings));

private DataGridTextColumn column;
private object viewModel;

public string ColumnBindingPath
{
get { return GetValue(ColumnBindingPathProperty) as string; }
set { SetValue(ColumnBindingPathProperty, value); }
}

public object Header
{
get { return GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
}

public void Setup(DataGridTextColumn column, object columnViewModel)
{
this.column = column;
viewModel = columnViewModel;
this.DataContext = columnViewModel;

if (Header is FrameworkElement)
{
(Header as FrameworkElement).DataContext = columnViewModel;
column.Header = Header;
}
else
{
BindingOperations.SetBinding(column, DataGridColumn.HeaderProperty, new Binding("Header") { Source = this });
}
column.Binding = new Binding(ColumnBindingPath);
}

private static void ColumnBindingPathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = d as DataGridColumnSettings;

if (target == null) return;

target.column.Binding = new Binding(e.NewValue as string);
}

}


XAML

<DataGrid 
t:DataGridColumns.DisplayColumns="{Binding Columns}"
ItemsSource="{Binding Rows}"
AutoGenerateColumns="False">
<t:DataGridColumns.ColumnSettingsTemplate>
<DataTemplate>
<t:DataGridColumnSettings
ColumnBindingPath="{Binding ColumnPath }">
<t:DataGridColumnSettings.Header>
<TextBlock Text="{Binding Header}"/>
</t:DataGridColumnSettings.Header>
</t:DataGridColumnSettings>
</DataTemplate>
</this:DataGridColumns.ColumnSettingsTemplate>
</DataGrid>


Nun weiss ich aber nicht wie folgende Zeile
ColumnBindingPath="{Binding  ColumnPath }"

implementiert werden muss.

Die Eigenschaft 'ColumnBindingPath' erwartet einen String, aber auf was zeigt dieser String?

Mit freundlichen Grüssen

Alex
News:
01.02.2013
Maendler 214 1 7
TOP TECHNOLOGIES CONSULTING GmbH