There are various posts on the web about this issue whereby the ComboBox only changes its dropdown height to fit the items on its very first dropdown. Any changes to the items shown doesn't cause the dropdown to resize. The various workarounds I have found don't seem to work for me, so I was wondering if anyone had found a way to solve this.
网上有关于此问题的各种帖子,其中ComboBox仅更改其下拉高度以适合其第一个下拉列表中的项目。对所显示项目的任何更改都不会导致下拉列表调整大小。我发现的各种变通方法对我来说似乎不起作用,所以我想知道是否有人找到了解决这个问题的方法。
My current option is to use MinHeight and set it to a reasonable size (if I set MinHeight and MaxHeight, I get the same issue as if I set Height - no scrollbar when the items go outside the bounds.
我当前的选择是使用MinHeight并将其设置为合理的大小(如果我设置MinHeight和MaxHeight,我得到的问题就像我设置高度一样 - 当项目超出界限时没有滚动条。
I've tried changing the container for the items, but it seems they all have this issue. Any ideas?
我已经尝试更改项目的容器,但似乎他们都有这个问题。有任何想法吗?
Note that I have also tried programmatically recreating the combo whenever I change the ItemsSource as indicated on several forums, but I can't get this to work without crashing.
请注意,每当我更改几个论坛上指示的ItemsSource时,我也尝试以编程方式重新创建组合,但我无法在没有崩溃的情况下使其工作。
2 个解决方案
#1
The 'add and remove' method works for me. Here's what I do (in case you're doing something slightly different, or anyone else wants to try this method):
'添加和删除'方法适合我。这就是我做的事情(如果你做的事略有不同,或者其他人想尝试这种方法):
- Read and store locally all the properties you're interested in
- Remove the combo-box from the visual tree
- Set the variable to null
- Create a new combo box
- Restore the properties you stored above
- Take away the number you first thought of
- Add it back into the visual tree
在本地读取并存储您感兴趣的所有属性
从可视树中删除组合框
将变量设置为null
创建一个新的组合框
恢复上面存储的属性
拿走你最初想到的数字
将其添加回可视树中
For example:
string lName = lComboBox.Name;
DataTemplate lTemplate = lComboBox.ItemTemplate;
Thickness lMargin = lComboBox.Margin;
// Other properties
LayoutParent.Children.Remove(lComboBox);
lComboBox= null;
lComboBox= new ComboBox(){
Name = lName,
ItemTemplate = lTemplate,
Margin = lMargin,
ItemsSource = lList // Your datasource
};
LayoutParent.Children.Add(lComboBox);
#2
I found a great solution thanks to this answer. It works great and isn't quite as hacky as other suggestions (effective as they are).
由于这个答案我找到了一个很好的解决方案。它工作得很好,并不像其他建议那样hacky(有效)。
Obviously, community wiki-ing this post as it isn't my answer. Please go vote for the real answer, markti deserves double rep for this one.
显然,社区维基这篇文章并不是我的答案。请投票给出真正的答案,Markti应该得到双重代表。
#1
The 'add and remove' method works for me. Here's what I do (in case you're doing something slightly different, or anyone else wants to try this method):
'添加和删除'方法适合我。这就是我做的事情(如果你做的事略有不同,或者其他人想尝试这种方法):
- Read and store locally all the properties you're interested in
- Remove the combo-box from the visual tree
- Set the variable to null
- Create a new combo box
- Restore the properties you stored above
- Take away the number you first thought of
- Add it back into the visual tree
在本地读取并存储您感兴趣的所有属性
从可视树中删除组合框
将变量设置为null
创建一个新的组合框
恢复上面存储的属性
拿走你最初想到的数字
将其添加回可视树中
For example:
string lName = lComboBox.Name;
DataTemplate lTemplate = lComboBox.ItemTemplate;
Thickness lMargin = lComboBox.Margin;
// Other properties
LayoutParent.Children.Remove(lComboBox);
lComboBox= null;
lComboBox= new ComboBox(){
Name = lName,
ItemTemplate = lTemplate,
Margin = lMargin,
ItemsSource = lList // Your datasource
};
LayoutParent.Children.Add(lComboBox);
#2
I found a great solution thanks to this answer. It works great and isn't quite as hacky as other suggestions (effective as they are).
由于这个答案我找到了一个很好的解决方案。它工作得很好,并不像其他建议那样hacky(有效)。
Obviously, community wiki-ing this post as it isn't my answer. Please go vote for the real answer, markti deserves double rep for this one.
显然,社区维基这篇文章并不是我的答案。请投票给出真正的答案,Markti应该得到双重代表。