Wpf 应用CustomListBoxItem样式时的应用程序异常

Wpf 应用CustomListBoxItem样式时的应用程序异常,wpf,xaml,windows-phone-8,listboxitem,Wpf,Xaml,Windows Phone 8,Listboxitem,我有个问题需要你花点时间。我需要在我的Windows phone 8应用程序的多个页面上添加列表框,在每个页面上,ListBoxItem的正常/选定背景是不同的(10-12页)。一种解决方案是,我为每个页面的每个ListBoxItem创建不同的样式,在样式表中硬编码颜色代码(我不想要)。因此,我扩展了ListBoxItem类,并为ListBoxItem的普通项和选定项背景添加了2个依赖项属性。这样我就可以在每个xaml页面上设置这些属性,而不必为每个页面创建不同的样式。 这是我的扩展控制 pub

我有个问题需要你花点时间。我需要在我的Windows phone 8应用程序的多个页面上添加列表框,在每个页面上,ListBoxItem的正常/选定背景是不同的(10-12页)。一种解决方案是,我为每个页面的每个ListBoxItem创建不同的样式,在样式表中硬编码颜色代码(我不想要)。因此,我扩展了ListBoxItem类,并为ListBoxItem的普通项和选定项背景添加了2个依赖项属性。这样我就可以在每个xaml页面上设置这些属性,而不必为每个页面创建不同的样式。 这是我的扩展控制

public class CustomListboxItem : ListBoxItem
{
    public static readonly DependencyProperty NormalBackgroundProperty =
        DependencyProperty.Register("NormalBackground", typeof(SolidColorBrush), typeof(CustomListboxItem), new PropertyMetadata(null));
    public static readonly DependencyProperty SelectedBackgroundProperty =
        DependencyProperty.Register("SelectedBackground", typeof(SolidColorBrush), typeof(CustomListboxItem), new PropertyMetadata(null));
    public SolidColorBrush NormalBackground
    {
        get { return (SolidColorBrush)GetValue(NormalBackgroundProperty); }
        set { base.SetValue(NormalBackgroundProperty, value); }
    }
    public SolidColorBrush SelectedBackground
    {
        get { return (SolidColorBrush)GetValue(SelectedBackgroundProperty); }
        set { base.SetValue(SelectedBackgroundProperty, value); }
    }
}
下面是如何使用这个(CyclesList只是字符串列表):


这是我正在应用的使其通用的样式

<Style x:Key="FilterListBoxItemStyle" TargetType="controls:CustomListboxItem">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Top"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver"/>
                            <VisualState x:Name="Disabled"/>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="SelectionStates">
                            <VisualState x:Name="Unselected"/>
                            <VisualState x:Name="Selected">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ContentContainer">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedBackground}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                    Background="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=NormalBackground}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

但是我的应用程序在这里抛出了一个异常(完整堆栈跟踪):

{System.Windows.Markup.XamlParseException:[行:0位置:0]-->System.Windows.Markup.XamlParseException:[行:0位置:0]-->System.Windows.Markup.XamlParseException:[行:0位置:0]-->System.Windows.Markup.XamlParseException:[行:0位置:0]-->System.Windows.Markup.XamlParseException:[行:0位置:0]-->System.Windows.Markup.XamlParseException:[行:0位置:0] 在MS.Internal.XcpImports.CheckHResult(UInt32小时) 在MS.Internal.XcpImports.SetValue处(IManagedPeerBase对象,DependencyProperty属性,DependencyObject doh) 位于System.Windows.DependencyObject.SetValue(DependencyProperty属性,DependencyObject doh) 在System.Windows.Controls.ListBox.GetContainerForItemOverride()中 位于System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.GetContainerForItem(对象项,DependencyObject recycledContainer) 位于System.Windows.Controls.ItemContainerGenerator.Generator.GenerateNext(布尔stopAtRealized,布尔&isNewlyRealized) 位于System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.GenerateNext(布尔值&isNewlyRealized) 位于System.Windows.Controls.VirtualzingStackPanel.MeasureOverride(大小约束) 在System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget、双内宽、双内高、双外宽、双外高) ---内部异常堆栈跟踪的结束--- 在MS.Internal.XcpImports.CheckHResult(UInt32小时) 在MS.Internal.XcpImports.FrameworkElement_测量值超过(FrameworkElement元素,可用大小大小) 位于System.Windows.Controls.ScrollContentPresenter.MeasureOverride(大小约束) 在System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget、双内宽、双内高、双外宽、双外高) ---内部异常堆栈跟踪的结束--- 在MS.Internal.XcpImports.CheckHResult(UInt32小时) 在MS.Internal.XcpImports.UIElement_处测量(UIElement元素,可用大小大小) 位于System.Windows.UIElement.Measure(可用大小大小) 位于System.Windows.Controls.ScrollViewer.MeasureOverride(大小约束) 在System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget、双内宽、双内高、双外宽、双外高) ---内部异常堆栈跟踪的结束--- 在MS.Internal.XcpImports.CheckHResult(UInt32小时) 在MS.Internal.XcpImports.FrameworkElement_测量值超过(FrameworkElement元素,可用大小大小) 位于System.Windows.FrameworkElement.MeasureOverride(可用大小Size) 在System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget、双内宽、双内高、双外宽、双外高) ---内部异常堆栈跟踪的结束--- 在MS.Internal.XcpImports.CheckHResult(UInt32小时) 在MS.Internal.XcpImports.FrameworkElement_测量值超过(FrameworkElement元素,可用大小大小) 位于System.Windows.FrameworkElement.MeasureOverride(可用大小Size) 在System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget、双内宽、双内高、双外宽、双外高) ---内部异常堆栈跟踪的结束--- 在MS.Internal.XcpImports.CheckHResult(UInt32小时) 在MS.Internal.XcpImports.FrameworkElement_测量值超过(FrameworkElement元素,可用大小大小) 位于Microsoft.Phone.Controls.PhoneApplicationFrame.MeasureOverride(可用大小大小) 在System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget、双内宽、双内高、双外宽、双外高) ---内部异常堆栈跟踪结束---}

从堆栈跟踪中,如果我缺少控件的宽度/高度,可能会出现这种情况,但我也尝试过设置这些宽度/高度,但没有任何效果。我是否做错了什么?或者你能推荐一种更好的方法来制作通用的CustomListBoxItem吗

期待中的感谢


关于,

CustomListboxItem样式的资源字典键是
FilterListBoxItemStyle

<Style x:Key="FilterListBoxItemStyle" TargetType="controls:CustomListboxItem">
将上述声明更改为

<ListBox ... ItemContainerStyle="{StaticResource FilterListBoxItemStyle}">
自定义列表框不再声明
<ListBox ... ItemContainerStyle="{StaticResource FilterListBoxStyle}">
<ListBox ... ItemContainerStyle="{StaticResource FilterListBoxItemStyle}">
public class CustomListBox : ListBox
{
    protected override DependencyObject GetContainerForItemOverride()
    {
        return new CustomListboxItem();
    }
}
<controls:CustomListBox ...
    ItemContainerStyle="{StaticResource FilterListBoxItemStyle}" />
<ListBox Grid.Row="1" ItemsSource="{Binding CyclesList}" ItemContainerStyle="{StaticResource FilterListBoxItemStyle}" >
    <ListBox.Resources>
        <SolidColorBrush Color="Transparent"
                     x:Key="NormalBackground" />
        <SolidColorBrush Color="Red"
                     x:Key="SelectedBackground" />
    </ListBox.Resources>
</ListBox>