如何在代码中绑定GridViewColumn的DisplayMemberBinding

时间:2012-10-29 23:10:13

标签: c# wpf binding gridviewcolumn

我有一个使用GridViewColumnCollection的MultiColumn TreeView。我的情况,我不知道会有多少列,也不知道它们的标题名称。这是在运行时发现的。

因此,我需要在代码中创建这些列并动态绑定它们。

好的 - 创作很简单:

GridViewColumn c = new GridViewColumn();
c.Header = "Next Column";
myTree.Columns.Add(c);

现在我绊倒了 - 假设我希望绑定到我的viewModel的“MyName”属性:

Binding myBinding = new Binding(??);
myBinding.Source = ??
BindingOperations.SetBinding(myTree,GridViewColumn.????  , myBinding);

现在是这个

的模板
 <DataTemplate x:Key="CellTemplate_Name">
        <DockPanel>
            <ToggleButton x:Name="Expander"
                          Margin="{Binding Level,
                                           Converter={StaticResource LevelToIndentConverter},
                                           RelativeSource={RelativeSource AncestorType={x:Type l:TreeListViewItem}}}"
                          ClickMode="Press"
                          IsChecked="{Binding Path=IsExpanded,
                                              RelativeSource={RelativeSource AncestorType={x:Type l:TreeListViewItem}}}"
                          Style="{StaticResource ExpandCollapseToggleStyle}" />
            <TextBlock Text="{Binding Name}" />
.                
.
.
    <Style TargetType="{x:Type l:TreeListViewItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type l:TreeListViewItem}">
                    <StackPanel>
                        <Border Name="Bd"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Padding="{TemplateBinding Padding}">
                            <GridViewRowPresenter x:Name="PART_Header"
                                                  Columns="{Binding Path=Columns,
                                                                    RelativeSource={RelativeSource AncestorType={x:Type l:TreeListView}}}"
                                                  Content="{TemplateBinding Header}" />
                        </Border>
                        <ItemsPresenter x:Name="ItemsHost" />
                    </StackPanel>

XAML中的列插入和绑定显示为:

    <l:TreeListView x:Name="myTree" ItemsSource="{Binding MySource}">
    <l:TreeListView.Columns>
        <GridViewColumn x:Name="GridViewColumn0" CellTemplate="{StaticResource CellTemplate_Name}" 
                        Header="Name" />
        <GridViewColumn Width="60"
                        DisplayMemberBinding="{Binding Description}"
                        Header="Description" />

任何有关SetBinding的帮助都将非常感谢。我一直在搜寻,直到我的手指脱落。

更新:      优秀的答案:

        GridViewColumn c = new GridViewColumn();
        c.Header = "Next Column";
        myTree.Columns.Add(c);

        Binding myBinding = new Binding("MyName"); 
        myBinding.Source = viewModel;
        BindingOperations.SetBinding(myTree.Columns[myTree.Columns.Count - 1], 
            GridViewColumn.HeaderProperty, 
            myBinding);

绑定现在可以完美地对抗Header - 非常感谢你。

2 个答案:

答案 0 :(得分:2)

(从评论中提取答案:)

您可以使用以下代码手动设置绑定:

Binding myBinding = new Binding("MyName");
myBinding.Source = viewModel;
BindingOperations.SetBinding(myTree.Columns[i], GridViewColumn.HeaderProperty, myBinding);

其中i是列数。

要设置DisplayMemberBinding,可以使用更简单的代码:

Binding descriptionBinding = new Binding("Description");
myTree.Columns[i].DisplayMemberBinding = descriptionBinding;

答案 1 :(得分:0)

我将它设为方法NewGridViewColumn,将参数headerbindingName设置为方法,您将获得一个绑定完成的GridViewColumn:

    private GridViewColumn NewGridViewColumn(string header, string bindingName)
    {
        GridViewColumn gvc = new GridViewColumn();
        gvc.Header = header;
        gvc.DisplayMemberBinding = new Binding(bindingName);            
        return gvc;
    }

然后在ListView xaml的情况下:

<Grid>
        <ListView Margin="5" Name="ListViewTest"></ListView>
</Grid>

您可以使用NewGridViewColumn在构造函数中轻松地将GridViewColumn添加到ListView:

    public MainWindow()
    {
        InitializeComponent();

        //Give the Source to ListViewTest
        //ProduceNames() makes a List<NameClass> that 
        //NameClass has 3 property Name1,Name2,Nmae3
        ListViewTest.ItemsSource = ProduceNames();

        GridView gview = new GridView();

        //add some GridViewColumns to the gview
        gview.Columns.Add(NewGridViewColumn("FirstName","Name1"));
        gview.Columns.Add(NewGridViewColumn("SecondName","Name2"));
        gview.Columns.Add(NewGridViewColumn("ThirdName","Name3"));

        //set the gview to the ListViewTest's View
        ListViewTest.View = gview;

    }

结果:

enter image description here