2011-11-14 28 views
1

我想在XAML中爲CornerRadius添加ListView的樣式我有一種方法,但這在我的情況下不起作用。 喜歡這個。如何在XAML中添加ListView CornerRadius

    <ListView.Style> 
        <Style TargetType="{x:Type ListView}"> 
         <Setter Property="BorderBrush" Value="White"/> 
         <Setter Property="BorderThickness" Value="0"/> 
         <Setter Property="Margin" Value="0"/> 
         <Setter Property="OverridesDefaultStyle" Value="true"/> 
         <Setter Property="Template"> 
          <Setter.Value> 
           <ControlTemplate TargetType="{x:Type ListView}"> 
            <Border CornerRadius="5"> 
            </Border> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 

         <!-- here we go --> 
         <Style.Resources> 
          <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Pink"/> 
          <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Pink"/> 
         </Style.Resources> 
        </Style> 

       </ListView.Style> 

這將不會顯示我的ItemsSource的數據,所以我的Listview是這樣的。

   <ListView x:Name="MenuBarList" 
       ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
       Height="{Binding MainMenuHeight}" 
       Width="{Binding MainMenuWidth}" 
       ItemsSource="{Binding}" 
         Foreground="White" 
       Background="#FF3D61D0" 
       SelectionMode="Single"> 

所以我如何添加這種風格。 謝謝.. ..

回答

1

您的模板應包含一個ItemsPresenter,以便ListView知道在哪裏顯示項目。該ItemsPresenter通常ScrollViewer裏面是讓它滾動:

    ... 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="{x:Type ListView}"> 
           <Border CornerRadius="5"> 
            <ScrollViewer> 
             <ItemsPresenter /> 
            </ScrollViewer> 
           </Border> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
        ... 

更簡單的解決方法是編輯default template的副本:

  <ControlTemplate TargetType="{x:Type ListView}"> 
       <Border Name="Bd" 
         CornerRadius="5" 
         Background="{TemplateBinding Background}" 
         BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}" 
         SnapsToDevicePixels="true"> 
        <ScrollViewer Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}" 
            Padding="{TemplateBinding Padding}"> 
         <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
        </ScrollViewer> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsGrouping" 
          Value="true"> 
         <Setter Property="ScrollViewer.CanContentScroll" 
           Value="false"/> 
        </Trigger> 
        <Trigger Property="IsEnabled" 
          Value="false"> 
         <Setter TargetName="Bd" 
           Property="Background" 
           Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate>