2011-04-11 94 views
0

我嘗試從DataTemplate綁定到myGridViewColumn。我想在網格視圖標題中顯示自定義文本(如'Caption =「Name」'),但它不起作用!WPF從DataTemplate綁定到GridViewColumn

XAML的DataTemplate:

<Window x:Class="DataTemplateTest.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:my="clr-namespace:DataTemplateTest" Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <DataTemplate x:Key="System3" DataType="{x:Type my:MyGridViewColumn}"> 
      <StackPanel Grid.Column="0" Margin="2" Orientation="Horizontal"> 
       <TextBlock Text="113 " Foreground="Red"/> 
       <TextBlock Text="{Binding Path=Caption}"/> 
       <TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:MyGridViewColumn}}, Path=Caption}"/> 
       <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Caption}"/> 
      </StackPanel> 
     </DataTemplate>  
    </Window.Resources> 
    <Grid> 
     <ListView 
      Height="auto" 
      SelectionMode="Single" 
      Name="lstvMain" 
      > 
      <ListView.View> 
       <GridView> 
        <my:MyGridViewColumn HeaderTemplate="{StaticResource ResourceKey=System3}" Width="150" Caption="Name" DisplayMemberBinding="{Binding Path=Name}"/> 
        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 
        <GridViewColumn Header="Surname" DisplayMemberBinding="{Binding Path=Surname, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 
       </GridView> 
      </ListView.View> 
     </ListView> 
    </Grid> 
</Window> 

和C#代碼

#region code 
using System; 
using System.Collections.Generic; 
using System.Windows; 
using System.Windows.Controls; 

namespace DataTemplateTest 
{ 
    public partial class MainWindow 
    { 
     public List<User> Users = new List<User> { new User { Name = "John", Surname = "Smith" }, new User { Name = "Joe", Surname = "Brown" } }; 

     public MainWindow() 
     { 
      InitializeComponent(); 
      lstvMain.ItemsSource = Users; 
     } 
    } 
    public class User : DependencyObject 
    { 
     public static DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(String), typeof(User), new PropertyMetadata(String.Empty)); 
     public String Name 
     { 
      get { return (String)GetValue(NameProperty); } 
      set 
      { 
       SetValue(NameProperty, value); 
       OnPropertyChanged(new DependencyPropertyChangedEventArgs(NameProperty, null, value)); 
      } 
     } 

     public static DependencyProperty SurnameProperty = DependencyProperty.Register("Surname", typeof(String), typeof(User), new PropertyMetadata(String.Empty)); 
     public String Surname 
     { 
      get { return (String)GetValue(SurnameProperty); } 
      set 
      { 
       SetValue(SurnameProperty, value); 
       OnPropertyChanged(new DependencyPropertyChangedEventArgs(SurnameProperty, null, value)); 
      } 
     } 
    } 
    public class MyGridViewColumn : GridViewColumn 
    { 
     public static DependencyProperty CaptionProperty = DependencyProperty.Register("Caption", typeof(String), typeof(MyGridViewColumn), new PropertyMetadata(String.Empty)); 
     public String Caption 
     { 
      get { return (String)GetValue(CaptionProperty); } 
      set 
      { 
       SetValue(CaptionProperty, value); 
       OnPropertyChanged(new DependencyPropertyChangedEventArgs(CaptionProperty, null, value)); 
      } 
     } 
    } 
} 
#endregion 

任何幫助將不勝感激!

回答

3

您定義的HeaderTemplate,但沒有Header,所以模板沒有DataContext

但無論如何,你將不能夠直接與綁定定義標題,因爲GridViewColumn不繼承DataContext 。我寫了一篇關於解決這個問題的解決方案here

+0

非常感謝!這個問題讓我很生氣,因爲我嘗試過使用「RelativeSource FindAncestor」,但它沒有任何效果......奇怪的是,該列不屬於視覺或邏輯樹。無論如何感謝快速和有益的答案! :) – 2011-04-13 09:38:01

相關問題