【问题标题】:Why is this WPF ComboBox not showing the selected value?为什么这个 WPF ComboBox 不显示选定的值?
【发布时间】:2010-01-22 05:31:29
【问题描述】:
<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>

在WindowOnLoaded事件中,我编写了设置ItemsSource的代码:

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

在加载窗口时,我可以看到最初第一个项目正在加载,但同时它清除了显示的项目。我被困在这种情况下,感谢任何帮助。

问候

基肖尔

【问题讨论】:

    标签: wpf combobox datatemplate selectedvalue


    【解决方案1】:

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

    看来XAML中的代码先执行(InitializeComponent()方法),设置SelectedIndex = 1,但是ItemsSource还没有指定!所以SelectedIndex 不会影响! (记住,你不能指定ItemsSource 之前 InitializeComponent())

    所以你必须在设置ItemsSource之后手动设置SelectedIndex = 1。


    你应该这样做:

    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;
    

    或者这个:

    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,不要同时使用。

    【讨论】:

    • 非常感谢您的 cmets。
    【解决方案2】:

    重置 ItemsSource 会打乱选择。

    另外,您同时设置了 SelectedItem 和 SelectedIndex。您只想设置其中之一;如果两者都设置,则只有一个会生效。

    此外,您的 SelectedItem 声明可能是错误的。 SelectedItem="{Binding NAME}" 将查找等于环境(可能是窗口级别)DataContext 的 NAME 属性值的项。这仅在 ComboBox.ItemsSource 是字符串列表时才有效。由于您的 ItemTemplate 有效,我假设 ComboBox.ItemsSource 实际上是 City 对象的列表。但是您告诉 WPF SelectedItem 应该是一个字符串(一个名称)。此字符串永远不会等于任何 City 对象,因此结果将是无选择。

    所以要解决这个问题,在设置 ItemsSource 之后,根据您的要求和数据模型设置 SelectedItem 或 SelectedIndex:

    cbo.ItemsSource = ser.GetCity().DefaultView;
    cbo.SelectedIndex = 1;
    // or: cbo.SelectedItem = "Wellington";    // if GetCity() returns strings - probably not
    // or: cbo.SelectedItem = City.Wellington; // if GetCity() returns City objects
    

    【讨论】:

    • +1 重置 ItemsSource 会打乱选择。我正在刷新我的 ItemsSource,这弄乱了绑定。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 2014-11-05
    • 2020-10-18
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多