2016-02-29 13 views
5

Có cách nào để thay đổi các thuộc tính của một ListviewItem khi cái này được chọn không?uwp win10 Listview SelectedItem Style

Ví dụ, tôi muốn hình chữ nhật bên trong ListviewItem thành Đỏ khi được chọn và Màu xanh theo mặc định.

Làm thế nào để đạt được điều này một cách thanh lịch?

Trả lời

16

Bạn có thể đặt ListView.ItemContainerStyle để tùy chỉnh kiểu ListViewItems được sử dụng trong ListView.

Trang này hiển thị kiểu mặc định: https://msdn.microsoft.com/en-us/library/windows/apps/mt299136.aspx

Trong trường hợp ví dụ của bạn - bạn sẽ thay đổi Selected~Background thuộc tính trong mã tương tự như dưới đây:

<ListView ...> 
    <ListView.ItemContainerStyle> 
     <Style 
      TargetType="ListViewItem"> 
      <Setter Property="Template"> 
       <Setter.Value> 
    <ControlTemplate TargetType="ListViewItem"> 
     <ListViewItemPresenter 
      ContentTransitions="{TemplateBinding ContentTransitions}" 
      SelectionCheckMarkVisualEnabled="True" 
      CheckBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}" 
      CheckBoxBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}" 
      DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}" 
      DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}" 
      FocusBorderBrush="{ThemeResource SystemControlForegroundAltHighBrush}" 
      FocusSecondaryBorderBrush="{ThemeResource SystemControlForegroundBaseHighBrush}" 
      PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" 
      PointerOverBackground="{ThemeResource SystemControlHighlightListLowBrush}" 
      PointerOverForeground="{ThemeResource SystemControlHighlightAltBaseHighBrush}" 
      SelectedBackground="{ThemeResource SystemControlHighlightListAccentLowBrush}" 
      SelectedForeground="{ThemeResource SystemControlHighlightAltBaseHighBrush}" 
      SelectedPointerOverBackground="{ThemeResource SystemControlHighlightListAccentMediumBrush}" 
      PressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}" 
      SelectedPressedBackground="{ThemeResource SystemControlHighlightListAccentHighBrush}" 
      DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}" 
      DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}" 
      ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}" 
      HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" 
      VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" 
      ContentMargin="{TemplateBinding Padding}" 
      CheckMode="Inline"/> 
    </ControlTemplate> 
+2

Ngoài ra, cách thay đổi BorderBrush cho SelectedItem bằng cách sử dụng kiểu này? –

-2

Tôi đã trả lời câu hỏi này ở nơi khác xin vui lòng kiểm tra xem nó! UWP gridview item selection style

+1

Vâng, điều đó chắc chắn có thể hoạt động, tôi giống như câu trả lời của tôi nhiều hơn và sẽ không giới thiệu bạn. :) –

+0

Cảm ơn bạn đã trả lời, Filip. Tôi đã có xaml chuẩn bị theo cách bạn trình bày nó. Mã của Ricardo đã đẩy tôi thêm một bước nữa để giải quyết vấn đề. Tôi đã bình chọn cả hai câu trả lời, nhưng tôi đã chọn. Một lần nữa cám ơn hai bạn. – phm

Các vấn đề liên quan