数据网格中的头绑定,vb.net [英] header binding in datagrid, vb.net
问题描述
大家好,我有一些问题我需要用英语或法语来绑定数据网格的数据,
到目前为止我做了什么。
在我的xaml文件中
DataGridTextColumn Binding =" {Binding Path = DataContext.LCOwnerName}">
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < DataGridTextColumn.HeaderTemplate>
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < DataTemplate>
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < TextBlock Text =" {Binding Path = DataContext.LCOwnerName,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged,RelativeSource = {RelativeSource
AncestorType = Window}}"宽度= QUOT; 176"背景= QUOT;#FFE61313"高度="42"/>
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < / DataTemplate>
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < /DataGridTextColumn.HeaderTemplate>
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < / DataGridTextColumn>
在我的MVVM(VB.NET)中
Public Property DataContext As DataHeader
Public Class DataHeader
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Private _lCOwnerName As String
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP;公共财产LCOwnerName As String
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;获得¥b $ b &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;返回_lCOwnerName
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;结束获取¥b $ b &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;设置(值为字符串)
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _lCOwnerName =值
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;结束集
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP;结束物业
&NBSP; &NBSP; &NBSP;结束等级
Public Sub New() &NBSP;
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Dim _data As DataHeader = New DataHeader With {.LCOwnerName =" Header2"}
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; DataContext = _data &NBSP; &NBSP; &NBSP;
&NBSP; &NBSP; &NBSP; End Sub
该代码不会显示任何错误,但不会在datagrid的标题上显示标签"header2",不显示任何内容。
嗨帮助人,
根据你的描述,你想使用DataGrid来在DataGridColumn中加载数据,并且您还希望对DataGridColumn标头使用绑定。
DataGrid控件提供了一种灵活的方式来显示行和列中的数据集合。 DataGrid包含内置列类型和用于托管自定义内容的模板列。内置行类型包含一个下拉详细信息
部分,您可以使用该部分显示单元格值下方的其他内容。
所以你应该使用DataGrid Itemsource的集合,对于一个DataGridColumn头,它使用一个简单的属性,如string。
< DataGrid AutoGenerateColumns =" False" ItemsSource =" {Binding datas}">
< DataGrid.Columns>
< DataGridTextColumn Binding =" {Binding LCOwnerName}">
< DataGridTextColumn.HeaderTemplate>
< DataTemplate>
< TextBlock
Width =" 200"
高度="30"
Text =" {Binding DataContext.header,RelativeSource = {RelativeSource AncestorType = Window}}" />
< / DataTemplate>
< /DataGridTextColumn.HeaderTemplate>
< / DataGridTextColumn>
< /DataGrid.Columns>
< / DataGrid>
Public Class Window5
Public Property datas As ObservableCollection(Of DataHeader)
Public Property header As String
Public Sub New()
'设计者需要此调用。
InitializeComponent()
'在InitializeComponent()调用后添加任何初始化。
datas = New ObservableCollection(Of DataHeader)From {
New DataHeader()With {.LCOwnerName =" test1"},
New DataHeader()With {.LCOwnerName =" test2" },
新DataHeader()使用{.LCOwnerName =" test2"},
新DataHeader()使用{.LCOwnerName =" test2"},
新DataHeader()使用{ .LCOwnerName =" test2"},
新DataHeader()使用{.LCOwnerName =" test2"},
新DataHeader()使用{.LCOwnerName =" test2"}
}
header =" header1"
Me.DataContext = Me
End Sub
End Class
Public Class DataHeader
Private _lCOwnerName As String
Public Property LCOwnerName As String
获取
返回_lCOwnerName
结束获取
设置(值为字符串)
_lCOwnerName = value
结束集
结束物业
结束类这里我做一个示例,对DataGrid使用Observablecollection,为DataGridColumn头使用一个字符串属性头。
关于DataGrid,你可以查看以下文章了解详细信息:
https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.datagrid?view=netframework-4.7.2
Best Regadrs,
Cherry
Hi everybody, I have a issues I need to bindinf\g of the header datagrid by english or french,
what I did until now is this.
in my xaml file
DataGridTextColumn Binding="{Binding Path=DataContext.LCOwnerName}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=DataContext.LCOwnerName,Mode=TwoWay ,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType=Window}}" Width="176" Background="#FFE61313" Height="42"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>in my MVVM (VB.NET)
Public Property DataContext As DataHeader
Public Class DataHeader
Private _lCOwnerName As String
Public Property LCOwnerName As String
Get
Return _lCOwnerName
End Get
Set(value As String)
_lCOwnerName = value
End Set
End Property
End Class Public Sub New()
Dim _data As DataHeader = New DataHeader With {.LCOwnerName = "Header2"}
DataContext = _data
End Subthat code don't show me any error, but don't show on the header of the datagrid the label "header2", show nothing.
解决方案Hi helpingpeople,
According to your description, you want to use DataGrid to load data in DataGridColumn, and you also want to use binding for DataGridColumn header.
The DataGrid control provides a flexible way to display a collection of data in rows and columns. The DataGrid includes built-in column types and a template column for hosting custom content. The built-in row type includes a drop-down details section that you can use to display additional content below the cell values.
So you should use collection for DataGrid Itemsource, for one DataGridColumn header, it use one simple property, like string.
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding datas}"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding LCOwnerName}"> <DataGridTextColumn.HeaderTemplate> <DataTemplate> <TextBlock Width="200" Height="30" Text="{Binding DataContext.header, RelativeSource={RelativeSource AncestorType=Window}}" /> </DataTemplate> </DataGridTextColumn.HeaderTemplate> </DataGridTextColumn> </DataGrid.Columns> </DataGrid>
Public Class Window5 Public Property datas As ObservableCollection(Of DataHeader) Public Property header As String Public Sub New() ' This call is required by the designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. datas = New ObservableCollection(Of DataHeader) From { New DataHeader() With {.LCOwnerName = "test1"}, New DataHeader() With {.LCOwnerName = "test2"}, New DataHeader() With {.LCOwnerName = "test2"}, New DataHeader() With {.LCOwnerName = "test2"}, New DataHeader() With {.LCOwnerName = "test2"}, New DataHeader() With {.LCOwnerName = "test2"}, New DataHeader() With {.LCOwnerName = "test2"} } header = "header1" Me.DataContext = Me End Sub End Class Public Class DataHeader Private _lCOwnerName As String Public Property LCOwnerName As String Get Return _lCOwnerName End Get Set(value As String) _lCOwnerName = value End Set End Property End ClassHere I do one sample, use Observablecollection for DataGrid, and one string property header for DataGridColumn header.
About DataGrid, you can take a look the following article for detailed info:
https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.datagrid?view=netframework-4.7.2
Best Regadrs,
Cherry
这篇关于数据网格中的头绑定,vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!