为什么此WPF组合框未显示所选值? [英] Why is this WPF ComboBox not showing the selected value?

查看:77
本文介绍了为什么此WPF组合框未显示所选值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<CombobBox x:Name="cbo" 
           Style="{StaticResource ComboStyle1}"
           DisplayMemberPath="NAME"
           SelectedItem="{Binding Path=NAME}"
           SelectedIndex="1">
  <ComboBox.ItemTemplate>
    <DataTemplate>
      <Grid>
        <TextBlock Text="{Binding Path=NAME}"/>
      </Grid>
    </DataTemplate>
  </ComboBox.ItemTemplate>
</ComboBox>

窗口中 OnLoaded 事件,我编写了代码来设置 ItemsSource

In the Window OnLoaded event, I wrote the code to set the ItemsSource:

cbo.ItemsSource = ser.GetCity().DefaultView;

在加载窗口时,我可以看到最初的第一个项目正在加载,但同时清除显示的项目。我处于这种情况下,我们将不胜感激。

While loading the window I can see that the initially the the first item is loading but at the same time it clears the displayed item. I am stuck in this scenario and any help is appreciated.

问候

Kishore

推荐答案

快速答案:从代码隐藏中设置 SelectedIndex = 1

QUICK ANSWER : Set SelectedIndex = 1 from code-behind.

首先执行XAML中的代码( InitializeComponent()方法),该方法将设置 SelectedIndex = 1 ,但 ItemsSource 尚未指定!因此 SelectedIndex 不会影响! (记住,您不能在之前指定 ItemsSource InitializeComponent()

It seems that the code in XAML executes first (the InitializeComponent() method), which sets SelectedIndex = 1, but ItemsSource is not specified yet! So SelectedIndex won't affect! (And remember, you cannot specify ItemsSource before InitializeComponent())

因此,您必须在设置 ItemsSource 之后手动设置 SelectedIndex = 1

So you have to manually set SelectedIndex = 1 after setting ItemsSource.

您应该这样做:

XAML

            <ComboBox x:Name="cbo"
                      Style="{StaticResource ComboStyle1}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <TextBlock Text="{Binding Path=NAME}"/>
                        </Grid>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

代码

     cbo.ItemsSource = ser.GetCity().DefaultView;
     cbo.SelectedIndex = 1;






或者这个:


Or this:

XAML

            <ComboBox x:Name="cbo" Initialized="cbo_Initialized"
                      Style="{StaticResource ComboStyle1}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <TextBlock Text="{Binding Path=NAME}"/>
                        </Grid>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

代码

    private void cbo_Initialized(object sender, EventArgs e)
    {
        cbo.SelectedIndex = 1;
    }






也请注意,我已删除 DisplayMemberPath = NAME ,因为您无法同时设置 DisplayMemberPath ItemTemplate 同时。而且,使用 SelectedItem SelectedIndex 而不是两者都使用。


Also note that i've removed DisplayMemberPath="NAME" because you cannot set both DisplayMemberPath and ItemTemplate at the same time. And also, use either SelectedItem or SelectedIndex, not both.

这篇关于为什么此WPF组合框未显示所选值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