2017-04-18 54 views
1

的ListViewItem的風格着扳機未選定,但可以觸發選擇。的ListViewItem的風格着扳機未選擇

我想看到的東西表明,當選擇的ListView項藏時未選擇。

但我可以看到它被顯示,但無法看到它被隱藏。

我複製樣式從https://msdn.microsoft.com/en-us/library/windows/apps/mt299136.aspx

而且我確信我將SelectionMode是單身。

,我從複製的代碼是

<Style x:Key="ListViewItemStyle" TargetType="ListViewItem"> 
      <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/> 
      <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/> 
      <Setter Property="Background" Value="{ThemeResource ListViewItemBackground}"/> 
      <Setter Property="Foreground" Value="{ThemeResource ListViewItemForeground}"/> 
      <Setter Property="TabNavigation" Value="Local"/> 
      <Setter Property="IsHoldingEnabled" Value="True"/> 
      <Setter Property="Padding" Value="12,0,12,0"/> 
      <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
      <Setter Property="VerticalContentAlignment" Value="Center"/> 
      <Setter Property="MinWidth" Value="{ThemeResource ListViewItemMinWidth}"/> 
      <Setter Property="MinHeight" Value="{ThemeResource ListViewItemMinHeight}"/> 
      <Setter Property="AllowDrop" Value="False"/> 
      <Setter Property="UseSystemFocusVisuals" Value="True"/> 
      <Setter Property="FocusVisualMargin" Value="0"/> 
      <Setter Property="FocusVisualPrimaryBrush" Value="{ThemeResource ListViewItemFocusVisualPrimaryBrush}"/> 
      <Setter Property="FocusVisualPrimaryThickness" Value="2"/> 
      <Setter Property="FocusVisualSecondaryBrush" Value="{ThemeResource ListViewItemFocusVisualSecondaryBrush}"/> 
      <Setter Property="FocusVisualSecondaryThickness" Value="1"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ListViewItem"> 
         <Grid> 
          <ContentPresenter ></ContentPresenter> 
          <Button x:Name="b" Opacity="0"></Button> 
          <VisualStateManager.VisualStateGroups> 
           <VisualStateGroup x:Name="SelectionStates"> 
            <VisualState x:Name="Unselecting"> 
             <Storyboard BeginTime="0:0:0"> 
              <DoubleAnimation Storyboard.TargetName="b" 
                Storyboard.TargetProperty="Opacity" 
                Duration="0:0:0.1" 
                To="0" /> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="Unselected"> 
             <Storyboard BeginTime="0:0:0"> 
              <DoubleAnimation Storyboard.TargetName="b" 
                Storyboard.TargetProperty="Opacity" 
                Duration="0" 
                To="0" /> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="Selected"> 
             <Storyboard BeginTime="0:0:0"> 
              <DoubleAnimation Storyboard.TargetName="b" 
                Storyboard.TargetProperty="Opacity" 
                Duration="0" 
                To="1" /> 
             </Storyboard> 
            </VisualState> 
           </VisualStateGroup> 
          </VisualStateManager.VisualStateGroups> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 

      </Setter> 

     </Style> 

我看到這個答案https://stackoverflow.com/a/36010296/6116637這阿奇納提供使用CustomUnselected和的SelectionChanged時改變它的方法。

這是一個很好的方式,但我不想寫相同的代碼來很多地方,我還有很多的ListView應該做的it.Are有沒有什麼好的辦法做到這一點只寫XAML?

我已經看到,說結合IsSelected答案,但不能做它的TemplateBinding,當我想能見度爲TemplateBinding不能使用轉換。

而當我使用Visibility="{Binding Path={TemplateBinding IsSelected},Converter={StaticResource BooleanVisibility},Mode=OneWay}"Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected,Converter={StaticResource BooleanInverseVisibility},Mode=OneWay}",結果不是我想要的。

參見:https://stackoverflow.com/a/40328520/6116637

+0

我也看到https://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj709921.aspx?f=255&MSPPError=-2147217396 – lindexi

+0

見http://stackoverflow.com/questions/40327901/how-to-bind-isselected-property-in-datatemplate-of-listviewitem我不想將它綁定到IsSelected。 – lindexi

+0

你可以發佈數據模板代碼嗎?你能告訴你想要展示什麼嗎? – Archana

回答

1

創建逆知名度轉換器:

public class BooleanInverseVisibilityConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, string language) 
    { 
     return (bool)value ? Visibility.Collapsed : Visibility.Visible; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, string language) 
    { 
     return (Visibility)value != Visibility.Visible; 
    } 
} 

這崩潰的時候它是真實的,並顯示當它是假的。

Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected,Converter={StaticResource BooleanInverseVisibility},Mode=OneWay}" 
+0

對不起,但轉換中的轉折點永遠不會輸入。 – lindexi

+0

剛剛意識到我複製/粘貼了你的代碼,這是不正確的。請查看更新的'{Binding ..}'示例。 – Laith

+0

該代碼是失去了轉換,就像你support.Thx你的答覆,但你的代碼不能工作太。我知道RelativeSource可以綁定到TemplatedParent,但它不是我想要的。參見http://stackoverflow.com/questions/40327901 /如何對結合 - isselected屬性合的DataTemplate-的-ListViewItem的 – lindexi