我的 WPF UserControl 绝不能比包含的 ButtonBar 宽

发布于 2024-09-28 04:11:30 字数 3398 浏览 2 评论 0原文

我已将顶部 UserControl 的宽度绑定到顶部 ButtonGrid 的宽度。

它不起作用。我希望我的 UserControl 始终与 ButtonGrid 的宽度一样宽。问题是加载名称较长的文档 > Sum(3 个按钮的宽度)使 UserControl 与文档名称一样宽。现在想象一下,文档名称包含 100 个字符或更长,并且您添加 UserContol 会碰撞和跳转的文档...

<UserControl x:Class="TBM.View.DocumentListView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:Behaviours="clr-namespace:FunctionalFun.UI.Behaviours"
             xmlns:Helper="clr-namespace:TBM.Helper"            
             mc:Ignorable="d"          
             d:DesignHeight="300"
             d:DesignWidth="300"   
             Width="{Binding ElementName=ButtonGrid,Path=Width}"
             >
    <UserControl.Resources>
        <Helper:BooleanToVisibilityConverter x:Key="boolToVisibilityConverter" />
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ListBox
            SelectionMode="Extended"
            VirtualizingStackPanel.IsVirtualizing="True"
            VirtualizingStackPanel.VirtualizationMode="Recycling"
            Behaviours:MultiSelectorBehaviours.SynchronizedSelectedItems="{Binding SelectedDocumentViewModelList,Mode=TwoWay}"                                                                                 
            Width="Auto"
            Focusable="True"
            ScrollViewer.HorizontalScrollBarVisibility="Auto" 
            ScrollViewer.VerticalScrollBarVisibility="Auto" 
            Grid.Row="0"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            Name="documentListBox"
            BorderThickness="1"                                                
            ItemsSource="{Binding DocumentList}"
            Visibility="{Binding ElementName=documentListBox,Path=HasItems, Converter={StaticResource boolToVisibilityConverter}}"
            >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <!--<TextBlock Text="{Binding Path=Id}" />-->
                        <TextBlock Text="{Binding Path=DocumentName}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>            
        </ListBox>
        <UniformGrid  x:Name="ButtonGrid"         
            Grid.Row="1"
            Rows="1"                                           
            HorizontalAlignment="Stretch"
            VerticalAlignment="Bottom"
            >
            <Button Command="{Binding Path=DeleteDocumentCommand}" HorizontalAlignment="Stretch" Content="Delete" />
            <Button Command="{Binding Path=AddDocumentCommand}" HorizontalAlignment="Stretch" Content="Add" />
            <Button Command="{Binding Path=OpenDocumentCommand}" HorizontalAlignment="Stretch" Content="Open" />           
        </UniformGrid>
    </Grid>
</UserControl>

I have bound at top the Width of the UserControl to the Width of the ButtonGrid at top.

It does not work. I want my UserControl always that wide as the width of the ButtonGrid. The problem is loading documents with a long name > Sum(Width of 3 buttons) makes the UserControl as wide as the document name. Now imagine having document names with 100 chars or longer and you add documents the UserContol will bump and jump...

<UserControl x:Class="TBM.View.DocumentListView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:Behaviours="clr-namespace:FunctionalFun.UI.Behaviours"
             xmlns:Helper="clr-namespace:TBM.Helper"            
             mc:Ignorable="d"          
             d:DesignHeight="300"
             d:DesignWidth="300"   
             Width="{Binding ElementName=ButtonGrid,Path=Width}"
             >
    <UserControl.Resources>
        <Helper:BooleanToVisibilityConverter x:Key="boolToVisibilityConverter" />
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ListBox
            SelectionMode="Extended"
            VirtualizingStackPanel.IsVirtualizing="True"
            VirtualizingStackPanel.VirtualizationMode="Recycling"
            Behaviours:MultiSelectorBehaviours.SynchronizedSelectedItems="{Binding SelectedDocumentViewModelList,Mode=TwoWay}"                                                                                 
            Width="Auto"
            Focusable="True"
            ScrollViewer.HorizontalScrollBarVisibility="Auto" 
            ScrollViewer.VerticalScrollBarVisibility="Auto" 
            Grid.Row="0"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            Name="documentListBox"
            BorderThickness="1"                                                
            ItemsSource="{Binding DocumentList}"
            Visibility="{Binding ElementName=documentListBox,Path=HasItems, Converter={StaticResource boolToVisibilityConverter}}"
            >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <!--<TextBlock Text="{Binding Path=Id}" />-->
                        <TextBlock Text="{Binding Path=DocumentName}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>            
        </ListBox>
        <UniformGrid  x:Name="ButtonGrid"         
            Grid.Row="1"
            Rows="1"                                           
            HorizontalAlignment="Stretch"
            VerticalAlignment="Bottom"
            >
            <Button Command="{Binding Path=DeleteDocumentCommand}" HorizontalAlignment="Stretch" Content="Delete" />
            <Button Command="{Binding Path=AddDocumentCommand}" HorizontalAlignment="Stretch" Content="Add" />
            <Button Command="{Binding Path=OpenDocumentCommand}" HorizontalAlignment="Stretch" Content="Open" />           
        </UniformGrid>
    </Grid>
</UserControl>

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

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

发布评论

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

评论(2

寻梦旅人 2024-10-05 04:11:30

您还可以将 ParentGrid(包含控件)的宽度设置为固定大小,然后水平对齐为 Stretch

You could also set the width of the ParentGrid(containing the control) to a fixed size, en then horizontal alignment to Stretch

没︽人懂的悲伤 2024-10-05 04:11:30

尝试 2 个东西

  1. 在用户控件上使用水平对齐来拉伸 将
  2. 绑定设置为与宽度绑定相同的 MaxWidth

Try 2 stuff

  1. Use horizontal alignment on user control to stretch
  2. Set binding to MaxWidth same as Width binding
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文