wpf 如何在代码中绑定GridViewColumn的DisplayMemberBinding
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13130993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to bind GridViewColumn's DisplayMemberBinding in code
提问by Walt Lounsbury
I have a MultiColumn TreeView which uses a GridViewColumnCollection. I my situation, I don't know how many columns will be there, nor their header names. That's discovery at run time.
我有一个使用 GridViewColumnCollection 的 MultiColumn TreeView。我的情况,我不知道会有多少列,也不知道它们的标题名称。这就是运行时的发现。
Hence I have need to create these columns in code and bind dynamically to them.
因此,我需要在代码中创建这些列并动态绑定到它们。
Okay - creation is simple enough:
好的 - 创建很简单:
GridViewColumn c = new GridViewColumn();
c.Header = "Next Column";
myTree.Columns.Add(c);
Now where I stumble - Suppose I wish to bind to my viewModel's "MyName" property:
现在我绊倒了 - 假设我希望绑定到我的 viewModel 的“MyName”属性:
Binding myBinding = new Binding(??);
myBinding.Source = ??
BindingOperations.SetBinding(myTree,GridViewColumn.???? , myBinding);
Now the template for this
现在这个模板
<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>
Where column insertion and binding in XAML appears as:
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" />
Any help with SetBinding here would be greatly appreciated. I've searched till my fingers fell off.
任何有关 SetBinding 的帮助将不胜感激。我一直在寻找,直到我的手指掉下来。
Update: Excellent answer:
更新:优秀的答案:
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);
The binding now works against the Header perfectly - thank you so much.
绑定现在可以完美地对抗标题 - 非常感谢。
回答by Vlad
(Extracting an answer from comments:)
(从评论中提取答案:)
You can set the binding manually with the code like this:
您可以使用如下代码手动设置绑定:
Binding myBinding = new Binding("MyName");
myBinding.Source = viewModel;
BindingOperations.SetBinding(myTree.Columns[i], GridViewColumn.HeaderProperty, myBinding);
where iis the number of column.
其中i是列数。
For setting the DisplayMemberBinding, a simpler code can be used:
要设置DisplayMemberBinding,可以使用更简单的代码:
Binding descriptionBinding = new Binding("Description");
myTree.Columns[i].DisplayMemberBinding = descriptionBinding;
回答by yu yang Jian
I make it a method NewGridViewColumn, set the parameter headerand bindingNameto the method, and you will get a binding-completed GridViewColumn:
我把它变成一个方法NewGridViewColumn,设置参数header和bindingName方法,你会得到一个绑定完成的 GridViewColumn:
private GridViewColumn NewGridViewColumn(string header, string bindingName)
{
GridViewColumn gvc = new GridViewColumn();
gvc.Header = header;
gvc.DisplayMemberBinding = new Binding(bindingName);
return gvc;
}
.
.
Then in case of ListView xaml like this:
然后在 ListView xaml 这样的情况下:
<Grid>
<ListView Margin="5" Name="ListViewTest"></ListView>
</Grid>
You can easily add the GridViewColumn to ListView in Constructor using NewGridViewColumn:
您可以使用以下方法轻松地将 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;
}
the result:
结果:


