如何在数据网格的行标题模板中设置值?
我正在尝试使用绑定来制定行标题模板,但绑定不起作用。这就是我到目前为止所拥有的:
<DataGrid Name="dgFruit" ItemsSource="{Binding}" AutoGenerateColumns="false"><!--ItemTemplate="{StaticResource datagrid}"-->
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<Label Content="{Binding Path=Number, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
</DataTemplate>
</DataGrid.RowHeaderTemplate>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Name}" Header="Name"/>
<DataGridTextColumn Binding="{Binding Path=Color}" Header="Color"/>
<DataGridCheckBoxColumn Binding="{Binding Path=Mjummy}" Header="Mjummy"/>
</DataGrid.Columns>
</DataGrid>
如果我将内容设置为一个值(例如“45”),我将获得行标题,但是当我尝试绑定时,行标题不再显示。我正在尝试绑定到与列中的值属于同一类的属性。
那么,如何从列表中检索 Number 值呢?
提前致谢
I'm trying to work out a rowheadertemplate with a binding, but the binding doesn't work. This is what i have so far:
<DataGrid Name="dgFruit" ItemsSource="{Binding}" AutoGenerateColumns="false"><!--ItemTemplate="{StaticResource datagrid}"-->
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<Label Content="{Binding Path=Number, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
</DataTemplate>
</DataGrid.RowHeaderTemplate>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Name}" Header="Name"/>
<DataGridTextColumn Binding="{Binding Path=Color}" Header="Color"/>
<DataGridCheckBoxColumn Binding="{Binding Path=Mjummy}" Header="Mjummy"/>
</DataGrid.Columns>
</DataGrid>
I'm getting row headers if i set the content to a value, like "45", but when i try to bind, the rowheaders aren't shown anymore. I'm trying to bind to a property from the same class as the values in the columns.
So, how can i retrieve the Number value from my list?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎把它放在一种风格上效果更好:
seems putting it in a style works better:
只需删除RelativeSource 的东西:
Number 是dataItem 的属性,而不是Grid 本身。
just remove the RelativeSource thing:
The Number is a property of the dataItem, not the Grid itself.