2012-06-28 55 views
0

我正在爲ListBox創建一個自定義的控件模板,而且我對視覺狀態有困難。當使用VisualState MouseOver時,它會影響包括所選項目在內的所有項目,我希望使用獨立的樣式。C#VisualStateManager - WPF中SelectedPointerOver(Metro)中的等效參數是什麼

在Metro中,有SelectedPointerOver,在WPF或其他選擇中是否有任何等價物?

編輯:

例如,所有項目初步具備黑色前景。

當選擇一個項目時,其前景變爲白色(並且未選擇的項目保持黑色前景)。

現在,當我MouseOver非選擇的項目,我會喜歡它的前景變成藍色,當我MouseOver選定的項目,我會喜歡它的前景變成紅色。

+0

對於哪個控件你想SelectedPointerOver狀態...你的意思是任何屬性? –

回答

1

據我所知,在WPF中沒有相應的狀態。

的WPF ListBoxItem的具有狀態Unselected,在組SelectionStatesCommonStatesSelectedSelectedUnfocused和各州NormalMouseOverDisabled。各組中的國家相互排斥,但可以同時採用不同組別的國家。 ListBoxItem可以例如同時處於狀態SelectedMouseOver

由控制模板來定義考慮到這一事實的可視化。例如,它可以在選中時使用不同的背景畫筆填充項目,並且當鼠標懸停在項目上時繪製外部邊框。重要的是,因爲相關狀態是獨立的並且可以同時進行,所以獨立可視化同時可見。只有當選定的列表項目沒有被選中時,用戶通常會變得有點困惑,因爲他將鼠標移動到該項目上。

編輯 - 列表框例如下。使用觸發器而不是視覺狀態可能會更容易。

<ListBox SelectionMode="Extended"> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ListBoxItem"> 
         <Grid Background="LightGray" Margin="1"> 
          <VisualStateManager.VisualStateGroups> 
           <VisualStateGroup x:Name="CommonStates"> 
            <VisualState x:Name="Normal"/> 
            <VisualState x:Name="Disabled"/> 
            <VisualState x:Name="MouseOver"> 
             <Storyboard> 
              <ColorAnimation Storyboard.TargetName="UnselectedText" 
                  Storyboard.TargetProperty="Foreground.Color" 
                  To="Blue" Duration="0:0:0.1"/> 
              <ColorAnimation Storyboard.TargetName="SelectedText" 
                  Storyboard.TargetProperty="Foreground.Color" 
                  To="Red" Duration="0:0:0.1"/> 
             </Storyboard> 
            </VisualState> 
           </VisualStateGroup> 
           <VisualStateGroup x:Name="SelectionStates"> 
            <VisualState x:Name="Unselected"/> 
            <VisualState x:Name="Selected"> 
             <Storyboard> 
              <DoubleAnimation Storyboard.TargetName="UnselectedText" 
                  Storyboard.TargetProperty="Opacity" 
                  To="0" Duration="0:0:0.1"/> 
              <DoubleAnimation Storyboard.TargetName="SelectedText" 
                  Storyboard.TargetProperty="Opacity" 
                  To="1" Duration="0:0:0.1"/> 
             </Storyboard> 
            </VisualState> 
           </VisualStateGroup> 
          </VisualStateManager.VisualStateGroups> 
          <TextBlock Name="UnselectedText" Margin="2" Text="{TemplateBinding Content}"> 
           <TextBlock.Foreground> 
            <SolidColorBrush Color="Black"/> 
           </TextBlock.Foreground> 
          </TextBlock> 
          <TextBlock Name="SelectedText" Margin="2" Text="{TemplateBinding Content}" Opacity="0"> 
           <TextBlock.Foreground> 
            <SolidColorBrush Color="White"/> 
           </TextBlock.Foreground> 
          </TextBlock> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ListBox.ItemContainerStyle> 
    <ListBox.Items> 
     <ListBoxItem Content="Item 1"/> 
     <ListBoxItem Content="Item 2"/> 
     <ListBoxItem Content="Item 3"/> 
     <ListBoxItem Content="Item 4"/> 
     <ListBoxItem Content="Item 5"/> 
     <ListBoxItem Content="Item 6"/> 
     <ListBoxItem Content="Item 7"/> 
     <ListBoxItem Content="Item 8"/> 
     <ListBoxItem Content="Item 9"/> 
     <ListBoxItem Content="Item 10"/> 
    </ListBox.Items> 
</ListBox> 
+0

我實際上更感興趣的是知道是否可以允許基於IsSelected屬性的可視化變體 – Wee

+0

這正是「Selected」和「Unselected」視覺狀態的製作原因。有關處理這些狀態的示例ControlTemplate,請參見[ListBox樣式和模板](http://msdn.microsoft.com/zh-cn/library/ms754242(v = vs.100))。 – Clemens

+0

讓我澄清一下自己。即使MouseOver發生,我也會喜歡所選項目和非選定項目以不同方式更改其樣式。例如,所有項目最初都有黑色的前景。當選擇一個項目時,其前景變爲白色。現在,當我將MouseOver作爲未選擇的項目時,我會喜歡它的前景變成藍色,並且當我MouseOver選定的項目時,我會喜歡它的前景變成紅色。我希望現在更清楚。 – Wee

相關問題