3

我有一个使用 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);

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

4

2 回答 2

3

(从评论中提取答案:)

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

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;
于 2012-10-31T19:13:47.890 回答
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>

您可以使用以下方法轻松地将 GridViewColumn 添加到构造函数中的 ListView NewGridViewColumn

    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;

    }

结果:

在此处输入图像描述

于 2016-11-23T03:58:36.587 回答