c# - 如何在代码中绑定(bind)GridViewColumn的DisplayMemberBinding

标签 c# wpf binding gridviewcolumn

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

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

好的 - 创建很简单:

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

现在我遇到了什么问题 - 假设我想绑定(bind)到我的 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 中的列插入和绑定(bind)显示为:

    <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);

绑定(bind)现在可以完美地针对 header - 非常感谢。

最佳答案

(从评论中提取答案:)

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

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;

关于c# - 如何在代码中绑定(bind)GridViewColumn的DisplayMemberBinding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13130993/

相关文章:

c# - 在 Visual Studio 中创建转换器的最快方法

c# - 带集合的 MVVM 模型。在模型中使用或不使用 observablecollection

c# - WPF 绑定(bind) View 作为内容

c# - 读取远程 MS Access 数据库 C#

c# - FormCollection 到 ExpandoObject

c# - ASP.NET 下载处理程序适用于 IE 但不适用于 Chrome

c# - WPF 将绑定(bind)传递到我的用户控件

c# - 如何使 Microsoft.VisualStudio.Diagnostics.UI.Controls.MultiSelectComboBox 工作

c# - 关闭 RecognizesAccessKey 的 WPF DataGrid

c# - 绑定(bind)到集合计数