Silverlight组合框显示的项数刷新 [英] Silverlight combobox number of items shown refresh
问题描述
我在SL页面上有两个组合框。当组合1更新时,将调用服务并填充组合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.
如果我重新载入并反向执行这些步骤,调用和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
代码遵循MV-VM模式。当页面加载时, Group1
填充第一个组合框,并且不选择任何内容。
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}" />
在viewmodel中,在 Group1Selection
property,我有类似
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执行我的服务调用async, ,并将该数据放入 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.
在MV-VM模式之后,代码中没有代码。
Following the M-V-VM pattern, there is no code in the code-behind.
推荐答案
您可以通过执行以下操作来解决这个问题:
You can fix this by doing the following:
-
继承ComboBox
Inherit from the ComboBox
public class MyComboBox:ComboBox
public class MyComboBox : ComboBox
获取对OnApplyTemplate()方法中ComboBox的Popup部分的引用
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屋!