【问题标题】:My WPF UserControl must never get wider than the containing ButtonBar我的 WPF UserControl 决不能比包含的 ButtonBar 更宽
【发布时间】:2014-02-13 15:34:40
【问题描述】:

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

它不起作用。我希望我的 UserControl 始终与 ButtonGrid 的宽度一样宽。问题是加载具有长名称的文档 > Sum(Width of 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>

【问题讨论】:

    标签: wpf xaml user-controls width bind


    【解决方案1】:

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

    【讨论】:

    • 包含UserControl的控件准确来说是DataGrid或者DataGridTemplateColumn。我不想设置固定大小,因为/并且将来可能会在 3 个现有按钮中添加更多按钮。那么宽度必须是总和(4个按钮的宽度)
    • 在本例中为 DataGrid,当您添加按钮时,只需更改 DataGrid 的大小。但我对 WPF 相当陌生,所以可能有更好的方法;)
    • 大声笑...可能有更好的方法! ;-) 目前我将 UserControl 设置为 125 像素,它适用于 3 个按钮。但不是更多的按钮,然后我必须将 125 更改为 ... 等等...不可维护 ;-)
    • 我刚刚将 UserControl 的“Horizo​​ntalAlignment”设置为“Stretch”!它就像一个魅力!谢谢!
    【解决方案2】:

    试试 2 样东西

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

    【讨论】:

    • 我不明白你的回答。将水平对齐设置为 WHAT。设置最大高度??为什么是身高?我谈WIDTH
    • Ok 绑定到 ListBox 的方式与我对 UserControl 所做的方式相同,当我将文档添加到列表框时,但是当我在日历 DataGrid 中来回导航时,文档列表框的宽度再次为最长的 ListboxItem :/
    • 不明白最后的评论能解释一下吗?
    • 确定我可以: 1. 用户 Horizo​​ntAlighment="Stretch" ?为什么这个?我不想拉伸我的 UserControl,我没有写过。
    • 我刚刚将 UserControl 的“Horizo​​ntalAlignment”设置为“Stretch”!它就像一个魅力!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 2015-02-04
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    相关资源
    最近更新 更多