如何调整根据窗口大小在WPF一定的控制?大小、根据、窗口、WPF

2023-09-04 23:02:29 作者:你是我血液里的毒

我有一个的ListView 控制,我想调整同步的最后一列有规模的窗口 。因此,如果窗口的宽度增加了100个单位,我想列的宽度也由100增加。

我应该使用在窗口中的调整事件并使用一个神奇的数字来手动调整列标题,有点像:

  columnHeader.Width = windowSize.X  -  400;
 

解决方案

我不能充分信贷这个答案,因为我得到了它这里

但基本上是这种想法:

替换视图在一个GridView ListView控件。然后,您可以指定任何宽度你想要的第一列。在这种情况下100,50,50。 然后,我们添加在最后一列,即作为输入父ListView控件绑定。但是,我们允许一个转换器做肮脏的工作,为我们的。

 < ListView控件>
    < ListView.View>
      < GridView控件>
        < GridViewColumn标题=标题DisplayMemberBinding ={结合}WIDTH =100/>
        < GridViewColumn标题=标题DisplayMemberBinding ={结合}WIDTH =50/>
        < GridViewColumn标题=标题DisplayMemberBinding ={结合}WIDTH =50/>
        < GridViewColumn标题=4DisplayMemberBinding ={结合}>
        < GridViewColumn.Width>
          < MultiBinding转换器={的StaticResource starWidthConverter}>
            <绑定路径=ActualWidth的的RelativeSource ={的RelativeSource AncestorType =的ListView}/>
            <绑定的RelativeSource ={的RelativeSource模式= FindAncestor,AncestorType =的ListView}/>
          < / MultiBinding>
        < /GridViewColumn.Width>
      < / GridViewColumn>
     < / GridView的>
    < /ListView.View>
    < ListViewItem的> ITEM1< / ListViewItem的>
    < ListViewItem的>项目2< / ListViewItem的>
    < ListViewItem的>项目3< / ListViewItem的>
    < ListViewItem的> ITEM4< / ListViewItem的>
  < / ListView控件>
 

所以,在转换器转换功能,我们这样做:

 的ListView列表视图=值[1]的ListView;
双幅= listview.ActualWidth;
GridView控件GV = listview.View为GridView控件;
的for(int i = 0; I< 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.