combobox selectedItem在UWP中不起作用

发布于 2025-01-30 23:41:07 字数 1404 浏览 1 评论 0原文

我有一个带有某些项目的组合,以这种方式填充Combobox:

var files = Directory.GetFiles(Constants.TranslationsPath);
var items = new ObservableCollection<Translation>();
using var db = new AlAnvarDBContext();
if (files.Count() > 0)
 {
   foreach (var file in files)
   {
     var id = Path.GetFileNameWithoutExtension(file);
     var trans = db.Translations.Where(x => x.Link.Contains(id)).FirstOrDefault();
     if (trans != null)
     {
      items.Add(trans);
     }
   }
   cmbTranslators.ItemsSource = items;
   cmbTranslators.SelectedItem = Settings.DefaultTranslation;
 }

XAML:

<ComboBox x:Name="cmbTranslators">
                <ComboBox.ItemTemplate>
                    <DataTemplate x:DataType="table:Translation">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{x:Bind Name}"/>
                            <TextBlock Text="-"/>
                            <TextBlock Text="{x:Bind Language}"/>
                            <TextBlock Text="-"/>
                            <TextBlock Text="{x:Bind Translator}"/>
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

问题是未选择默认项目(SelectedItem),我需要在ComboBox中选择默认项目。

I have a Combobox with some items, i fill combobox this way:

var files = Directory.GetFiles(Constants.TranslationsPath);
var items = new ObservableCollection<Translation>();
using var db = new AlAnvarDBContext();
if (files.Count() > 0)
 {
   foreach (var file in files)
   {
     var id = Path.GetFileNameWithoutExtension(file);
     var trans = db.Translations.Where(x => x.Link.Contains(id)).FirstOrDefault();
     if (trans != null)
     {
      items.Add(trans);
     }
   }
   cmbTranslators.ItemsSource = items;
   cmbTranslators.SelectedItem = Settings.DefaultTranslation;
 }

and xaml:

<ComboBox x:Name="cmbTranslators">
                <ComboBox.ItemTemplate>
                    <DataTemplate x:DataType="table:Translation">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{x:Bind Name}"/>
                            <TextBlock Text="-"/>
                            <TextBlock Text="{x:Bind Language}"/>
                            <TextBlock Text="-"/>
                            <TextBlock Text="{x:Bind Translator}"/>
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

The problem is that the default item (SelectedItem) is not selected and i need to select default item in combobox.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海未深 2025-02-06 23:41:07

settings.defaultTranslation是否可能不等于项目集合中的任何items,这就是为什么不发生任务的原因?也许您可以将其添加到您的Collection settings.defaultTranslation喜欢第一个元素和SET cmbtranslators.selectedIndex = 0

Is it possible that the Settings.DefaultTranslation is not equal to any of the items in the item collection and that is why the assignment did not occur? Maybe you can add it to your collection Settings.DefaultTranslation like the first element and set cmbTranslators.SelectedIndex = 0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文