2011-01-17 11 views
0

Ich habe eigene Art auf Listbox-Element, hier ist es:Problem mit Steuervorlage auf Listbox-Element

 <Style x:Key="friendsListStyle" TargetType="{x:Type ListBox}"> 
      <Setter Property="ItemTemplate"> 
       <Setter.Value> 
        <DataTemplate> 
         <Grid Name="RootLayout"> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="0.3*"></ColumnDefinition> 
           <ColumnDefinition Width="*"></ColumnDefinition> 
          </Grid.ColumnDefinitions> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="60"></RowDefinition> 
          </Grid.RowDefinitions> 
          <Image Margin="4,4,4,2" Grid.Column="0"> 
           <Image.Source > 
            <MultiBinding Converter="{StaticResource avatarConverter}"> 
             <Binding Path="ProfilePhoto"></Binding> 
             <Binding Path="StatusInfo.IsLogged"></Binding> 
            </MultiBinding> 
           </Image.Source> 
          </Image> 
          <Grid Grid.Column="1"> 
            <Grid.ColumnDefinitions> 
             <ColumnDefinition Width="*"></ColumnDefinition> 
            </Grid.ColumnDefinitions> 
            <Grid.RowDefinitions> 
             <RowDefinition Height="*"></RowDefinition> 
             <RowDefinition Height="*"></RowDefinition> 
             <RowDefinition Height="*"></RowDefinition> 
            </Grid.RowDefinitions> 
            <TextBlock 
             Text="{Binding Path=Nick}" 
             Margin="2,2,2,2" 
             FontSize="13" 
             FontWeight="Medium" 
             Grid.Column="0" Grid.Row="0"> 
            </TextBlock> 
            <TextBlock 
             Text="{Binding Path=StatusMessageInfo.Message}" 
             FontSize="11" 
             FontWeight="Normal" 
             Foreground="DarkGray" 
             Grid.Column="0" Grid.Row="1" Margin="2,2,2,2"></TextBlock> 
            <TextBlock 
             Style="{StaticResource StatusStyle}" 
             Grid.Column="0" Grid.Row="2" > 
            <TextBlock.Text> 
            <MultiBinding Converter="{StaticResource infoConverter}"> 
             <Binding Path="StatusInfo.IsLogged"></Binding> 
             <Binding Path="StatusInfo.IsChating"></Binding> 
             <Binding Path="StatusInfo.RoomName"></Binding> 
            </MultiBinding> 
            </TextBlock.Text> 
            </TextBlock> 
           </Grid> 
         </Grid> 
        </DataTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

In App wie folgt aussehen:

alt text

I Farbe der Listbox ändern müssen Element, wenn ausgewählt ist, so habe ich versucht, Steuerelement Vorlage auf Listbox-Element zu schreiben und in Listbox-Stil zu verwenden:

Hier ist Steuerelement Vorlage auf Listbox Element:

 <Style x:Key="FriendListBoxItemStyle" TargetType="ListBoxItem"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
         <Grid Name="RootLayout"> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="0.3*"></ColumnDefinition> 
           <ColumnDefinition Width="*"></ColumnDefinition> 
          </Grid.ColumnDefinitions> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="60"></RowDefinition> 
          </Grid.RowDefinitions> 
          <Image Margin="4,4,4,2" Grid.Column="0"> 
           <Image.Source > 
            <MultiBinding Converter="{StaticResource avatarConverter}"> 
             <Binding Path="ProfilePhoto"></Binding> 
             <Binding Path="StatusInfo.IsLogged"></Binding> 
            </MultiBinding> 
           </Image.Source> 
          </Image> 
          <Grid Grid.Column="1"> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="*"></ColumnDefinition> 
           </Grid.ColumnDefinitions> 
           <Grid.RowDefinitions> 
            <RowDefinition Height="*"></RowDefinition> 
            <RowDefinition Height="*"></RowDefinition> 
            <RowDefinition Height="*"></RowDefinition> 
           </Grid.RowDefinitions> 
           <TextBlock 
             Text="{Binding Path=Nick}" 
             Margin="2,2,2,2" 
             FontSize="13" 
             FontWeight="Medium" 
             Grid.Column="0" Grid.Row="0"> 
           </TextBlock> 
           <TextBlock 
             Text="{Binding Path=StatusMessageInfo.Message}" 
             FontSize="11" 
             FontWeight="Normal" 
             Foreground="DarkGray" 
             Grid.Column="0" Grid.Row="1" Margin="2,2,2,2"></TextBlock> 
           <TextBlock 
             Style="{StaticResource StatusStyle}" 
             Grid.Column="0" Grid.Row="2" > 
            <TextBlock.Text> 
            <MultiBinding Converter="{StaticResource infoConverter}"> 
             <Binding Path="StatusInfo.IsLogged"></Binding> 
             <Binding Path="StatusInfo.IsChating"></Binding> 
             <Binding Path="StatusInfo.RoomName"></Binding> 
            </MultiBinding> 
            </TextBlock.Text> 
           </TextBlock> 
          </Grid> 
         </Grid>       
         <ControlTemplate.Triggers> 
          <MultiTrigger> 
           <MultiTrigger.Conditions> 
            <Condition Property="IsMouseOver" Value="True" /> 
            <Condition Property="IsSelected" Value="False"/> 
           </MultiTrigger.Conditions> 
           <Setter Property="Background" Value="Yellow" /> 
          </MultiTrigger> 
          <Trigger Property="IsSelected" Value="True"> 
           <Setter Property="Background" Value="Red" /> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

auf List-Box-Stil Nehmen:

 <Style x:Key="FriendListBoxStyle" TargetType="{x:Type ListBox}"> 
      <Setter Property="ItemContainerStyle" Value="{DynamicResource FriendListBoxItemStyle}" /> 
     </Style> 

Ein schließlich listbox Stil auf Kontrolle im Hinblick gelten:

<ListBox Name="Friends" 
      SelectedIndex="{Binding Path=SelectedFriendsIndex,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
      SelectedItem="{Binding Path=SelectedFriend, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" 
      Style="{StaticResource FriendListBoxStyle}"/> 

Ich betreibe App und listbox wie hier aussehen:

alt text

Auch Einträge in der Listbox sind nicht auswählbar, daher kann ich keine Einträge in der Listbox auswählen. Was ist schlecht?

Antwort

0

Was Sie getan haben, ist ItemContainerStyle mit ItemTemplate mischen.

Was Sie tun müssen, ist:

  1. Extract ListBoxItem Vorlage Blend oder ShowMeTheTemplate mit und fügen Sie ein Trigger es Hintergrundfarbe zu ändern, wenn es ausgewählt ist.
  2. Verschieben Sie Ihre Datenbindungen in eine DataTemplate zugewiesen ItemTemplate Eigenschaft der ListBox.