2010-10-27 53 views
1

下午好,焦點和選擇造型與多個可能的模板列表框項目

我已經消耗從模型的項目列表的列表框,地址和RAddresses(從地址繼承)

現在,以前我已經在ItemContainerStyle中創建我的項目模板,這樣我只有自己的焦點視覺效果(即沒有虛線,沒有淺灰色矩形等)。這在過去很好。

我現在需要兩個模板,每個類型一個(地址和RAddress)。

<DataTemplate DataType="{x:Type DAL:RAddress}"> 

我也有我的列表框的ItemContainerStyle屬性指向以下:

<Style x:Key="AddressContainerStyle" TargetType="{x:Type ListBoxItem}"> 
     <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="0,0,0,0"/> 
     <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
    </Style> 

以前當模板應用到我在ListBox.resources爲默認模板(如下所示)創建這些風格,這保持了虛線和灰色矩形的視覺效果。但是,現在他們這樣做了,我設置的VisualState設置似乎不適用。

所有的DataTemplate實例如下:

<DataTemplate DataType="{x:Type DAL:Address}"> 
<Border x:Name="Bd" SnapsToDevicePixels="true" Background="#4C000000" CornerRadius="10" Padding="0" BorderBrush="#00FF0000" BorderThickness="1"> 
    <VisualStateManager.VisualStateGroups> 
     <VisualStateGroup x:Name="SelectionStates"> 
      <VisualState x:Name="Unselected"/> 
      <VisualState x:Name="Selected"> 
       <Storyboard> 
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Bd"> 
         <EasingColorKeyFrame KeyTime="0" Value="#80000000"/> 
        </ColorAnimationUsingKeyFrames> 
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="Bd"> 
         <EasingColorKeyFrame KeyTime="0" Value="#00FF0000"/> 
        </ColorAnimationUsingKeyFrames> 
       </Storyboard> 
      </VisualState> 
      <VisualState x:Name="SelectedUnfocused"> 
       <Storyboard> 
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Bd"> 
         <EasingColorKeyFrame KeyTime="0" Value="#80000000"/> 
        </ColorAnimationUsingKeyFrames> 
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="Bd"> 
         <EasingColorKeyFrame KeyTime="0" Value="Red"/> 
        </ColorAnimationUsingKeyFrames> 
       </Storyboard> 
      </VisualState> 
     </VisualStateGroup> 
    </VisualStateManager.VisualStateGroups> 
    <Grid Height="Auto" Width="Auto"> 
     <CheckBox x:Name="checkBox" Content="CheckBox" Margin="0" VerticalAlignment="Top" d:LayoutOverrides="Width" Style="{DynamicResource EditGemStyle}" HorizontalAlignment="Right" IsChecked="{Binding EditMode}"/> 
     <StackPanel Margin="0" Orientation="Vertical" d:LayoutOverrides="Height"> 
      <Grid x:Name="DisplayGrid" Height="Auto" Width="Auto" d:LayoutOverrides="Width" VerticalAlignment="Stretch" Margin="7,3,11,3"> 
       <StackPanel Orientation="Vertical" Width="Auto" d:LayoutOverrides="Width, Height" Margin="0,0,10,0"> 
        <TextBlock x:Name="AddressFriendlyName" TextWrapping="Wrap" Text="{Binding AddressFriendlyName}" FontSize="18.667" Style="{DynamicResource Data}"/> 
        <TextBlock x:Name="Address1" TextWrapping="Wrap" Text="{Binding Address1}" d:LayoutOverrides="Width" FontSize="13.333" Style="{DynamicResource Data}"/> 
        <TextBlock x:Name="Address2" TextWrapping="Wrap" Text="{Binding Address2}" FontSize="13.333" Style="{DynamicResource Data}"/> 
        <TextBlock x:Name="Address3" TextWrapping="Wrap" Text="{Binding Address3}" FontSize="13.333" Style="{DynamicResource Data}"/> 
        <StackPanel Orientation="Horizontal"> 
         <TextBlock x:Name="City" TextWrapping="Wrap" Text="{Binding City}" FontSize="13.333" Style="{DynamicResource Data}"/> 
         <TextBlock TextWrapping="Wrap" Text=", " FontSize="13.333" Style="{DynamicResource Data}"/> 
         <TextBlock x:Name="State" TextWrapping="Wrap" Text="{Binding State}" Margin="0,0,5,0" FontSize="13.333" Style="{DynamicResource Data}"/> 
         <TextBlock x:Name="ZIP" TextWrapping="Wrap" Text="{Binding ZIP}" FontSize="13.333" Style="{DynamicResource Data}"/> 
        </StackPanel> 
        <TextBlock x:Name="Country" TextWrapping="Wrap" Text="{Binding Country}" FontSize="13.333" Style="{DynamicResource Data}"/> 
       </StackPanel> 
      </Grid> 
      <Grid x:Name="EditGrid" Height="Auto" Margin="7,3,11,0" Visibility="Collapsed"> 
       <StackPanel Orientation="Vertical" Width="Auto" Margin="0,0,10,0" d:LayoutOverrides="Width, Height"> 
        <TextBox x:Name="AddressFriendlyName1" TextWrapping="Wrap" Text="{Binding AddressFriendlyName}" Template="{DynamicResource SnazzyTextBoxTemplate}" Margin="0,2" Foreground="White"/> 
        <TextBox x:Name="Address4" TextWrapping="Wrap" Text="{Binding Address1}" d:LayoutOverrides="Width" Template="{DynamicResource SnazzyTextBoxTemplate}" Margin="0,2" Foreground="White"/> 
        <TextBox x:Name="Address5" TextWrapping="Wrap" Text="{Binding Address2}" Template="{DynamicResource SnazzyTextBoxTemplate}" Margin="0,2" Foreground="White"/> 
        <TextBox x:Name="Address6" TextWrapping="Wrap" Text="{Binding Address3}" Template="{DynamicResource SnazzyTextBoxTemplate}" Margin="0,2" Foreground="White"/> 
        <StackPanel Orientation="Horizontal"> 
         <TextBox x:Name="City1" TextWrapping="Wrap" Text="{Binding City}" Template="{DynamicResource SnazzyTextBoxTemplate}" Margin="0,2" Foreground="White"/> 
         <TextBlock TextWrapping="Wrap" Text=", " Foreground="White"/> 
         <TextBox x:Name="State1" TextWrapping="Wrap" Text="{Binding State}" Margin="0,2,5,2" Template="{DynamicResource SnazzyTextBoxTemplate}" Foreground="White"/> 
         <TextBox x:Name="ZIP1" TextWrapping="Wrap" Text="{Binding ZIP}" Template="{DynamicResource SnazzyTextBoxTemplate}" Margin="0,2" Foreground="White"/> 
        </StackPanel> 
        <TextBox x:Name="Country1" TextWrapping="Wrap" Text="{Binding Country}" Template="{DynamicResource SnazzyTextBoxTemplate}" Margin="0,2" Foreground="White"/> 
       </StackPanel> 
      </Grid> 
     </StackPanel> 
    </Grid> 
