为 datagrid.columns 制定样式
我还在为某件事而挣扎。我在互联网上看到的所有示例都在数据网格本身的资源中应用 datagrid.columns 。我不想要这样。我想在 window.resources 中定义一个模板或样式,它必须通过绑定应用于数据网格。我可以吗?我怎样才能做到这一点?这是我的控件资源中的 xaml:
<DataGrid Name="dgFruit" ItemsSource="{Binding}" AutoGenerateColumns="false" Style="{StaticResource datagrid}" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Name}" Header="Name"/>
<DataGridTextColumn Binding="{Binding Path=Color}" Header="Color"/>
<DataGridCheckBoxColumn Binding="{Binding Path=Mjummy}" Header="Mjummy"/>
<DataGridTextColumn Binding="{Binding Path=Number}" Header="Number"/>
<DataGridTextColumn Binding="{Binding Path=Pits.Count}" Header="Pits"/>
</DataGrid.Columns>
</DataGrid>
I'm still struggling on something. All examples i see on the internet apply datagrid.columns in the resources of the datagrid itself. I don't want that. I want to define a template or style in the window.resources which has to apply to the datagrid through binding. Can i and how can i do that? This is the xaml in my control's resources :
<DataGrid Name="dgFruit" ItemsSource="{Binding}" AutoGenerateColumns="false" Style="{StaticResource datagrid}" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Name}" Header="Name"/>
<DataGridTextColumn Binding="{Binding Path=Color}" Header="Color"/>
<DataGridCheckBoxColumn Binding="{Binding Path=Mjummy}" Header="Mjummy"/>
<DataGridTextColumn Binding="{Binding Path=Number}" Header="Number"/>
<DataGridTextColumn Binding="{Binding Path=Pits.Count}" Header="Pits"/>
</DataGrid.Columns>
</DataGrid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,您不能这样做,因为
DataGrid.Columns
属性没有设置访问器,否则可以使用 Setter 在 Style 中设置它。您只能直接设置列,看起来也不能使用集合绑定它们。
这是一种在多个网格中获取相同列的非常迂回且丑陋的方法:
资源:
数据网格:
事件:
You cannot do that as far as i know since there is no set-accessor for the
DataGrid.Columns
property, otherwise it could be set in a Style using a Setter.You can only set columns directly, you cannot bind them using a collection either it seems.
Here is a very roundabout and ugly way to get the same columns in multiple grids:
Resources:
Datagrid:
Event:
您还可以在
UserControl.
/Window.Resources
中添加Datagrid
,然后使用ContentControl
传递 < code>DataContext 到它。请注意
x:Key="MyDataGrid"
和x:Shared="False"
的使用,然后在您的视图中,您可以通过执行以下操作来调用它:
You can also add your
Datagrid
in yourUserControl.
/Window.Resources
and then use aContentControl
passing theDataContext
to it.Notice the use of
x:Key="MyDataGrid"
andx:Shared="False"
Then in your view, you can call this by doing: