Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何根据WPF中列表框的高度显示项目?_C#_Wpf_Listbox_Horizontal Scrolling - Fatal编程技术网

C# 如何根据WPF中列表框的高度显示项目?

C# 如何根据WPF中列表框的高度显示项目?,c#,wpf,listbox,horizontal-scrolling,C#,Wpf,Listbox,Horizontal Scrolling,我正在将大约100个项目绑定到ListBox。项目垂直显示,如下所示: 但我希望它水平显示。在谷歌上搜索后,我尝试了以下代码: 现在它是这样显示的。但是我想在一列中显示4行。有人能帮我吗 提前感谢。听起来像是UniformGrid的完美用例: 预期结果: 可以使用稍微修改的UniformGrid实现垂直方向(取自: 公共类UniformGridWithOrientation:UniformGrid { 公共静态只读DependencyProperty方向属性= Dependen

我正在将大约100个项目绑定到ListBox。项目垂直显示,如下所示:


但我希望它水平显示。在谷歌上搜索后,我尝试了以下代码:


现在它是这样显示的。但是我想在一列中显示4行。有人能帮我吗


提前感谢。

听起来像是
UniformGrid的完美用例


预期结果:

可以使用稍微修改的UniformGrid实现垂直方向(取自:

公共类UniformGridWithOrientation:UniformGrid
{
公共静态只读DependencyProperty方向属性=
DependencyProperty.Register(“方向”)、typeof(System.Windows.Controls.Orientation)、typeof(UniformGridWithOrientation),
新框架属性元数据(
System.Windows.Controls.Orientation.Vertical,
FrameworkPropertyMetadataOptions.AffectsMeasure),
新的ValidateValueCallback(UniformGridWithOrientation.isValidationation));
内部静态布尔值有效性(对象o)
{  
System.Windows.Controls.Orientation=(System.Windows.Controls.Orientation)o;
if(方向!=System.Windows.Controls.orientation.Horizontal)
{  
返回(方向==System.Windows.Controls.orientation.Vertical);
}  
返回true;
}  
公共系统.Windows.Controls.Orientation
{  
获取{return(System.Windows.Controls.Orientation)GetValue(OrientationProperty);}
set{SetValue(方向属性,值);}
}
专用int_列;
私有整数行;
受保护的覆盖尺寸测量覆盖(尺寸约束)
{  
this.updateComputerdValues();
Size availableSize=新尺寸(constraint.Width/((两倍)this._列),constraint.Height/((两倍)this._行));
双倍宽度=0.0;
双倍高度=0.0;
int num3=0;
int count=base.InternalChildren.count;
而(num3<计数)
{  
UIElement=base.InternalChildren[num3];
元素。度量(可用性大小);
大小desiredSize=元素。desiredSize;
如果(宽度<所需尺寸宽度)
{  
宽度=所需尺寸。宽度;
}  
如果(高度<所需尺寸高度)
{  
高度=所需尺寸。高度;
}  
num3++;
}  
返回新大小(宽度*此。\列,高度*此。\行);
}  
私有void updateComputerdValues()
{  
this.\u columns=this.columns;
this._rows=this.rows;
如果(this.FirstColumn>=this.\u columns)
{  
this.FirstColumn=0;
}  
如果(第一列>0)
抛出新的NotImplementedException(“不支持设置第一列(或第一行)。”;
if((此._行==0)| |(此._列==0))
{  
int num=0;//可见子项
int num2=0;
int count=base.InternalChildren.count;
while(num2<计数)
{  
UIElement=base.InternalChildren[num2];
if(element.Visibility!=Visibility.Collapsed)
{  
num++;
}  
num2++;
}  
如果(num==0)
{  
num=1;
}  
如果(此._行==0)
{  
如果(此._列>0)
{  
this._rows=((num+this.FirstColumn)+(this._columns-1))/this._columns;
}  
其他的
{  
这个._rows=(int)Math.Sqrt((double)num);
if((此._行*此._行)=numX)
{  
finalRect.X+=finalRect.Width;
最终结果Y=0.0;
}  
}  
}  
返回安排大小;
}  
}  
设置
Orientation=“Vertical”
时的结果:


如何知道复选框上的列表框名称。@Anjali您不需要知道,您使用的是ViewModel。但是你当然可以遍历VisualTree直到找到一个列表框(例如)@Anjali我可以问你为什么需要它吗?@ManfredRadlwimmer,我们可以垂直显示它吗?有可能吗?@Anjali有点模棱两可-你到底是怎么想的
public class UniformGridWithOrientation : UniformGrid  
{
    public static readonly DependencyProperty OrientationProperty =  
        DependencyProperty.Register( "Orientation", typeof( System.Windows.Controls.Orientation ), typeof( UniformGridWithOrientation ),  
            new FrameworkPropertyMetadata(   
                System.Windows.Controls.Orientation.Vertical,   
                FrameworkPropertyMetadataOptions.AffectsMeasure ),    
            new ValidateValueCallback( UniformGridWithOrientation.IsValidOrientation ) );  

    internal static bool IsValidOrientation( object o )  
    {  
        System.Windows.Controls.Orientation orientation = (System.Windows.Controls.Orientation)o;  
        if( orientation != System.Windows.Controls.Orientation.Horizontal )  
        {  
            return ( orientation == System.Windows.Controls.Orientation.Vertical );  
        }  
        return true;  
    }  

    public System.Windows.Controls.Orientation Orientation  
    {  
        get { return (System.Windows.Controls.Orientation)GetValue( OrientationProperty ); }  
        set { SetValue( OrientationProperty, value ); }  
    }

    private int _columns;  
    private int _rows;  

    protected override Size MeasureOverride( Size constraint )  
    {  
        this.UpdateComputedValues();  
        Size availableSize = new Size( constraint.Width / ( (double)this._columns ), constraint.Height / ( (double)this._rows ) );  
        double width = 0.0;  
        double height = 0.0;  
        int num3 = 0;  
        int count = base.InternalChildren.Count;  
        while( num3 < count )  
        {  
            UIElement element = base.InternalChildren[ num3 ];  
            element.Measure( availableSize );  
            Size desiredSize = element.DesiredSize;  
            if( width < desiredSize.Width )  
            {  
                width = desiredSize.Width;  
            }  
            if( height < desiredSize.Height )  
            {  
                height = desiredSize.Height;  
            }  
            num3++;  
        }  
        return new Size( width * this._columns, height * this._rows );  
    }  

    private void UpdateComputedValues()  
    {  
        this._columns = this.Columns;  
        this._rows = this.Rows;  
        if( this.FirstColumn >= this._columns )  
        {  
            this.FirstColumn = 0;  
        }  

        if( FirstColumn > 0 )  
            throw new NotImplementedException( "There is no support for seting the FirstColumn (nor the FirstRow)." );  
        if( ( this._rows == 0 ) || ( this._columns == 0 ) )  
        {  
            int num = 0;    // Visible children  
            int num2 = 0;  
            int count = base.InternalChildren.Count;  
            while( num2 < count )  
            {  
                UIElement element = base.InternalChildren[ num2 ];  
                if( element.Visibility != Visibility.Collapsed )  
                {  
                    num++;  
                }  
                num2++;  
            }  
            if( num == 0 )  
            {  
                num = 1;  
            }  
            if( this._rows == 0 )  
            {  
                if( this._columns > 0 )  
                {  
                    this._rows = ( ( num + this.FirstColumn ) + ( this._columns - 1 ) ) / this._columns;  
                }  
                else 
                {  
                    this._rows = (int)Math.Sqrt( (double)num );  
                    if( ( this._rows * this._rows ) < num )  
                    {  
                        this._rows++;  
                    }  
                    this._columns = this._rows;  
                }  
            }  
            else if( this._columns == 0 )  
            {  
                this._columns = ( num + ( this._rows - 1 ) ) / this._rows;  
            }  
        }  
    }  

    protected override Size ArrangeOverride( Size arrangeSize )  
    {  
        Rect finalRect = new Rect( 0.0, 0.0, arrangeSize.Width / ( (double)this._columns ), arrangeSize.Height / ( (double)this._rows ) );  
        double height = finalRect.Height;  
        double numX = arrangeSize.Height - 1.0;  
        finalRect.X += finalRect.Width * this.FirstColumn;  
        foreach( UIElement element in base.InternalChildren )  
        {  
            element.Arrange( finalRect );  
            if( element.Visibility != Visibility.Collapsed )  
            {  
                finalRect.Y += height;  
                if( finalRect.Y >= numX )  
                {  
                    finalRect.X += finalRect.Width;  
                    finalRect.Y = 0.0;  
                }  
            }  
        }  
        return arrangeSize;  
    }  
}