</Border> 
<DataTemplate.Triggers> 
    <DataTrigger Binding="{Binding Address2}" Value="{x:Null}"> 
     <Setter TargetName="Address2" Property="Visibility" Value="Collapsed"/> 
    </DataTrigger> 
    <DataTrigger Binding="{Binding Address3}" Value="{x:Null}"> 
     <Setter TargetName="Address3" Property="Visibility" Value="Collapsed"/> 
    </DataTrigger> 
    <DataTrigger Binding="{Binding EditMode}" Value="False"> 
     <Setter Property="Visibility" TargetName="DisplayGrid" Value="Visible"/> 
     <Setter Property="Visibility" TargetName="EditGrid" Value="Collapsed"/> 
    </DataTrigger> 
    <DataTrigger Binding="{Binding EditMode}" Value="True"> 
     <Setter Property="Visibility" TargetName="DisplayGrid" Value="Collapsed"/> 
     <Setter Property="Visibility" TargetName="EditGrid" Value="Visible"/> 
    </DataTrigger> 
</DataTemplate.Triggers> 

以前我已經使用StyleSelector創建的ItemContainerStyles(每個類型)之間進行選擇嘗試。這導致我遇到了一個錯誤「不能動畫」(0)。(1)'在一個不可變的對象實例上「,所以我開始查看當前的設置。

如何在同一個ListBox中託管的兩個單獨項目模板上強制執行自己的焦點和選擇樣式?

任何幫助將不勝感激。

回答

1

我認爲您需要將故事板的規格移動到ItemContainerStyle,而不是讓它們在DataTemplates本身中指定。這是更幹,並將導致更少的併發症。

<Window.Resources> 

    <DataTemplate DataType="{x:Type DAL:RAddress}"> 
     <TextBlock>RAddress!</TextBlock> 
    </DataTemplate> 

    <DataTemplate DataType="{x:Type DAL:Address}"> 
     <TextBlock>Address!</TextBlock> 
    </DataTemplate> 
    <Style x:Key="IHateDottedBorders" TargetType="{x:Type ListBoxItem}"> 
     <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="0,0,0,0"/> 
     <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
        <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="SelectionStates"> 
           <VisualState x:Name="Unselected"/> 
           <VisualState x:Name="Selected"> 
            <Storyboard> 
             <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Bd"> 
              <EasingColorKeyFrame KeyTime="0" Value="#80000000"/> 
             </ColorAnimationUsingKeyFrames> 
             <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="Bd"> 
              <EasingColorKeyFrame KeyTime="0" Value="#00FF0000"/> 
             </ColorAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="SelectedUnfocused"> 
            <Storyboard> 
             <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Bd"> 
              <EasingColorKeyFrame KeyTime="0" Value="#80000000"/> 
             </ColorAnimationUsingKeyFrames> 
             <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="Bd"> 
              <EasingColorKeyFrame KeyTime="0" Value="Red"/> 
             </ColorAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsSelected" Value="true"> 
          <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
          <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="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
          <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> 


</Window.Resources> 

<Grid> 
    <ListBox ItemContainerStyle="{StaticResource IHateDottedBorders}" ItemsSource="{Binding CollectionOfAddresses}" /> 
</Grid> 
+0

上面的樣式在單擊列表框項時會返回被恨的「無法動畫」(0)。(1)'對不可變對象實例'錯誤。我也嘗試過一個你以前給過我的樣式,它包含一個用於VisualState的setter的樣式,但是VisualState和VisualStateManager沒有一個有效的setter。任何其他想法? – CodeWarrior 2010-11-03 14:42:46

相關問題