子控件的宽度应与父容器的宽度匹配

发布于 2024-08-18 08:02:48 字数 2048 浏览 4 评论 0原文

我对 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="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>

在这种情况下,我感兴趣的是获取 stackpanel 的子控件的宽度:

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

以匹配宽度 ?

<ListBox x:Name="BrugereListBox"

像这样的场景有通用的解决方案吗

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞花火° 2024-08-25 08:02:48

事实证明是一个骗局:

wpf 边框控件跨越listboxItem 的宽度

以及那里给出的答案:

这可能更多地与
ListBoxItems 本身不占用
列表框的整个宽度。添加
Horizo​​ntalContentAlignment="拉伸"
属性到您的列表框并查看是否
它将各个项目拉伸到
填充宽度。

所以改为:

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

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

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.

难得心□动 2024-08-25 08:02:48

使用正确的面板,一切都会好起来的。 StackPanel 根据其方向及其子项自行决定其宽度/高度。使用Grid,它只会占用给定的空间 - 不多也不少。

Use the right panel and you'll be fine. StackPanel makes its own decisions about its width/height depending on its orientation and its children. Use a Grid and it will just take up the space it is given - no more, no less.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文