2016-07-21 5 views
0

Ich bin relativ neu in universellen Apps und XAML, und ich versuche, Stile auf meiner Hauptseite anzuwenden. Bisher habe ich erfolgreich einen Stil für ListBoxItem s definiert, aber ich finde keine Möglichkeit, die Hintergrundfarbe zu ändern, wenn ein Element ausgewählt wird.Wie kann ich die Eigenschaften eines ListboxItem ändern, wenn es ausgewählt ist?

Ich weiß, dass es in WPF sehr einfach ist, einen Auslöser zu setzen und die Eigenschaften des Elements zu ändern, wenn ein Ereignis ausgelöst wird, Trigger sind jedoch in Universal Apps nicht verfügbar.

Mein Ziel ist es, die Hintergrundeigenschaft von ListBoxItem auf Grau zu setzen, wenn es ausgewählt ist, aber ich kann keinen Weg finden, dies zu erreichen. Ich habe versucht mit VisualStateManager, aber ich bin mir nicht sicher, wie es funktioniert, und ich habe noch einen funktionierenden Visual State auf etwas anzuwenden.

Kann jemand erklären, wie man VisualStates benutzt, oder schlägt sogar eine alternative Methode vor, um dies zu tun? Hier ist mein Code so weit als Referenz:

<Application 
    x:Class="VSC_QC1.App" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:VSC_QC1" 
    RequestedTheme="Light"> 
    <Application.Resources> 
     <Style x:Key="ListBoxStyle" TargetType="ListBox"> 
      <Setter Property="FontFamily" Value="Tahoma"/> 
      <Setter Property="FontWeight" Value="Bold"/> 
      <Setter Property="Foreground" Value="White"/> 
      <Setter Property="HorizontalAlignment" Value="Center"/> 
      <Setter Property="VerticalAlignment" Value="Center"/> 
      <Setter Property="BorderBrush" Value="Black" /> 
      <Setter Property="BorderThickness" Value="2" /> 
     </Style> 
     <Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem"> 
      <Setter Property="Background"> 
       <Setter.Value> 
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
         <GradientStop Color="#00b300" Offset="0" /> 
         <GradientStop Color="#107028" Offset="1" /> 
        </LinearGradientBrush> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Application.Resources> 
</Application> 

Hier ist mein Code für die List-Box selbst (in MainPage.xaml) mit der Steuerungsvorlage:

 <ListBox 
     x:Name="LightSelector" 
     Grid.Column="2" 
     Grid.Row="0" 
     Width="300" 
     Style="{StaticResource ListBoxStyle}" 
     FontSize="30" Background="#FFC8C8C8" 
     VerticalAlignment="Top" 
     HorizontalAlignment="Center" 
     SelectionChanged="LightSelector_SelectionChanged"> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="ListBoxItem"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="ListViewItem"> 
          <ListViewItemPresenter   
           PointerOverBackground="{ThemeResource SystemControlHighlightListLowBrush}"  
           SelectedBackground="{ThemeResource SystemControlHighlightListMediumBrush}"  
           SelectedPointerOverBackground="{ThemeResource SystemControlHighlightListMediumBrush}" 
           PressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}" 
           SelectedPressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}" /> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </ListBox.ItemContainerStyle> 
     <ListBoxItem>White flood</ListBoxItem> 
     <ListBoxItem>UV flood</ListBoxItem> 
     <ListBoxItem>IR flood</ListBoxItem> 
     <ListBoxItem>White oblique</ListBoxItem> 
     <ListBoxItem>IR oblique</ListBoxItem> 
     <ListBoxItem>Coaxial</ListBoxItem> 
     <ListBoxItem>Backlight panel</ListBoxItem> 
    </ListBox> 

Antwort

0
<page> 

