Silverlight 组合框显示的项目数刷新 [英] Silverlight combobox number of items shown refresh

查看:18
本文介绍了Silverlight 组合框显示的项目数刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SL 页面上有两个组合框.当 Combo 1 更​​新时,会调用一个服务并填充 Combo 2.

I have two combo boxes on a SL page. When Combo 1 updates, a service is called and populates Combo 2.

在第一次调用时,返回 3 个结果.当组合框展开时,您可以看到所有 3 个选项.

On the first call, 3 results are returned. When the combo box is expanded, you can see all 3 options.

在第二次调用时,返回 4 个结果.当组合框展开时,您可以看到 3 个选项,带有垂直滚动条.

On the second call, 4 results are returned. When the combo box is expanded, you can see 3 options, with a vertical scroll bar.

如果我重新加载并反向执行这些步骤,我会在第一次调用时得到 4 行,在第二次调用时得到 3 行 + 一个空白行.(不,空白不是记录.它不能被选中.)

If I reload and do those steps in reverse, I get 4 rows the first call and 3 rows + a blank row on the second call. (No, the blank is not a record. It cannot be selected.)

下拉列表的大小似乎保持了第一个生成的高度.

It appears that the drop down list size keeps the first generated height.

如何刷新每次服务调用后显示的组合框最大项目数?

How can I refresh the combo box max items shown after each service call?

谢谢!

编辑 #1

代码遵循 M-V-VM 模式.当页面加载时,Group1 填充第一个组合框,并且没有选择任何内容.当用户进行选择时,该选择将绑定到 Group1Selection.

The code follows the M-V-VM pattern. When the page loads, the Group1 populates the first combo box, and nothing is selected. When the user makes a selection, that selection is bound to Group1Selection.

<ComboBox ItemsSource="{Binding Path=Group1}" SelectedItem="{Binding Path=Group1Selection}" />
<ComboBox ItemsSource="{Binding Path=Group2}" SelectedItem="{Binding Path=Group2Selection}" />

在视图模型中,在 Group1Selection 属性的 set 访问器中,我有类似的东西

In the viewmodel, in the set accessor of the Group1Selection property, I have something like

set
{
    if (group1Selection != value)
    {
        group1Selection = value;
        PopulateGroup2();
        OnPropertyChanged("Group1Selection");
    }
}

PopulateGroup2 在何处执行我的服务调用异步、获取数据并将该数据放入 Group2 的公开属性中.

Where PopulateGroup2 performs my service call async, gets the data, and puts that data into the exposed property of Group2.

在正常"条件下,这不是问题,因为大多数选项都有数十种可能的选择.但是,几个 Group1 选项只有 3 或 4 个子选项.如果首先选择其中一个,则该应用程序实例其余部分的 ComboBox 高度分别设置为 3 或 4,而不是最大显示 8 个项目.

Under "normal" conditions, this isn't a problem, since most options have dozens of possible selections. However, a couple of the Group1 choices only have 3 or 4 child choices. If one of those is selected first, then the height of the ComboBox, for the rest of that application instance is set to 3 or 4, respectively, instead of maxing out at 8 shown items.

遵循 M-V-VM 模式,代码隐藏中没有代码.

Following the M-V-VM pattern, there is no code in the code-behind.

推荐答案

这是 Silverlight 2 中 ComboBox 中的一个已知错误.我认为它已在 SL 3 中修复.

This is a known bug in the ComboBox in Silverlight 2. I think its been fixed in SL 3.

您可以通过执行以下操作来解决此问题:

You can fix this by doing the following:

  1. 从组合框继承

  1. Inherit from the ComboBox

公共类 MyComboBox : ComboBox

public class MyComboBox : ComboBox

在 OnApplyTemplate() 方法中获取对 ComboBox 的弹出"部分的引用

Get a reference to the "Popup" part of the ComboBox inside the OnApplyTemplate() method

    Popup thePopup = GetTemplateChild("Popup") as Popup;
    FrameworkElement thePopupContent = thePopup.Child as FrameworkElement;

  • 覆盖 OnItemsChanged 方法

  • Override the OnItemsChanged method

    在重写的 OnItemsChagned 方法内部重置 Height &使用 ClearValue(DP) 方法的 Popup 上的宽度依赖属性.

    Inside the overridden OnItemsChagned method reset the Height & Width dependency properties on the Popup using the ClearValue(DP) method.

            thePopupContent.ClearValue(FrameworkElement.WidthProperty);
            thePopupContent.ClearValue(FrameworkElement.HeightProperty);
    

  • 您可以清除最大和最小高度 &如果您也担心宽度属性.

    You can clear the Max and Min Height & Width properties if you are worried about those too.

    这篇关于Silverlight 组合框显示的项目数刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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