子控件的宽度应与父容器的宽度匹配 [英] Width of child control should match width of parent container

查看:89
本文介绍了子控件的宽度应与父容器的宽度匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF的新手。我经常发现自己很挣扎,要获得一堆子控件的组合宽度以匹配给定的父容器。如下所示:

 < ListBox x:Name = BrugereListBox Grid.Column = 0 Grid.Row = 3 DataContext = {DynamicResource Brugere} 
ItemsSource = {Binding}
PreviewMouseLeftButtonDown = BrugereListBox_MouseLeftButtonDown
PreviewMouseMove = BrugereListBox_PreviewMouseMove
>
< ListBox.ItemTemplate>
< DataTemplate>
< Border BorderBrush = Black BorderThickness = 1 Margin = 2>
< StackPanel Orientation = Vertical IsEnabled = True
PreviewMouseLeftButtonDown = StackPanel_PreviewMouseLeftButtonDown>
< StackPanel Orientation = Horizo​​ntal>
< Label> Navn< / Label>
< TextBox Text = {Binding Name}>< / TextBox>
< / StackPanel>
< StackPanel Orientation = Horizo​​ntal>
< Label>密码< / Label>
< TextBox Text = {Binding Password}>< / TextBox>
< / StackPanel>
< StackPanel Orientation = Horizo​​ntal>
< Label> Email< / Label>
< TextBox Text = {Binding Email}>< / TextBox>
< / StackPanel>
< / StackPanel>
< / Border>
< / DataTemplate>
< /ListBox.ItemTemplate>
< / ListBox>

在这种情况下,我有兴趣获取堆栈面板子级的宽度:

 < StackPanel Orientation = Horizo​​ntal> 
< Label> Navn< / Label>
< TextBox Text = {Binding Name}>< / TextBox>
< / StackPanel>

匹配



<$ p $的宽度p> < ListBox x:Name = BrugereListBox

任何通用解决方案

解决方案

原来是对以下内容的欺骗:



wpf边框控件可跨越listboxItem的宽度



以及给出的答案:



ListBoxItems本身不会占用ListBox整个宽度的
。将
Horizo​​ntalContentAlignment = Stretch
属性添加到列表框,看看
是否将单个项目拉伸到
填充宽度。


因此更改为:

 < ListBox x:Name = BrugereListBox  Grid.Column = 0 Grid.Row = 3 DataContext = {DynamicResource Brugere} 
ItemsSource = {Binding}
PreviewMouseLeftButtonDown = BrugereListBox_MouseLeftButtonDown
PreviewMouseMove = BrugereListBox_PreviewMouseMove
Horizo​​ntalContentAlignment = Stretch
>

但正如Kent所说,在我看来,使用网格会更好。


I'm pretty new to WPF. I often find my self struggling with getting a bunch of child controls combined width to match a given parent container.. As in the following:

<ListBox x:Name="BrugereListBox" Grid.Column="0" Grid.Row="3" DataContext="{DynamicResource Brugere}" 
             ItemsSource="{Binding}" 
             PreviewMouseLeftButtonDown="BrugereListBox_MouseLeftButtonDown"
             PreviewMouseMove="BrugereListBox_PreviewMouseMove"
             >
        <ListBox.ItemTemplate  >
            <DataTemplate >
                <Border BorderBrush="Black" BorderThickness="1" Margin="2">
                    <StackPanel Orientation="Vertical" IsEnabled="True" 
                                PreviewMouseLeftButtonDown="StackPanel_PreviewMouseLeftButtonDown">
                        <StackPanel Orientation="Horizontal" >
                            <Label>Navn</Label>
                            <TextBox Text="{Binding Name}"></TextBox>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <Label>Password</Label>
                            <TextBox Text="{Binding Password}"></TextBox>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <Label>Email</Label>
                            <TextBox Text="{Binding Email}"></TextBox>
                        </StackPanel>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

In this case I'm interested in getting the width of the children of the stackpanel:

<StackPanel Orientation="Horizontal" >
                        <Label>Navn</Label>
                        <TextBox Text="{Binding Name}"></TextBox>
                    </StackPanel>

to match the width of

<ListBox x:Name="BrugereListBox"

Any generic solution to scenarios like this?

解决方案

turns out to be a dupe of:

wpf border control to span the width of listboxItem

and the answer given there:

This is probably more to do with the ListBoxItems themselves not taking up the full width of the ListBox. Add the HorizontalContentAlignment="Stretch" attribute to your ListBox and see if it stretches the individual items to fill the width.

so change to:

<ListBox x:Name="BrugereListBox" Grid.Column="0" Grid.Row="3" DataContext="{DynamicResource Brugere}" 
             ItemsSource="{Binding}" 
             PreviewMouseLeftButtonDown="BrugereListBox_MouseLeftButtonDown"
             PreviewMouseMove="BrugereListBox_PreviewMouseMove"
             HorizontalContentAlignment="Stretch"
             >

but as Kent says, using a Grid would be better in my opinion.

这篇关于子控件的宽度应与父容器的宽度匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