2010-04-22 63 views
1

我對WPF相當陌生,並且無法使DataTemplateKey找到我的ListBox。WPF DataTemplateKey找不到ListBox鍵

<Window.Resources> 

    <ControlTemplate x:Key="FocusTemplate" > 
     <Rectangle Fill="Azure" Width="290" Height="55" /> 
    </ControlTemplate> 
    <Style x:Key="FocusStyle" TargetType="{x:Type Control}"> 
     <Setter Property="Template" Value="{StaticResource FocusTemplate}"/> 
    </Style> 

    <Style TargetType="ListBoxItem"> 
     <EventSetter Event="GotFocus" Handler="ListItem_GotFocus"></EventSetter> 
    </Style> 

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

    <DataTemplate x:Key="CustomListData" DataType="{x:Type ListBox}"> 
     <Border BorderBrush="Black" BorderThickness="1" Margin="-2,0,0,-1"> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="55*" /> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="Auto" /> 
        <ColumnDefinition Width="*" /> 
       </Grid.ColumnDefinitions> 
       <Grid.RenderTransform> 
        <TransformGroup> 
         <ScaleTransform ScaleX="1" ScaleY="1"/> 
         <SkewTransform AngleX="0" AngleY="0"/> 
         <RotateTransform Angle="0"/> 
         <TranslateTransform X="0" Y="0"/> 
        </TransformGroup> 
       </Grid.RenderTransform> 
       <!--<ScrollViewer x:Name="PART_ContentHost" />--> 
       <TextBox Width="290" TextAlignment="Left" VerticalContentAlignment="Center" BorderThickness="0" BorderBrush="Transparent" 
             Foreground="#FF6FB8FD" 
             FontSize="18" 
             FocusVisualStyle="{StaticResource FocusStyle}" 
             Name="editingBox" 
             TextWrapping="Wrap" 
             Text="{Binding .}" 
             Grid.Column="1" 
             Grid.Row="1" 
             MinHeight="55" 
             Cursor="Hand" 
             IsReadOnly="True" 
             > 
        <TextBox.Background> 
         <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> 
          <LinearGradientBrush.RelativeTransform> 
           <TransformGroup> 
            <ScaleTransform CenterX="0.5" CenterY="0.5"/> 
            <SkewTransform CenterX="0.5" CenterY="0.5"/> 
            <RotateTransform Angle="0" CenterX="0.5" CenterY="0.5"/> 
            <TranslateTransform/> 
           </TransformGroup> 
          </LinearGradientBrush.RelativeTransform> 
          <GradientStop Color="#FF2D4984"/> 
          <GradientStop Color="#FF182D56" Offset="0.042"/> 
         </LinearGradientBrush> 
        </TextBox.Background> 

       </TextBox> 
      </Grid> 
     </Border> 
    </DataTemplate> 

    <Style TargetType="{x:Type ListBox}"> 
     <Setter Property="ItemTemplate" Value="{StaticResource CustomListData }" /> 
     <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" /> 
    </Style> 

    </Window.Resources> 

    <Window.DataContext> 
    <ObjectDataProvider 
    ObjectType="{x:Type local:ImageLoader}" 
    MethodName="LoadImages" 
    /> 
    </Window.DataContext> 

    <ListBox ItemsSource="{Binding}" Width="320" Background="#FF021422" BorderBrush="#FF1C4B79"> 

     <ListBox.Resources> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">Transparent</SolidColorBrush> 
     </ListBox.Resources> 

    </ListBox> 

下面的代碼會發現一個TextBlock

var key = new System.Windows.DataTemplateKey(typeof(TextBlock)); 
var r = (DataTemplate)this.FindResource(key); 

但是,當我改變類型列表框,鑰匙無法找到。我錯過了什麼?

感謝 瑞安

+0

你可以接受答案,如果它幫助 – Amsakanna 2010-04-22 16:49:23

回答

1

你給你的ListBox DataTemplate一個不同的密鑰,具體而言,x:Key="CustomListData"x:Key屬性將優先於根據DataType生成的自動DataTemplateKey。刪除該密鑰並自動找到DataTemplateKey

+0

查理,這很好。但現在我怎麼會有這個列表框使用我的自定義ItemTemplate。因爲我不再有鑰匙,所以我不得不評論這位二傳手。 – Ryan 2010-04-22 15:57:41

+0

無鍵DataTemplate將自動應用於相同DataType的任何元素。我想知道爲什麼您的CustomListData將其DataType標記爲「ListBox」,即使它正在應用於ListBoxItems。也許將其DataType更改爲ListBoxItem? – Charlie 2010-04-22 16:41:37

+0

謝謝你做到了! – Ryan 2010-04-22 17:09:24