【发布时间】:2015-08-23 22:21:57
【问题描述】:
已扩展 ListBox 以给我一个水平列表框,即在 XAML 中:
<ListBox x:Class="Renishaw.Inspire.InTheatreCompanion.View.HorizontalListBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Style="{x:Null}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Name="ItemContainer"
Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
在后面的代码中
public partial class HorizontalListBox : ListBox
{
public static readonly DependencyProperty IndentItemsProperty = DependencyProperty.Register(
"IndentItems", typeof(double), typeof(HorizontalListBox), new PropertyMetadata(0.0, new PropertyChangedCallback(OnIndentItemsChanged)));
public HorizontalListBox()
{
InitializeComponent();
}
public double IndentItems
{
get { return (double)GetValue(IndentItemsProperty); }
set { SetValue(IndentItemsProperty, value); }
}
private static void OnIndentItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var horizontalListBox = (HorizontalListBox)d;
var thickness = new Thickness((double)e.NewValue, 0, 0, 0);
horizontalListBox.ItemContainer.Margin = thickness;
}
public static void SetIndentItems(DependencyObject obj, double thickness)
{
obj.SetValue(IndentItemsProperty, thickness);
}
public static double GetIndentItems(DependencyObject obj)
{
return (double)obj.GetValue(IndentItemsProperty);
}
}
假设依赖属性 IndentItems 允许我通过设置用于包含项目的 VirtualizingStackPanel 的边距来将项目缩进设定的数量。但是,尝试构建它会引发一个错误,告诉我“HorizonatlListBox 不包含 'ItemContainer' 的定义”,即使它已在 xaml 中指定了该名称。有什么想法我可能会出错吗?
【问题讨论】:
-
@Clemens:在 HorizonatlListBox 级别设置边距会产生不同的行为,即,识别列表框而不是列表框中的列表项。这会在滚动时呈现不同的外观。
-
确实如此!完全错过了那个。如果您将其设置为答案,我会将其标记为正确。 :)