为 datagrid.columns 制定样式

发布于 2024-10-29 03:36:42 字数 864 浏览 0 评论 0原文

我还在为某件事而挣扎。我在互联网上看到的所有示例都在数据网格本身的资源中应用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2024-11-05 03:36:42

据我所知,您不能这样做,因为 DataGrid.Columns 属性没有设置访问器,否则可以使用 Setter 在 Style 中设置它。

您只能直接设置列,看起来也不能使用集合绑定它们。

这是一种在多个网格中获取相同列的非常迂回且丑陋的方法:

资源:

<x:Array x:Key="MyColumns" Type="{x:Type DataGridColumn}">
    <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"/>
</x:Array>

数据网格:

<DataGrid ... Loaded="dataGrid_Loaded"/>

事件:

private void dataGrid_Loaded(object sender, RoutedEventArgs e)
{
    DataGridColumn[] columns = Resources["MyColumns"] as DataGridColumn[];
    DataGrid dg = sender as DataGrid;
    dg.Columns.Clear();
    foreach (var item in columns)
    {
        dg.Columns.Add(item);
    }
}

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:

<x:Array x:Key="MyColumns" Type="{x:Type DataGridColumn}">
    <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"/>
</x:Array>

Datagrid:

<DataGrid ... Loaded="dataGrid_Loaded"/>

Event:

private void dataGrid_Loaded(object sender, RoutedEventArgs e)
{
    DataGridColumn[] columns = Resources["MyColumns"] as DataGridColumn[];
    DataGrid dg = sender as DataGrid;
    dg.Columns.Clear();
    foreach (var item in columns)
    {
        dg.Columns.Add(item);
    }
}
泼猴你往哪里跑 2024-11-05 03:36:42

您还可以在 UserControl./Window.Resources 中添加 Datagrid ,然后使用 ContentControl 传递 < code>DataContext 到它。

请注意 x:Key="MyDataGrid"x:Shared="False" 的使用,

<UserControl.Resources>
    <DataGrid x:Key="MyDataGrid" x:Shared="False" 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>
</UserControl.Resources>

然后在您的视图中,您可以通过执行以下操作来调用它:

<ContentControl Content="{StaticResource MyDataGrid}" DataContext="{Binding MyFirstData}" />
<ContentControl Content="{StaticResource MyDataGrid}" DataContext="{Binding MySecondData}" />

You can also add your Datagrid in your UserControl./Window.Resources and then use a ContentControl passing the DataContext to it.

Notice the use of x:Key="MyDataGrid" and x:Shared="False"

<UserControl.Resources>
    <DataGrid x:Key="MyDataGrid" x:Shared="False" 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>
</UserControl.Resources>

Then in your view, you can call this by doing:

<ContentControl Content="{StaticResource MyDataGrid}" DataContext="{Binding MyFirstData}" />
<ContentControl Content="{StaticResource MyDataGrid}" DataContext="{Binding MySecondData}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文