have extended ListBox to give me a horizontal List Box, i.e, in 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>
and in the code behind
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);
}
}
where the dependency property IndentItems is suppose to allow me to indent the items a set amount by setting the margin of the VirtualizingStackPanel used to contain the items. However, attempting to build this throws an error telling me that "HorizonatlListBox does not contain a definition for 'ItemContainer'" even though it has been given that name in the xaml. Any ideas where I may be going wrong?
Setting the Name of an element in a Template does not generate a member in the class that declares the Template, i.e. there is no
ItemContainer
member in your HorizontalListBox class.Besides that, your
IndentItems
property seems to be redundant, becauseListBox
already has aPadding
property:Now there is probably no more need for a derived HorizontalListBox, as you could just write this: