将更多属性绑定到GridViewColumnHeader

时间:2016-03-09 23:35:55

标签: wpf xaml binding

我想使用列标题制作GridView,例如下面的示例(一列的示例)。我的DataTemplate for header包含更多控件(TextBlock和TextBox),我无法弄清楚如何设置绑定以从模板(标题)访问单个控件。在我的例子中,而不是" aaa"设置HColumn1Line2属性的值。

<ListView Name="PeopleList" ItemsSource="{Binding People}">

    <ListView.Resources>
        <DataTemplate x:Key="MyHeaderTemplate">
            <StackPanel>
                <TextBlock Text="{Binding}"/>
                <TextBox Text="aaa"></TextBox>
            </StackPanel>
        </DataTemplate>
    </ListView.Resources>

    <ListView.View>
        <GridView ColumnHeaderTemplate="{StaticResource MyHeaderTemplate}">
            <GridViewColumn Header="{Binding HColumn1Line1}" DisplayMemberBinding="{Binding PeopleListItem}"/>
        </GridView>
    </ListView.View>

</ListView>

//代码

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        PeopleList.DataContext = this;
        _people.Add(new Person() { PeopleListItem = "some information" });

        HColumn1Line1 = "Header line #1";
        HColumn1Line2 = "text instead of aaa";
    }

    public List<Person> People { get { return _people; } }
    List<Person> _people = new List<Person>();

    public string HColumn1Line1 { get; set; }
    public string HColumn1Line2 { get; set; }
}

public class Person
{
    public string PeopleListItem { get; set; }
}

1 个答案:

答案 0 :(得分:1)

您遇到的主要问题是,MainWindow DataContext ListView实例MainWindow位于Content public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); PeopleList.DataContext = new ViewModel(); } } public class ViewModel { public List<Person> People { get { return _people; } } List<Person> _people = new List<Person>(); public string HColumn1Line1 { get; set; } = "Header line #1"; public string HColumn1Line2 { get; set; } = "text instead of aaa"; public ViewModel() { _people.Add(new Person() { PeopleListItem = "some information" }); } } public class Person { public string PeopleListItem { get; set; } } <ListView Name="PeopleList" ItemsSource="{Binding People}"> <ListView.Resources> <DataTemplate x:Key="MyHeaderTemplate"> <StackPanel> <TextBlock Text="{Binding HColumn1Line1}"/> <TextBox Text="{Binding HColumn1Line2}"></TextBox> </StackPanel> </DataTemplate> </ListView.Resources> <ListView.View> <GridView ColumnHeaderTemplate="{StaticResource MyHeaderTemplate}"> <GridViewColumn Header="{Binding}" DisplayMemberBinding="{Binding PeopleListItem}"/> </GridView> </ListView.View> </ListView> 例如“遍历树时超出逻辑树深度”等异常,因此最好使DataContext成为一个单独的对象,如下所示:

public class SpiralMatrix {

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);

    int size=sc.nextInt();
    int[][] arr=new int[size][size];
    int r,c;
    int elements =size*size;

    for(int i=0;i<size;i++)
        for(int j=0;j<size;j++)
            arr[i][j]=sc.nextInt();

    int count=0;

    int left=-1,down=1,right=2,up=-2;

    if(size%2==0) {r=size/2-1; c=size/2;}       
    else r=c=size/2;
    System.out.print(arr[r][c]+" ");

    try{
    while(count<elements-1){
    for(int i=0;i>left;i--){
        c=c-1;
        System.out.print(arr[r][c]+" ");
        count++;
    }

    for(int i=0;i<down;i++){
        r=r+1;
        System.out.print(arr[r][c]+" ");
        count++;
    }

    for(int i=0;i<right;i++){
        c=c+1;
        System.out.print(arr[r][c]+" ");
        count++;
    }

    for(int i=0;i>up;i--){
        r=r-1;
        System.out.print(arr[r][c]+" ");
        count++;
    }

    left=left-2;
    down=down+2;
    right=right+2;
    up=up-2;

    }

    }catch(ArrayIndexOutOfBoundsException e){}

    }

更新了XAML:

x