두 개의 DataTemplate을 함께 사용하여 필요한 것을 쉽게 빌드 할 수 있습니다. 외부 DataTemplate은 단순히 내부 용 DataContext를 설정합니다 다음과 같이 DataTemplate을 사용합니다.
유일한 까다로운 점은 GridViewColumn에서이 설정을 편리하게하고 있습니다. 코드에서 동등하게
<GridViewColumn
my:GVCHelper.DisplayPath="FirstName"
my:GVCHelper.Template="{StaticResource DisplayTemplate}" />
을 또는 : 나는 당신이 쓸 수 있도록 연결된 속성에이를 것
"DisplayTemplate"라는 DataTemplate을을 야기 이들 중 하나는 FIRSTNAME을 표시하는 데 사용되는
var col = new GridViewColumn();
GVCHelper.SetDisplayPath(col, "FirstName");
GVCHelper.SetTemplate(col, (DataTemplate)FindResource("DisplayTemplate"));
그 칼럼에
도우미 클래스
이 구현 될 것 같은 :
작동 원리
public class GVCHelper : DependencyObject
{
public static string GetDisplayPath(DependencyObject obj) { return (string)obj.GetValue(DisplayPathProperty); }
public static void SetDisplayPath(DependencyObject obj, string value) { obj.SetValue(DisplayPathProperty, value); }
public static readonly DependencyProperty DisplayPathProperty = DependencyProperty.RegisterAttached("DisplayPath", typeof(string), typeof(GVCHelper), new PropertyMetadata
{
PropertyChangedCallback = (obj, e) => Update(obj)
});
public static DataTemplate GetTemplate(DependencyObject obj) { return (DataTemplate)obj.GetValue(TemplateProperty); }
public static void SetTemplate(DependencyObject obj, DataTemplate value) { obj.SetValue(TemplateProperty, value); }
public static readonly DependencyProperty TemplateProperty = DependencyProperty.RegisterAttached("Template", typeof(DataTemplate), typeof(GVCHelper), new PropertyMetadata
{
PropertyChangedCallback = (obj, e) => Update(obj)
});
private static void Update(DependencyObject obj)
{
var path = GetDisplayPath(obj);
var template = GetTemplate(obj);
if(path!=null && template!=null)
{
var factory = new FrameworkElementFactory(typeof(ContentPresenter));
factory.SetBinding(ContentPresenter.ContentProperty, new Binding(path));
factory.SetValue(ContentPresenter.ContentTemplateProperty, template);
obj.SetValue(GridViewColumn.CellTemplateProperty,
new DataTemplate { VisualTree = factory };
}
}
}
: 속성이 설정되어 있습니다 때마다, 새로운 DataTemplate을 구축하고 GridViewColumn.CellTemplate 속성이 업데이트됩니다.