WPF 扩展列表框找不到定义
WPF Extended ListBox doesn't find definition
扩展了 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 does not contain a definition for '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>
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();
}
}
在模板中设置元素的名称不会在 class 中生成声明模板的成员,即在您的 HorizontalListBox class 中没有 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}" />
扩展了 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 does not contain a definition for '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>
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();
}
}
在模板中设置元素的名称不会在 class 中生成声明模板的成员,即在您的 HorizontalListBox class 中没有 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}" />