作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在 WPF 中是否可以设置最大下拉项数而不是最大下拉高度?谢谢!-凯文
最佳答案
只有当您的所有元素都具有相同的高度时,这个问题才有意义。否则,当您上下滚动 ComboBox 以查看项目列表的不同部分时,您的 ComboBox 会随着滚动而变大或变小。
如果您的所有项目都是相同的高度,使用附加属性很容易做到这一点:
public class ComboBoxHelper : DependencyObject
{
public static int GetMaxDropDownItems(DependencyObject obj) { return (int)obj.GetValue(MaxDropDownItemsProperty); }
public static void SetMaxDropDownItems(DependencyObject obj, int value) { obj.SetValue(MaxDropDownItemsProperty, value); }
public static readonly DependencyProperty MaxDropDownItemsProperty = DependencyProperty.RegisterAttached("MaxDropDownItems", typeof(int), typeof(ComboBoxHelper), new PropertyMetadata
{
PropertyChangedCallback = (obj, e) =>
{
var box = (ComboBox)obj;
box.DropDownOpened += UpdateHeight;
if(box.IsDropDownOpen) UpdateHeight(box, null);
}
});
private static void UpdateHeight(object sender, EventArgs e)
{
var box = (ComboBox)sender;
box.Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =>
{
var container = box.ItemContainerGenerator.ContainerFromIndex(0) as UIElement;
if(container!=null && container.RenderSize.Height>0)
box.MaxDropDownHeight = container.RenderSize.Height * GetMaxDropDownItems(box);
}));
}
}
使用这个属性你可以写:
<ComboBox ...
my:ComboBoxHelper.MaxDropDownItems="8" />
关于c# - WPF 组合框 MaxDropDownItems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3018267/
我想知道如何限制在 DataGridViewComboBoxColumn 中显示的项目数。在简单的 ComboBox 中,我们可以这样做: comboBox1.IntegralHeight = fal
在 WPF 中是否可以设置最大下拉项数而不是最大下拉高度?谢谢!-凯文 最佳答案 只有当您的所有元素都具有相同的高度时,这个问题才有意义。否则,当您上下滚动 ComboBox 以查看项目列表的不同部分
我是一名优秀的程序员,十分优秀!