Gyl*_*son 1 c# combobox localization windows-phone-8.1
我们正在本地化我们的应用程序,并允许用户在应用程序运行时动态更改语言.
除了允许他们更改语言的ComboBox之外,这种方法运行良好.
这是XAML
<ComboBox
x:Name="label_Languages"
x:Uid="label_Languages"
Header="Preferred language"
ItemsSource="{Binding Languages}"
SelectedItem="{Binding LanguageSelected, Mode=TwoWay}"
PickerFlyoutBase.Title="{Binding Title, Mode=OneWay}"
SelectedValuePath="Name"
SelectionChanged="label_Languages_SelectionChanged" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding NativeName}" />
<TextBlock Text="{Binding EnglishName}" FontSize="14" Foreground="{ThemeResource TextBoxForegroundHeaderThemeBrush}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
这是C#
private void label_Languages_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox languages = sender as ComboBox;
SettingsViewModel vm = DataContext as SettingsViewModel;
SettingsViewModel.Language language = label_Languages.SelectedItem as SettingsViewModel.Language;
if (language != null)
{
string locale = language.Name;
App.ChangeAppLanguage(locale);
page_SettingsPage.Language = App.CultureInfo.Name;
page_SettingsPage.FlowDirection = App.FlowDirection;
// Modify the language of each page UI element and render it in the new language.
label_Languages.Header = ResourceStrings.GetString("label_Languages.Header");
// update a bunch of other items that all work perfectly!
}
}
Run Code Online (Sandbox Code Playgroud)
以下是前后屏幕截图,显示了选择和项目如何不变.


如何让ComboBox更改它的标题?感谢您对此感兴趣.
[UPDATE]
根据@Dev Dua的建议,我将PickerFlyoutBase.Title ="{Binding Title,Mode = OneWay}"添加到ComboBox XAML,并将Title属性添加到ViewModel.在ViewModel中更改语言后,Title属性的RaisePropertyChanged将导致稍后检索Title属性.大概是由PickerFlyoutBase.Title绑定.不幸的是,即使Title属性返回了正确的值,ComboBox也会继续显示英文选择项目.
这是ViewModel:
public Language _LanguageSelected = null;
public Language LanguageSelected
{
get { return _LanguageSelected; }
set
{
_LanguageSelected = value;
App.ChangeAppLanguage(_LanguageSelected.Name);
RaisePropertyChanged("LanguageSelected");
RaisePropertyChanged("Title");
}
}
public string Title
{ get { return ResourceStrings.GetString("LanguageChooseAnItem.Title"); } }
Run Code Online (Sandbox Code Playgroud)
ComboBox绑定到PickerFlyoutBase.Title属性时出现问题.在XAML中使用字符串常量按预期工作.
小智 5
使用PickerFlyoutBase.Title属性!将其绑定到要显示的任何文本.
<ComboBox PlaceholderText="Something here" PickerFlyoutBase.Title="Edited">
<ComboBoxItem Content="A"/>
<ComboBoxItem Content="B"/>
<ComboBoxItem Content="C"/>
<ComboBoxItem Content="D"/>
<ComboBoxItem Content="A"/>
<ComboBoxItem Content="A"/>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)