2012-01-29 66 views
1

我想本地化一个演示文稿,并对此进行了大脑冻结。我使用的是标记扩展,因此而不是这样的:DataGridColumn标题内容WPF

<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,但是...

什么是修复?

干杯,
Berryl

编辑

通过不工作我的意思是没有返回文本“数字”,而在英语,而不是返回默认值(即,“# Header_Number)。

通过的工作,我的意思是

<Label FontWeight="Bold" Grid.Row="0" Grid.Column="0" Content="{Resx Key=Label_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,但在这种情况下,它不是不值得麻烦。

干杯,
Berryl

+0

'不工作'是非常具体的... – 2012-01-29 18:11:01

+0

@ H.B。让我知道迭代#2是否更好地将问题带入焦点 – Berryl 2012-01-29 18:21:23

+0

这是一个更多的信息,但是您不认为扩展有什么问题吗?如何发布其代码? – 2012-01-29 18:59:20

回答

1

我假设你有模特representating您的实体。我所做的是使用数据注释。这里是例子。编辑:提供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> 

The DataGrid in the program!

然后在typeof资源(StringLibrary)

Resource File

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

可能有一些技巧在这里和那里来完成这一点。但是当你这样做时,你真的可以得到一个简单的解

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

+0

这绝对是我以前没见过的,虽然不知道我明白了。我可以看到你的虚拟机代码(假设你使用MVVM)和XAML的外观吗?欢呼 – Berryl 2012-01-30 17:08:30

+0

@Berryl我已经添加了你所要求的信息。让我知道你是否想知道更多。如果你愿意的话,我甚至可以给你这个项目,因为无论如何,它只是一个学习项目,尝试与PRISM和MVVM不同的东西。 – 2012-01-30 17:48:45