我的WPF用户控件的宽度决不能超过包含的按钮栏

我的WPF用户控件的宽度决不能超过包含的按钮栏,wpf,xaml,user-controls,width,bind,Wpf,Xaml,User Controls,Width,Bind,我已经在顶部将UserControl的宽度绑定到顶部ButtonGrid的宽度 它不起作用。我希望我的用户控件总是和ButtonGrid的宽度一样宽。问题是加载长名称>总和(3个按钮的宽度)的文档会使UserControl与文档名称一样宽。现在想象一下,如果文档名为100个字符或更长,那么您添加的文档UserControl会跳起来 <UserControl x:Class="TBM.View.DocumentListView" xmlns="http://sche

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

它不起作用。我希望我的用户控件总是和ButtonGrid的宽度一样宽。问题是加载长名称>总和(3个按钮的宽度)的文档会使UserControl与文档名称一样宽。现在想象一下,如果文档名为100个字符或更长,那么您添加的文档UserControl会跳起来

<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>

试试两样东西

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

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

    我不明白您的答案。将水平对齐设置为什么。设置最大高度??为什么是身高?我谈到WIDTHOk绑定到ListBox的方式与我在向ListBox添加文档时对UserControl的方式相同,但当我在日历数据网格中前后导航时,文档ListBox又具有最长ListboxItem的宽度:/不理解最后一条注释,可以解释吗?当然可以:1。用户HorizontAlighment=“拉伸”?为什么会这样?我不想拉伸我的用户控件,我还没有写过。我只是将“水平对齐”设置为“拉伸”用户控件!它就像一个符咒!谢谢包含UserControl的控件是DataGrid或DataGridTemplateColumn。我不想设置固定大小,因为/或者将来会在现有的3个按钮上添加更多按钮。然后宽度必须是数据网格的总和(4个按钮的宽度),在这种情况下,添加按钮时只需更改数据网格的大小。但我对WPF还比较陌生,所以可能有更好的方法;)英雄联盟可能还有更好的办法!;-)目前,我将UserControl设置为125像素,可用于3个按钮。但是没有更多的按钮,那么我必须把125换成。。。等不可维护;-)我刚刚将用户控件的“水平对齐”设置为“拉伸”!它就像一个符咒!谢谢