为什么在应用程序文化发生变化时会重新加载ItemSource?

时间:2014-12-18 05:04:14

标签: wpf datagrid localization cultureinfo

我有带有绑定ItemSource的DataGrid Control到员工BindingList。当我改变应用程序的文化然后,为什么它重新加载所有数据???

这是我的DataGrid:

    <DataGrid ItemsSource="{Binding Employees,UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Salary" Binding="{Binding Salary}"/>
        </DataGrid.Columns>
    </DataGrid>
    <Button Content="en-US" Name="btnEn" HorizontalAlignment="Left" Margin="74,279,0,0" VerticalAlignment="Top" Width="75" Click="btnEn_Click"/>
    <Button Content="ja-JP" Name="btnJP" HorizontalAlignment="Left" Margin="219,279,0,0" VerticalAlignment="Top" Width="75" Click="btnJP_Click"/>

员工类:

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Salary { get; set; }
}

BindingList:

    private BindingList<Employee> _employees;

    public BindingList<Employee> Employees
    {
        get 
        {
            if (_employees == null)
                _employees = new BindingList<Employee>();
            return _employees; 
        }
        set { _employees = value; }
    }

文化更改按钮上的代码:

    private void btnEn_Click(object sender, RoutedEventArgs e)
    {
        CultureInfo CustomCulture = new CultureInfo("en-US");
        CustomCulture.DateTimeFormat = new CultureInfo("en-US").DateTimeFormat; // Change DateTimeFormat to make date always in en-US Culture
        Thread.CurrentThread.CurrentCulture = CustomCulture;
        Thread.CurrentThread.CurrentUICulture = CustomCulture;
        this.Language = XmlLanguage.GetLanguage("en-US");
    }

    private void btnJP_Click(object sender, RoutedEventArgs e)
    {
        CultureInfo CustomCulture = new CultureInfo("ja-JP");
        CustomCulture.DateTimeFormat = new CultureInfo("en-US").DateTimeFormat; // Change DateTimeFormat to make date always in en-US Culture
        Thread.CurrentThread.CurrentCulture = CustomCulture;
        Thread.CurrentThread.CurrentUICulture = CustomCulture;
        this.Language = XmlLanguage.GetLanguage("ja-JP");
    }

任何帮助都是适当的。

更新

我该如何阻止这种行为?我不想再次对文化改变重新加载整个数据???

2 个答案:

答案 0 :(得分:1)

更改语言会影响用户界面;更特别是Measure,需要布局更新。这应该是预期的,因为改变文化可能意味着UI需要从右向左阅读,或者不同的文化字符集需要更多或更少的空间来正确显示。

您可以看到this.Language Window直接影响继承FrameworkElement的{​​{1}}。{/ p>

答案 1 :(得分:0)

此问题出现在语言切换,制表符切换以及应用不同主题上。我试着用谷歌搜索这个问题&amp;我找到了一个解决方案。

解决此问题的唯一方法是将固定的 ConverterCulture = en-US(或您的默认文化)提供给绑定。