wpf 边框控件跨越 listboxItem 的宽度

发布于 2024-07-11 19:46:47 字数 1717 浏览 5 评论 0原文

我试图在我的 wpf 应用程序中为业务对象定义一个 dataTemplate,该对象的集合绑定到 ListBox。

<UserControl.Resources>
    <DataTemplate x:Key="ResizedItemsDataTemplate" DataType="{x:Type resizer:ResizeMonitorItem}">
              <Border x:Name="bdr" BorderBrush="Blue" 
                                     BorderThickness="1" 
                                     CornerRadius="2" 
                                     Width="auto"
                                     HorizontalAlignment="Stretch"
                                     VerticalAlignment="Stretch">
                    <Grid Margin="2">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="14"></RowDefinition>
                            <RowDefinition Height="14"></RowDefinition>
                        </Grid.RowDefinitions>


                        <TextBlock Grid.Row="0" Text="{Binding SaveAsFileName}"></TextBlock>
                        <TextBlock Grid.Row="1" Text="{Binding ResizedImageFilePath}"></TextBlock>
                    </Grid>
             </Border>
    </DataTemplate>
</UserControl.Resources>
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0">    
    <Border BorderThickness="0,0,0,5" BorderBrush="DarkGray" >
        <ListBox x:Name="ListBoxResizeItems" ItemsSource="{Binding Path=ResizeItems}" BorderThickness="0" ItemTemplate="{DynamicResource ResizedItemsDataTemplate}">
        </ListBox>
    </Border>
</Grid>

如何获得使用 x:Name=bdr 定义的边框以跨越每个列表框项目的整个宽度? 目前,它仅跨越其中的文本块,不需要填充列表框项的整个宽度,并且每个列表框项也有所不同。

Im trying to define a dataTemplate for a business object in my wpf application a collection of which is being bound to a ListBox.

<UserControl.Resources>
    <DataTemplate x:Key="ResizedItemsDataTemplate" DataType="{x:Type resizer:ResizeMonitorItem}">
              <Border x:Name="bdr" BorderBrush="Blue" 
                                     BorderThickness="1" 
                                     CornerRadius="2" 
                                     Width="auto"
                                     HorizontalAlignment="Stretch"
                                     VerticalAlignment="Stretch">
                    <Grid Margin="2">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="14"></RowDefinition>
                            <RowDefinition Height="14"></RowDefinition>
                        </Grid.RowDefinitions>


                        <TextBlock Grid.Row="0" Text="{Binding SaveAsFileName}"></TextBlock>
                        <TextBlock Grid.Row="1" Text="{Binding ResizedImageFilePath}"></TextBlock>
                    </Grid>
             </Border>
    </DataTemplate>
</UserControl.Resources>
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0">    
    <Border BorderThickness="0,0,0,5" BorderBrush="DarkGray" >
        <ListBox x:Name="ListBoxResizeItems" ItemsSource="{Binding Path=ResizeItems}" BorderThickness="0" ItemTemplate="{DynamicResource ResizedItemsDataTemplate}">
        </ListBox>
    </Border>
</Grid>

How can I get the border defined with x:Name=bdr to span the full width of each listbox item? At the moment it only spans the with of the textblocks inside it which dont neccessary fill the full width of the listboxitem and also vary for each listboxitem.

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

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

发布评论

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

评论(3

这可能更多地与 ListBoxItems 本身不占据 ListBox 的整个宽度有关。 将 Horizo​​ntalContentAlignment="Stretch" 属性添加到 ListBox 中,看看它是否拉伸各个项目以填充宽度。

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.

指尖微凉心微凉 2024-07-18 19:46:47

与我尝试的解决方案相比,Horizo​​ntalContentAlignment 是一个很好、干净的解决方案。 谢谢!

这是几乎有效的方法,但有时会使对话框动画本身越来越宽:

Width="{Binding ActualWidth, 
        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}"

HorizontalContentAlignment is a nice, clean solution compared to what I was trying. Thanks!

Here's what ALMOST worked, but sometimes made a dialog box animated itself wider and wider forever:

Width="{Binding ActualWidth, 
        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}"
未蓝澄海的烟 2024-07-18 19:46:47

解决了。 诀窍是在列表框中设置 Horizo​​ntalContentAlignment="Stretch",使其内容拉伸整个宽度,而不是仅适合内容。

 <ListBox x:Name="ListBoxResizeItems" 
                HorizontalContentAlignment="Stretch"
                ItemsSource="{Binding Path=ResizeItems}" 
                BorderThickness="0"                                         
                ItemTemplate="{DynamicResource ResizedItemsDataTemplate}" >
        </ListBox>

抱歉,马特,在我写这篇文章时刚刚得到你的答案。

Worked it out. The trick is to set the HorizontalContentAlignment="Stretch" on your listbox to make its contents stretch the full width rather than fit the contents only.

 <ListBox x:Name="ListBoxResizeItems" 
                HorizontalContentAlignment="Stretch"
                ItemsSource="{Binding Path=ResizeItems}" 
                BorderThickness="0"                                         
                ItemTemplate="{DynamicResource ResizedItemsDataTemplate}" >
        </ListBox>

Sorry Matt, just got your answer thorugh as I was typing this post.

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