DataGridColumn标题内容WPF

DataGridColumn标题内容WPF,wpf,silverlight,datagrid,datatemplate,wpfdatagrid,Wpf,Silverlight,Datagrid,Datatemplate,Wpfdatagrid,我想定位一个演示文稿,这一次我的大脑冻结了。我使用的是标记扩展,因此不使用此扩展: <DataGridTextColumn Header="Number" Binding="{Binding Number}" Width="60" CellStyle="{StaticResource NumberStyle}" /> 但是,由于数据网格中的头不是可视化树的一部分(尽管看起来它们确实应该是!),我必须再次明确地输入Resx的名称,如中所示 <DataGr

我想定位一个演示文稿,这一次我的大脑冻结了。我使用的是标记扩展,因此不使用此扩展:

<DataGridTextColumn Header="Number" Binding="{Binding Number}" Width="60" CellStyle="{StaticResource NumberStyle}" />
但是,由于数据网格中的头不是可视化树的一部分(尽管看起来它们确实应该是!),我必须再次明确地输入Resx的名称,如中所示

            <DataGridTextColumn Header="{Resx ResxName=NMoneys.Presentation.Resources.AllocatorView, Key=ColumnHeader_Number}" Binding="{Binding Number}" Width="60" CellStyle="{StaticResource NumberStyle}" />

我以前遇到过这种情况,并见过一些转发DC的技术,但在这种情况下,不值得这么麻烦

干杯,

Berryl

我假设您有代表您的实体的模型。我所做的是使用数据注释。这是一个例子。编辑:提供带有屏幕截图的MVVM类

型号

using System.ComponentModel.DataAnnotations;

using Silverlight.Infrastructure.Properties;

namespace Silverlight.Infrastructure.Models
{
    public class Customer
    {
        [Display(ResourceType = typeof(Resources), Name = "CustomerIdHeader")]
        public int Id { get; set; }

        // There must be an entry CustomerNameHeader in the resources else it 
        // will throw an exception
        [Display(ResourceType = typeof(Resources), Name = "CustomerNameHeader")]
        public string Name { get; set; }
    }
}
using Silverlight.Infrastructure.Models

namespace Silverlight.ModuleA.ViewModels
{
    //Prism namespaces
    public class CustomerViewModel : NotificationObject
    {
        public CustomerViewModel()
        {   
            // service for my customers.
            CustomeService customerService = new CustomerService();
            Customers = customerService.GetCustomers()
        }   

        public ObservableCollection<Customer> Customers {get;set;}
    }
}
标题内容将自动绑定到属性的显示

视图模型

using System.ComponentModel.DataAnnotations;

using Silverlight.Infrastructure.Properties;

namespace Silverlight.Infrastructure.Models
{
    public class Customer
    {
        [Display(ResourceType = typeof(Resources), Name = "CustomerIdHeader")]
        public int Id { get; set; }

        // There must be an entry CustomerNameHeader in the resources else it 
        // will throw an exception
        [Display(ResourceType = typeof(Resources), Name = "CustomerNameHeader")]
        public string Name { get; set; }
    }
}
using Silverlight.Infrastructure.Models

namespace Silverlight.ModuleA.ViewModels
{
    //Prism namespaces
    public class CustomerViewModel : NotificationObject
    {
        public CustomerViewModel()
        {   
            // service for my customers.
            CustomeService customerService = new CustomerService();
            Customers = customerService.GetCustomers()
        }   

        public ObservableCollection<Customer> Customers {get;set;}
    }
}
使用Silverlight.Infrastructure.Models
名称空间Silverlight.ModuleA.ViewModels
{
//棱镜名称空间
公共类CustomerServiceModel:NotificationObject
{
公共CustomerServiceModel()
{   
//为我的顾客服务。
CustomeService customerService=新customerService();
Customers=customerService.GetCustomers()
}   
公共ObservableCollection客户{get;set;}
}
}
查看

<UserControl>
    <Grid x:Name="LayoutRoot">
        <data:DataGrid x:Name="CustomerGrid" ItemsSource="{Binding Customers}" AutoGenerateColumns="False" >
                    <data:DataGrid.Columns>
                        <data:DataGridTextColumn Binding="{Binding Id, Mode=TwoWay}" />
                        <data:DataGridTextColumn Binding="{Binding Name, Mode=TwoWay}"/>
                        <data:DataGridTextColumn Binding="{Binding Surname, Mode=TwoWay}"/>
                        <data:DataGridTextColumn Binding="{Binding Company, Mode=TwoWay}"/>
                    </data:DataGrid.Columns>
            </data:DataGrid>
    </Grid>
</UserControl>

然后是typeof(StringLibrary)中的资源

如您所见,显示的名称等于资源文件中的ResourceKey。大约2-3周前,当我在开发和测试XML时,我第一次提出了注释。但是像AutoMapper这样的东西可以将你的对象映射到像这样的POCO,或者你甚至可以使用WCF RIA服务,你可以有类似的注释。WCF RIA实际上是使用它们的人,但我只是以不同的方式使用它们

要做到这一点,可能有一些技巧。但是当你这样做的时候,你真的得到了一个简单的解决方案


我希望我已经提供了足够的信息。

我假设您有代表您的实体的模型。我所做的是使用数据注释。这是一个例子。编辑:提供带有屏幕截图的MVVM类

型号

using System.ComponentModel.DataAnnotations;

