Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF:按钮未占用列表框中的全部空间_Wpf_Xaml_Listbox_Styles - Fatal编程技术网

WPF:按钮未占用列表框中的全部空间

WPF:按钮未占用列表框中的全部空间,wpf,xaml,listbox,styles,Wpf,Xaml,Listbox,Styles,我有一个列表框,其中有一些由prism添加的按钮 现在,我有以下代码(这里有一些虚拟按钮,仅用于测试目的): <DockPanel LastChildFill="True"> <ListBox HorizontalContentAlignment="Stretch" Padding="0" Margin="0" BorderBrush="Black" DockPanel.Dock="Left" HorizontalAlignment="Stretch">

我有一个
列表框
,其中有一些由prism添加的
按钮

现在,我有以下代码(这里有一些虚拟按钮,仅用于测试目的):

<DockPanel LastChildFill="True">
    <ListBox HorizontalContentAlignment="Stretch" Padding="0" Margin="0" BorderBrush="Black" DockPanel.Dock="Left" HorizontalAlignment="Stretch">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">

            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.Resources>
            <Style TargetType="Button">
                <Setter Property="Height" Value="60" />
                <Setter Property="BorderBrush" Value="Transparent" />
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="BorderThickness" Value="0" />
                <Setter Property="Margin" Value="0" />
                <Setter Property="HorizontalAlignment" Value="Stretch"/>
            </Style>
        </ListBox.Resources>
        <Button>Button 1</Button>
        <Button >Button 2</Button>
    </ListBox>
    <ContentControl></ContentControl>
</DockPanel>

按钮1
按钮2
我目前面临的问题是,我的按钮没有占据全部空间:

  • 如何确保它使用所有可用空间
  • 是否有办法确保所有按钮的高度=宽度
(为了清楚起见,我不想在按钮上设置任何内容,因为它们将由来自不同模块的Prism提供)

当我运行应用程序并聚焦一个对象时,我们似乎看到
ListBoxItem
占据了全部位置,但里面的按钮没有:

如何确保它使用所有可用空间

这是由于
ListBox
控件的模板是如何设计的。若要删除按钮左右两侧的小边距,请为ListBoxItem样式设置Padding=0

        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Padding" Value="0" />
            </Style>
        </ListBox.ItemContainerStyle>

我刚试过,但似乎没有改变任何东西。我添加了一个屏幕截图,显示了它在运行时的反应。@J4N最后一个
ContentControl
在那里做什么?删除它,按钮也会拉伸以填充DockPanel。
ListBox
将是一个菜单,
ContentControl
只是一个位置保持器现在,我想让按钮占据列表框内的所有宽度,列表框不会占用整个空间。在您的屏幕截图中,我们还看到按钮没有占用整个空间(检查,右/左有一个间隙)@ J4N OK,我想我现在明白了——看看我的编辑。这个键仍然是ItMeNeCuultSype。@ J4N如果你想展示按钮,你可以考虑使用<代码> ITEMsStase而不是<代码> ListBox < /C>。后者支持选择,前者只是呈现它的孩子,没有额外的样式。
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Padding" Value="0" />
                <Setter Property="VerticalContentAlignment" Value="Stretch" />
                <Setter Property="Height" Value="{Binding RelativeSource={RelativeSource AncestorType=ListBox},Path=ActualWidth}" />
            </Style>
        </ListBox.ItemContainerStyle>