以下XAML解释了我想要做的事情。当父项方向设置为垂直时,列表框将按照我的要求进行包装,但我希望左侧的弹出窗口,然后列表框包装意外行为。我已经看过其他关于绑定到父级大小的帖子,使用值转换器等进行数学计算,但我不相信。为什么/应该更改上面的容器2父母突然打破列表框逻辑?
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="421" Width="602">
<Window.Resources>
<ObjectDataProvider x:Key="OrientationEnum" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="Orientation"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<Style x:Key="HorizontalWrapListBox" TargetType="ListBox">
<Style.Setters>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<ScrollViewer HorizontalScrollBarVisibility="Disabled" >
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
</Window.Resources>
<Grid>
<StackPanel Name="stackpanel1" Orientation="Vertical" >
<Expander >
<Grid Background="#FFFFFDEB" >
<StackPanel Width="200" >
<Slider Name="slider1" Minimum="50" Maximum="250"/>
<ComboBox ItemsSource="{Binding Source={StaticResource OrientationEnum}}"
SelectedItem="{Binding ElementName=stackpanel1,Path=Orientation}"/>
</StackPanel>
</Grid>
</Expander>
<Grid Name="parentingGrid">
<ListBox HorizontalAlignment="Stretch" DataContext="{Binding ElementName=slider1}" Style="{StaticResource HorizontalWrapListBox}" >
<Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" />
<Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" />
<Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" />
<Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" />
<Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" />
<Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" />
<Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" />
<Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" />
<Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" />
</ListBox>
</Grid>
</StackPanel>
</Grid>