扩展了ListBox以给我一个水平的List Box,即在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的边距来将项目缩进设定的金额。但是,尝试构建它会抛出一个错误,告诉我&#34; HorizonatlListBox不包含&#39; ItemContainer&#39;&#34;即使已经在xaml中给出了该名称。我可能会出错的任何想法?
答案 0 :(得分:1)
在模板中设置元素的名称不会在声明模板的类中生成成员,即您的HorizontalListBox类中没有ItemContainer
成员。
除此之外,您的IndentItems
属性似乎是多余的,因为ListBox
已经有Padding
属性:
<local:HorizontalListBox Padding="50,0,0,0" .../>
现在可能不再需要派生的HorizontalListBox,因为你可以这样写:
<ListBox Padding="50,0,0,0" ...>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox/>
答案 1 :(得分:0)
您可以比创建新控件更简单地满足您的要求:
<App.Resources>
<ItemsPanelTemplate x:Key="HorizontalStackPanelTemplate">
<VirtualizingStackPanel Orientation="Horizontal" />
<ItemsPanelTemplate>
</App.Resources>
<ListBox ItemsSource="{Binding source}" Padding ="50,0,0,0"
ItemsPanel="{StaticResource HorizontalStackPanelTemplate}" />
答案 2 :(得分:-1)
如何使用转换器?
<Window x:Class="ListBoxMargins.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ListBoxMargins"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<local:StackConverter x:Key="StackConverter"/>
</Grid.Resources>
<ListBox ItemsSource="{Binding source}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Name="ItemContainer"
Orientation="Horizontal"
Margin="{Binding RelativeSource={RelativeSource Self}, Path=IsKeyboardFocusWithin, Converter={StaticResource StackConverter}}"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
public partial class MainWindow : Window
{
public ObservableCollection<string> source { get; set; }
public MainWindow()
{
InitializeComponent();
source = new ObservableCollection<string>();
source.Add("Test element1");
source.Add("Test element2");
source.Add("Test element3");
this.DataContext = this;
}
}
我没有在这里进行所有检查,但在我的测试中,面板是缩进的。
public class StackConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool focused = (bool)value;
if (focused)
{
return new Thickness(50);
}
else
{
return new Thickness(0);
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}