【问题标题】:DataGridColumn Header content WPFDataGridColumn 标题内容 WPF
【发布时间】:2012-02-21 18:00:23
【问题描述】:

我想本地化一个演示文稿,但我正在为这个演示文稿绞尽脑汁。我正在使用标记扩展,所以不要这样:

<DataGridTextColumn Header="Number" Binding="{Binding Number}" Width="60" CellStyle="{StaticResource NumberStyle}" />

我想要这个:

<DataGridTextColumn Header="{Resx Key=Header_Number}" Binding="{Binding Number}" Width="60" CellStyle="{StaticResource NumberStyle}" />

经过充分测试的标记只会返回当前文化的正确文本。但它不起作用。我假设我需要 HeaderStyle 或 HeaderTemplate 但是...

解决方法是什么?

干杯,
浆果

编辑

不工作我的意思是它在英文中没有返回文本“Number”,而是返回一个默认值(即“#Header_Number)。

正在工作,我的意思是

    <Label FontWeight="Bold" Grid.Row="0" Grid.Column="0" Content="{Resx Key=Label_Amount}"/>

用英文返回“Amount”。

最终编辑

我的错,这实际上更多是因为 WPF 数据网格列不继承其父级的 DataContext。

标记扩展有一个 ResxName 属性,我喜欢为整个窗口设置一次:

    resx:ResxProperty.Name="NMoneys.Presentation.Resources.AllocatorView"
    Title="{Resx Key=Window_Title}" 

但是由于数据网格中的标题不是可视化树的一部分(尽管看起来它们确实应该是!),所以我必须再次专门输入 Resx 的名称,如

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

我之前遇到过这种情况,并看到了一些转发 DC 的技术,但在这种情况下,它不值得费心。

干杯,
浆果

【问题讨论】:

  • isn't working 非常具体...
  • @H.B.让我知道第 2 次迭代是否能更好地聚焦问题
  • 这是更多信息,但您不认为扩展程序有问题吗?发布它的代码怎么样?
  • 如果您认为它没有帮助,请发布或正确回答并接受它或删除问题,这很有可能。
  • 我实际上认为它很有帮助,这就是为什么我没有删除它,也不会。将其开放一天,看看是否其他人对涉及 DataGrid 数据上下文转发的答案有不同的看法;如果不是,则将其设为可接受的答案。感谢您的意见!

标签: wpf silverlight datagrid datatemplate wpfdatagrid


【解决方案1】:

我假设您有代表您的实体的模型。我所做的是使用数据注释。这是示例。编辑:提供带有屏幕截图的 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;}
    }
}

查看

<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 这样的东西可以将你的对象映射到这些 pocos,或者你甚至可以使用 WCF RIA 服务,你可以有类似的注释。 WCF RIA 实际上是使用它们的人,但我只是以不同的方式使用它们。

这里和那里可能有一些技巧可以实现这一点。但是当你这样做时,你真的得到了一个简单的解决方案。

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

【讨论】:

  • 这绝对是我以前从未见过的,虽然我不确定我是否明白。我可以看看您的 VM 代码(假设您使用 MVVM)和 XAML 的样子吗?干杯
  • @Berryl 我已经添加了您要求的信息。如果您想了解更多信息,请告诉我。如果你愿意,我什至可以给你这个项目,因为它只是一个学习项目,用 PRISM 和 MVVM 尝试不同的东西。
猜你喜欢
  • 1970-01-01
  • 2017-02-07
  • 2014-10-19
  • 2014-06-30
  • 1970-01-01
  • 2010-11-09
  • 2017-03-29
  • 1970-01-01
  • 2013-01-25
相关资源
最近更新 更多