<Page.Resources> 
     <Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem"> 
      <Setter Property="Background" Value="Transparent"/> 
      <Setter Property="TabNavigation" Value="Local"/> 
      <Setter Property="Padding" Value="12,11,12,13"/> 
      <Setter Property="HorizontalContentAlignment" Value="Left"/> 
      <Setter Property="UseSystemFocusVisuals" Value="True"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ListBoxItem"> 
         <Grid x:Name="LayoutRoot" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"> 
          <Grid.Resources> 
           <Style x:Key="BaseContentPresenterStyle" TargetType="ContentPresenter"> 
            <Setter Property="FontFamily" Value="Segoe UI"/> 
            <Setter Property="FontWeight" Value="SemiBold"/> 
            <Setter Property="FontSize" Value="15"/> 
            <Setter Property="TextWrapping" Value="Wrap"/> 
            <Setter Property="LineStackingStrategy" Value="MaxHeight"/> 
            <Setter Property="TextLineBounds" Value="Full"/> 
            <Setter Property="OpticalMarginAlignment" Value="TrimSideBearings"/> 
           </Style> 
           <Style x:Key="BodyContentPresenterStyle" BasedOn="{StaticResource BaseContentPresenterStyle}" TargetType="ContentPresenter"> 
            <Setter Property="FontWeight" Value="Normal"/> 
            <Setter Property="FontSize" Value="15"/> 
           </Style> 
          </Grid.Resources> 
          <VisualStateManager.VisualStateGroups> 
           <VisualStateGroup x:Name="CommonStates"> 
            <VisualState x:Name="Normal"/> 
            <VisualState x:Name="Disabled"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseLowBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="PointerOver"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="PressedBackground"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListLowBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="Pressed"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="PressedBackground"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListMediumBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="Selected"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="PressedBackground"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListMediumBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="SelectedUnfocused"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="PressedBackground"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListLowBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="SelectedPointerOver"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="PressedBackground"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListMediumBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="SelectedPressed"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="PressedBackground"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListMediumBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
           </VisualStateGroup> 
          </VisualStateManager.VisualStateGroups> 
          <Rectangle x:Name="PressedBackground" Fill="Transparent" Control.IsTemplateFocusTarget="True"/> 
          <ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Style="{StaticResource BodyContentPresenterStyle}" TextWrapping="NoWrap" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Page.Resources> 

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
     <ListBox 
     x:Name="LightSelector" 
     Grid.Column="2" 
     Grid.Row="0" 
     Width="300" 
     FontSize="30" Background="#FFC8C8C8" 
     VerticalAlignment="Top" 
     HorizontalAlignment="Center" 
     > 



      <ListBoxItem>White flood</ListBoxItem> 
      <ListBoxItem>UV flood</ListBoxItem> 
      <ListBoxItem>IR flood</ListBoxItem> 
      <ListBoxItem Style="{StaticResource ListBoxItemStyle1}">White oblique</ListBoxItem> 
      <ListBoxItem>IR oblique</ListBoxItem> 
      <ListBoxItem>Coaxial</ListBoxItem> 
      <ListBoxItem>Backlight panel</ListBoxItem> 

     </ListBox> 
    </Grid> 
</page> 

einfach den Code in Ihre Seite setzen, wo das Listenfeld defiene.I nur den Stil in weiß schräg pur. Sie können angeben, wo Sie möchten.

+0

also, Sie möchten, dass ich die Standardvorlage mit visuellen Zuständen benutze und sie einfach so anpasse? –

+0

Das hat funktioniert, der weiße schräge wird grau, wenn ausgewählt, aber alle anderen gehen standardmäßig violett –

+0

ich fand auch gerade das, was nützlich ist zu wissen http://stackoverflow.com/questions/19320208/change-background-color-of -selected-item-in-listbox? noredirekt = 1 & lq = 1 –

1

Hallo einfach diesen Code setzen in App. xAML,

**<SolidColorBrush x:Key="SystemControlHighlightListAccentLowBrush" Color="Gray" />** 

wenn Sie ein beliebiges Element in Listbox ausgewählt, dass die Zeit Ihre Listbox-Element Farbe wird an Gary geändert werden.

+0

"Der Typ 'Anwendung' unterstützt keinen direkten Inhalt." –

+0

Jetzt sagt es mir, die Eigenschaft Ressourcen können nur einmal gesetzt werden –

0
<Application.Resources> 

     <ResourceDictionary> 

       <SolidColorBrush x:Key="SystemControlHighlightListAccentLowBrush" Color="Gray" /> 

     </ResourceDictionary> 


     </Application.Resources> 

Verwendung dieser Code in app.xaml und auch diesen Link folgen Change background color of selected item in listbox

+0

Bitte geben Sie Ihren Code. –

+0

Sehen Sie den Screenshot ich hinzugefügt, auf die Frage –

+0

soweit ich weiß ... Ich habe nur die Eigenschaft 'Ressourcen' einmal gesetzt –