3
我有一个数据绑定ListBox与DataTemplate设置。 DataTemplate包含一个Grid控件,其中两列宽度为Auto和*。我希望此列始终填充ListBoxItem,并且永远不会超出LisBox控件以使水平滚动条可见。WPF列表框宽度/最大宽度
我能够在MaxWidth绑定到DataTemplate中的网格使用:
MaxWidth="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}}, Path=ActualWidth}"
这是实现这一目标的最佳方式是什么?
我也有ItemContainerStyle设置以下。触发器已被删除,使代码更小,更易于阅读。
<Style x:Key="ListBoxItemContainer" TargetType="{x:Type ListBoxItem}">
<Setter Property="Padding" Value="3"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Border" BorderBrush="{x:Null}" CornerRadius="4" BorderThickness="1" Margin="1">
<Border x:Name="InnerBorder" BorderBrush="{x:Null}" CornerRadius="4" BorderThickness="1">
<ContentPresenter x:Name="ContentPresenter" Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
谢谢。我在样式中使用了,但禁用ScrollViewer可以解决问题。 –
Luke
2009-06-30 06:09:35