修改列表框抛出异常

问题描述:

我修改列表框在我Change the Selected Color Listbox问题中提到,当过我选择项目的例外是thrown.It读取修改列表框抛出异常

'{DependencyProperty.UnsetValue}' is not a valid value for property 'Background'. 

我在做什么错

使用XMAL

<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle1}" Width="300" Height="519" Name="listFriends" HorizontalAlignment="Left" VerticalAlignment="Center" > 
    <ListBox.Items> 
     <StackPanel Width="289" Orientation="Horizontal" > 
     <Image Margin="0,0,4,0" Height="50" Width="50" Source="{Binding Picture}" /> 
     <my:RibbonCheckBox Label="test" IsChecked="{Binding IsChecked}"/> 
     </StackPanel> 
     <StackPanel Width="289" Orientation="Horizontal" > 
      <Image Margin="0,0,4,0" Height="50" Width="50" Source="{Binding Picture}" /> 
      <my:RibbonCheckBox Height="20" Label="test" IsChecked="{Binding IsChecked}" Foreground="White" /> 
     </StackPanel> 
     <StackPanel Width="289" Orientation="Horizontal" > 
      <Image Margin="0,0,4,0" Height="50" Width="50" Source="{Binding Picture}" /> 
      <my:RibbonCheckBox Height="20" Label="test" IsChecked="{Binding IsChecked}" Foreground="White" /> 
     </StackPanel> 
    </ListBox.Items> 
</ListBox> 

款式我有修改

<Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}"> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
    <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
    <Setter Property="Padding" Value="2,0,0,0"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" 
     BorderThickness="{TemplateBinding BorderThickness}" 
     Background="{TemplateBinding Background}" 
     Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true" CornerRadius="10" 
     > 
        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsSelected" Value="true"> 
         <Setter Property="Background" TargetName="Bd" Value="{StaticResource GrBrush}"/> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> 
        </Trigger> 

        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsSelected" Value="true"/> 
          <Condition Property="Selector.IsSelectionActive" Value="false"/> 
         </MultiTrigger.Conditions> 
         <Setter Property="Background" TargetName="Bd" Value="{StaticResource GradientBrush}"/> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
        </MultiTrigger> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
        </Trigger> 
       </ControlTemplate.Triggers>       
      </ControlTemplate>      
     </Setter.Value>     
    </Setter>    
</Style> 
+1

你能后的完整XAML片段?看起来你的绑定产生了意想不到的结果。 – 2011-04-02 01:59:37

+0

请看编辑问题 – 2011-04-02 02:46:13

+0

假设GrBrush和Gradient笔刷是合法的,这看起来很好。您是否100%确定问题来自您的XAML的这个片段,并且您没有代码与Background属性进行交互? – 2011-04-02 02:59:14

问题是,我宣布使用像

<Setter Property="Background" TargetName="Bd" Value="{StaticResource GradientBrush}"/> 

与GrBrush

<LinearGradientBrush x:Key="GrBrush" EndPoint="0.5,1" StartPoint="0.5,0"> 
      <GradientStop Color="#FF6BB9E8" Offset="0" /> 
      <GradientStop Color="#541882CE" Offset="1" /> 
     </LinearGradientBrush> 

和使用情况是不同的,这就是为什么出错