【问题标题】:How to change of selected item from listview in uwp?如何从 uwp 的列表视图中更改所选项目?
【发布时间】:2018-10-26 14:08:22
【问题描述】:

如何在 UWP 中完成以下任务?

  1. ListView 中仅选择一项
  2. 将所选项目的颜色从蓝色更改为红色
  3. 访问所选项目的所有属性

【问题讨论】:

  • 你的问题解决了吗?

标签: listview uwp


【解决方案1】:
  1. 设置列表视图控件的 SelectionMode="Single"
  2. // 需要更多信息..
  3. 设置 IsItemClickEnabled="true" 并制作 ItemClick 事件处理程序

在后面的代码中

private void ListView_ItemClick(object sender, ItemClickEventArgs e)
    {
        // retrieve all properties from e.ClickedItem
        // cast it to your model class
        var obj = e.ClickedItem; 
    }

【讨论】:

  • 如何改变选中项的颜色?
  • 如果你从来没有自定义你的列表视图,它可以改变颜色。
【解决方案2】:
  1. 将所选项目的颜色从蓝色更改为红色

Ashiq Hassan 已回答您的第 1 和第 3 个问题。我只是补充回答你的第二个问题。

将所选项目的背景颜色从蓝色更改为红色。您可以直接更改ListViewItem styles and templates。它比在代码隐藏中更改它更简单。只需在 ListViewItem 的 ControlTemplate 中设置ListViewItemPresenterSelectedBackground 属性即可。

<Page.Resources>
    <x:Double x:Key="ListViewItemContentOffsetX">-40.5</x:Double>
    <x:Double x:Key="ListViewItemDisabledThemeOpacity">0.55</x:Double>
    <x:Double x:Key="ListViewItemDragThemeOpacity">0.60</x:Double>
    <x:Double x:Key="ListViewItemReorderHintThemeOffset">10.0</x:Double>
    <x:Double x:Key="ListViewItemSelectedBorderThemeThickness">4</x:Double>
    <x:Double x:Key="ListViewItemMinWidth">88</x:Double>
    <x:Double x:Key="ListViewItemMinHeight">44</x:Double>
    <Thickness x:Key="ListViewItemCompactSelectedBorderThemeThickness">4</Thickness>

    <Style TargetType="ListViewItem">
        <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
        <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
        <Setter Property="TabNavigation" Value="Local"/>
        <Setter Property="IsHoldingEnabled" Value="True"/>
        <Setter Property="Padding" Value="12,0,12,0"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="MinWidth" Value="{ThemeResource ListViewItemMinWidth}"/>
        <Setter Property="MinHeight" Value="{ThemeResource ListViewItemMinHeight}"/>
        <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="Red"
      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>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>

<Grid>
    <ListView>
        <ListViewItem>abc</ListViewItem>
        <ListViewItem>def</ListViewItem>
        <ListViewItem>ghi</ListViewItem>
    </ListView>
</Grid>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    相关资源
    最近更新 更多