我们正在本地化我们的应用,并允许用户在应用运行时动态更改语言。
除了允许他们更改语言的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>
这是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!
}
}
以下是前后截图,展示了选择和项目如何变化。
如何让ComboBox更改标题?感谢您对此感兴趣。
[UPDATE]
基于@Dev Dua的建议,我添加了PickerFlyoutBase.Title =&#34; {Binding Title,Mode = OneWay}&#34;到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"); } }
ComboBox绑定到PickerFlyoutBase.Title属性时出现问题。在XAML中使用字符串常量可以按预期工作。
答案 0 :(得分: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>