using Silverlight.Infrastructure.Properties;

namespace Silverlight.Infrastructure.Models
{
    public class Customer
    {
        [Display(ResourceType = typeof(Resources), Name = "CustomerIdHeader")]
        public int Id { get; set; }

        // There must be an entry CustomerNameHeader in the resources else it 
        // will throw an exception
        [Display(ResourceType = typeof(Resources), Name = "CustomerNameHeader")]
        public string Name { get; set; }
    }
}
using Silverlight.Infrastructure.Models

namespace Silverlight.ModuleA.ViewModels
{
    //Prism namespaces
    public class CustomerViewModel : NotificationObject
    {
        public CustomerViewModel()
        {   
            // service for my customers.
            CustomeService customerService = new CustomerService();
            Customers = customerService.GetCustomers()
        }   

        public ObservableCollection<Customer> Customers {get;set;}
    }
}
标题内容将自动绑定到属性的显示

视图模型

using System.ComponentModel.DataAnnotations;

using Silverlight.Infrastructure.Properties;

namespace Silverlight.Infrastructure.Models
{
    public class Customer
    {
        [Display(ResourceType = typeof(Resources), Name = "CustomerIdHeader")]
        public int Id { get; set; }

        // There must be an entry CustomerNameHeader in the resources else it 
        // will throw an exception
        [Display(ResourceType = typeof(Resources), Name = "CustomerNameHeader")]
        public string Name { get; set; }
    }
}
using Silverlight.Infrastructure.Models

namespace Silverlight.ModuleA.ViewModels
{
    //Prism namespaces
    public class CustomerViewModel : NotificationObject
    {
        public CustomerViewModel()
        {   
            // service for my customers.
            CustomeService customerService = new CustomerService();
            Customers = customerService.GetCustomers()
        }   

        public ObservableCollection<Customer> Customers {get;set;}
    }
}
使用Silverlight.Infrastructure.Models
名称空间Silverlight.ModuleA.ViewModels
{
//棱镜名称空间
公共类CustomerServiceModel:NotificationObject
{
公共CustomerServiceModel()
{   
//为我的顾客服务。
CustomeService customerService=新customerService();
Customers=customerService.GetCustomers()
}   
公共ObservableCollection客户{get;set;}
}
}
查看

<UserControl>
    <Grid x:Name="LayoutRoot">
        <data:DataGrid x:Name="CustomerGrid" ItemsSource="{Binding Customers}" AutoGenerateColumns="False" >
                    <data:DataGrid.Columns>
                        <data:DataGridTextColumn Binding="{Binding Id, Mode=TwoWay}" />
                        <data:DataGridTextColumn Binding="{Binding Name, Mode=TwoWay}"/>
                        <data:DataGridTextColumn Binding="{Binding Surname, Mode=TwoWay}"/>
                        <data:DataGridTextColumn Binding="{Binding Company, Mode=TwoWay}"/>
                    </data:DataGrid.Columns>
            </data:DataGrid>
    </Grid>
</UserControl>

然后是typeof(StringLibrary)中的资源

如您所见,显示的名称等于资源文件中的ResourceKey。大约2-3周前,当我在开发和测试XML时,我第一次提出了注释。但是像AutoMapper这样的东西可以将你的对象映射到像这样的POCO,或者你甚至可以使用WCF RIA服务,你可以有类似的注释。WCF RIA实际上是使用它们的人,但我只是以不同的方式使用它们

要做到这一点,可能有一些技巧。但是当你这样做的时候,你真的得到了一个简单的解决方案


我希望我已经提供了足够的信息。

不起作用了
非常具体…@H.B.如果迭代#2能更好地解决问题,请告诉我更多的信息,但您不认为扩展有问题吗?发布代码如何?请发布或正确回答并接受它,或者删除问题,如果你认为它没有帮助,这可能是非常有可能的。我实际上认为它很有帮助,这就是为什么我没有删除它,也不会删除它。将把它留到另一天,看看是否有其他人对涉及DataGrid数据上下文转发的答案有不同的看法;或者,如果不是,就把它作为一个可接受的答案。谢谢你的意见
不起作用
非常具体…@H.B.如果迭代#2能更好地解决问题,请告诉我这是更多的信息,但您不认为扩展有问题吗?发布代码如何?请发布或正确回答并接受它,或者删除问题,如果你认为它没有帮助,这可能是非常有可能的。我实际上认为它很有帮助,这就是为什么我没有删除它,也不会删除它。将把它留到另一天,看看是否有其他人对涉及DataGrid数据上下文转发的答案有不同的看法;或者,如果不是,就把它作为一个可接受的答案。谢谢你的意见!这绝对是我以前没见过的,尽管我不确定我是否能理解。我可以看看您的VM代码(假设您使用MVVM)和XAML是什么样子的吗?Cheers@Berryl我已经添加了您要求的信息。如果你想知道更多,请告诉我。如果你想的话,我甚至可以给你这个项目,因为它只是一个学习项目,不管怎样,用PRISM和MVVM尝试不同的东西。这肯定是我以前没见过的项目,尽管我不确定我是否得到了它。我可以看看您的VM代码(假设您使用MVVM)和XAML是什么样子的吗?Cheers@Berryl我已经添加了您要求的信息。如果你想知道更多,请告诉我。如果你愿意的话,我甚至可以给你这个项目,因为它只是一个学习项目,不管怎样,用PRISM和MVVM尝试不同的东西。