本文介绍了Silverlight - ScrollViewer中的DataGrid,Column.Width =“*”使datagrid采取几个屏幕宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我进行以下设置时,最后一列的宽度为*会导致数据网格创建巨大的水平滚动条(将网格扩展到屏幕的多个宽度)。我不太确定为什么这是,但我真的需要一种方法来避免这种情况。我不想用*长度模拟列。



编辑:显然我不是唯一注意到这一点的人。



Xaml:

 < ScrollViewer Padding =0Horizo​​ntalScrollBarVisibility =AutoVerticalScrollBarVisibility =Auto> 
< sdk:DataGrid AutoGenerateColumns =Falsex:Name =dg/>
< / ScrollViewer>

代码:

 code> private void UserControl_Loaded(object sender,RoutedEventArgs e)
{
dg.Columns.Add(new DataGridTextColumn {Binding = new Binding(A),Header =A}) ;
dg.Columns.Add(new DataGridTextColumn {Binding = new Binding(B),Header =B});
dg.Columns.Add(new DataGridTextColumn {Binding = new Binding(C),Header =C});
dg.Columns [2] .Width = new DataGridLength(1,DataGridLengthUnitType.Star);
dg.ItemsSource = new []
{
new I {A =SAF,B =SAF,C =SAF},
new I {A =SAF,B =SAF,C =SAF},
new I {A =SAF,B =SAF,C =SAF}
};
}

public class I
{
public string A {get;组; }
public string B {get;组; }
public string C {get;组; }
}


解决方案

p>

dg.Columns [2] .Width = new DataGridLength(1,DataGridLengthUnitType.Star);



问题应该走开。我可以问你为什么要最后一列占据其余的空间?该列实际上知道要正确调整自己的大小以适应其单元格大小和列标题大小。



此外,如果不指定列的宽度,则不会真的需要一个滚动浏览器滚动并查看所有的列。 datagrid内置了一个scrollviewer,当屏幕上有列时,水平滚动条将会出现。



我希望这有帮助。 :)


When I have the following setup, the last column having a width of * causes the datagrid to create huge horizontal scrollbars (extends grid to several widths of the screen). I'm not really sure why this is, but I really need a way to avoid that. I don't want to have to "simulate" column's with * lengths.

edit: Apparently I'm not the only one who noticed this.http://connect.microsoft.com/VisualStudio/feedback/details/559644/silverlight-4-datagrid-star-column-width

Xaml:

<ScrollViewer Padding="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"  >
    <sdk:DataGrid AutoGenerateColumns="False" x:Name="dg"/>
</ScrollViewer>

Code:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        dg.Columns.Add(new DataGridTextColumn { Binding = new Binding("A"), Header = "A" });
        dg.Columns.Add(new DataGridTextColumn { Binding = new Binding("B"), Header = "B" });
        dg.Columns.Add(new DataGridTextColumn { Binding = new Binding("C"), Header = "C" });
        dg.Columns[2].Width = new DataGridLength(1, DataGridLengthUnitType.Star);
        dg.ItemsSource = new[] 
        {
            new I { A = "SAF", B = "SAF", C = "SAF" },
            new I { A = "SAF", B = "SAF", C = "SAF" },
            new I { A = "SAF", B = "SAF", C = "SAF" }
        };
    }

    public class I
    {
        public string A { get; set; }
        public string B { get; set; }
        public string C { get; set; }
    }
解决方案

If you remove

dg.Columns[2].Width = new DataGridLength(1, DataGridLengthUnitType.Star);

the problem should go away. Can I ask why you want the last column to take the rest of the space? The column actually knows to resize itself properly to fit its cell size and column header size.

Also, if you don't specify the column's width, then you don't really need a scrollviewer to scroll and see all the columns. The datagrid has a scrollviewer built in and when there are columns out of the screen the horizontal scrollbar will apear.

I hope this helps. :)

这篇关于Silverlight - ScrollViewer中的DataGrid,Column.Width =“*”使datagrid采取几个屏幕宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 23:04