如何调整根据窗口大小在WPF一定的控制? [英] How to resize a certain control based on Window size in WPF?
问题描述
我有一个的ListView
控制,我想调整同步的最后一列有规模的窗口
。因此,如果窗口
的宽度增加了100个单位,我想列的宽度也由100增加。
我应该使用在窗口中的调整
事件并使用一个神奇的数字来手动调整列标题,有点像:
columnHeader.Width = windowSize.X - 400;
我不能充分信贷这个答案,因为我得到了它<一个href="http://social.msdn.microsoft.com/forums/en-US/wpf/thread/83bd7ab9-3407-461f-a0bc-69e04870075c/"相对=nofollow>这里
但基本上是这种想法:
替换视图在一个GridView ListView控件。然后,您可以指定任何宽度你想要的第一列。在这种情况下100,50,50。 然后,我们添加在最后一列,即作为输入父ListView控件绑定。但是,我们允许一个转换器做肮脏的工作,为我们的。
&LT; ListView控件&GT;
&LT; ListView.View&GT;
&LT; GridView控件&GT;
&LT; GridViewColumn标题=标题DisplayMemberBinding ={结合}WIDTH =100/&GT;
&LT; GridViewColumn标题=标题DisplayMemberBinding ={结合}WIDTH =50/&GT;
&LT; GridViewColumn标题=标题DisplayMemberBinding ={结合}WIDTH =50/&GT;
&LT; GridViewColumn标题=4DisplayMemberBinding ={结合}&GT;
&LT; GridViewColumn.Width&GT;
&LT; MultiBinding转换器={的StaticResource starWidthConverter}&GT;
&LT;绑定路径=ActualWidth的的RelativeSource ={的RelativeSource AncestorType =的ListView}/&GT;
&LT;绑定的RelativeSource ={的RelativeSource模式= FindAncestor,AncestorType =的ListView}/&GT;
&LT; / MultiBinding&GT;
&LT; /GridViewColumn.Width>
&LT; / GridViewColumn&GT;
&LT; / GridView的&GT;
&LT; /ListView.View>
&LT; ListViewItem的&GT; ITEM1&LT; / ListViewItem的&GT;
&LT; ListViewItem的&GT;项目2&LT; / ListViewItem的&GT;
&LT; ListViewItem的&GT;项目3&LT; / ListViewItem的&GT;
&LT; ListViewItem的&GT; ITEM4&LT; / ListViewItem的&GT;
&LT; / ListView控件&GT;
所以,在转换器转换功能,我们这样做:
的ListView列表视图=值[1]的ListView;
双幅= listview.ActualWidth;
GridView控件GV = listview.View为GridView控件;
的for(int i = 0; I&LT; gv.Columns.Count-1;我++)
{
如果(!Double.IsNaN(gv.Columns [I] .WIDTH))
宽度 - = gv.Columns [I] .WIDTH;
}
返回宽度 - 5; //这是为了照顾保证金/填充
这抓住ListView的宽度,并计算新的大小。
I have a ListView
control where I want to resize the last column in sync with the size of the Window
. So if the Window
's width increases by 100 units, I want the columns' width to also increase by 100.
Should I be using the Resize
event on the Window and use a magic number to resize the column header manually, sort of like?:
columnHeader.Width = windowSize.X - 400;
I can't take full credit for this answer, because I got it here
But basically this is the idea:
Replace the "View" in the listView with a GridView. Then, you can specify whatever width you want for the first columns. In this case 100, 50, 50. Then we add a binding on the final column, that takes as input the parent ListView control. But we are allowing a converter to do the dirty work for us.
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Header="Title" DisplayMemberBinding="{Binding}" Width="100"/>
<GridViewColumn Header="Title" DisplayMemberBinding="{Binding}" Width="50"/>
<GridViewColumn Header="Title" DisplayMemberBinding="{Binding}" Width="50"/>
<GridViewColumn Header="4" DisplayMemberBinding="{Binding}">
<GridViewColumn.Width>
<MultiBinding Converter="{StaticResource starWidthConverter}">
<Binding Path="ActualWidth" RelativeSource="{RelativeSource AncestorType=ListView}"/>
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ListView}"/>
</MultiBinding>
</GridViewColumn.Width>
</GridViewColumn>
</GridView>
</ListView.View>
<ListViewItem>item1</ListViewItem>
<ListViewItem>item2</ListViewItem>
<ListViewItem>item3</ListViewItem>
<ListViewItem>item4</ListViewItem>
</ListView>
So, in the converter 'Convert' function we do this:
ListView listview = value[1] as ListView;
double width = listview.ActualWidth;
GridView gv = listview.View as GridView;
for(int i = 0;i < gv.Columns.Count-1;i++)
{
if(!Double.IsNaN(gv.Columns[i].Width))
width -= gv.Columns[i].Width;
}
return width - 5;// this is to take care of margin/padding
Which grabs the listview width, and calculates the new size.
这篇关于如何调整根据窗口大小在WPF一定的控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!