WPF扩展列表框不';找不到定义
已扩展ListBox以提供一个水平列表框,即在XAML中:WPF扩展列表框不';找不到定义,wpf,xaml,listbox,Wpf,Xaml,Listbox,已扩展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"
<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>
其中,dependency属性IndentItems允许我通过设置用于包含项目的VirtualzingStackPanel的边距,将项目缩进设置的数量。然而,试图构建它会抛出一个错误,告诉我“HorizontalListBox不包含‘ItemContainer’的定义”,即使它在xaml中被赋予了这个名称。你知道我哪里出了问题吗?用转换器怎么样
<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>
在模板中设置元素的名称不会在声明模板的类中生成成员,即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/>
与创建新控件相比,您可以更简单地满足您的要求:
<App.Resources>
<ItemsPanelTemplate x:Key="HorizontalStackPanelTemplate">
<VirtualizingStackPanel Orientation="Horizontal" />
<ItemsPanelTemplate>
</App.Resources>
<ListBox ItemsSource="{Binding source}" Padding ="50,0,0,0"
ItemsPanel="{StaticResource HorizontalStackPanelTemplate}" />
我不确定是否看到使用转换器的好处(其中仍然有一个硬编码的厚度值)。理想情况下,缩进(厚度)应该是扩展列表框的一个属性,因此使用依赖属性。@Clemens:在HorizontalListBox级别设置边距会产生不同的行为,即列表框是标识的,而不是列表框中的列表项。这会在滚动时显示不同的外观。确实如此!完全错过了那个。如果你把它作为答案,我会把它标记为正确的。:)
<local:HorizontalListBox Padding="50,0,0,0" .../>
<ListBox Padding="50,0,0,0" ...>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox/>
<App.Resources>
<ItemsPanelTemplate x:Key="HorizontalStackPanelTemplate">
<VirtualizingStackPanel Orientation="Horizontal" />
<ItemsPanelTemplate>
</App.Resources>
<ListBox ItemsSource="{Binding source}" Padding ="50,0,0,0"
ItemsPanel="{StaticResource HorizontalStackPanelTemplate}" />