UWP ListView 绑定 单击 选中项 颜色

简介: xaml ...

xaml

<Page
    x:Class="SuperTools.Views.BlankPage3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:interop="using:Windows.UI.Xaml.Interop"
    xmlns:local="using:SuperTools.Views"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid Background="Gray">
        <ListView
            Name="listView1"
            IsItemClickEnabled="True"
            ItemClick="listView1_ItemClick">
            <ListView.Resources>
                <Style x:Key="ListViewItemStyle1" TargetType="ListViewItem">
                    <!--HorizontalContentAlignment 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">
                                <ListViewItemPresenter
                                    HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                    CheckBoxBrush="{ThemeResource ListViewItemCheckBoxBrush}"
                                    CheckBrush="{ThemeResource ListViewItemCheckBrush}"
                                    CheckMode="{ThemeResource ListViewItemCheckMode}"
                                    ContentMargin="{TemplateBinding Padding}"
                                    ContentTransitions="{TemplateBinding ContentTransitions}"
                                    Control.IsTemplateFocusTarget="True"
                                    DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
                                    DragBackground="{ThemeResource ListViewItemDragBackground}"
                                    DragForeground="{ThemeResource ListViewItemDragForeground}"
                                    DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
                                    FocusBorderBrush="{ThemeResource ListViewItemFocusBorderBrush}"
                                    FocusSecondaryBorderBrush="{ThemeResource ListViewItemFocusSecondaryBorderBrush}"
                                    FocusVisualMargin="{TemplateBinding FocusVisualMargin}"
                                    FocusVisualPrimaryBrush="{TemplateBinding FocusVisualPrimaryBrush}"
                                    FocusVisualPrimaryThickness="{TemplateBinding FocusVisualPrimaryThickness}"
                                    FocusVisualSecondaryBrush="{TemplateBinding FocusVisualSecondaryBrush}"
                                    FocusVisualSecondaryThickness="{TemplateBinding FocusVisualSecondaryThickness}"
                                    PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackground}"
                                    PointerOverBackground="#FFFFAA"
                                    PointerOverForeground="{ThemeResource ListViewItemForegroundPointerOver}"
                                    PressedBackground="#FFFFAA"
                                    ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}"
                                    SelectedBackground="#FFFFAA"
                                    SelectedForeground="{ThemeResource ListViewItemForegroundSelected}"
                                    SelectedPointerOverBackground="Red"
                                    SelectedPressedBackground="#FFFFAA"
                                    SelectionCheckMarkVisualEnabled="{ThemeResource ListViewItemSelectionCheckMarkVisualEnabled}" />
                                <!--
                                    PointerOverBackground 鼠标移到元素上时;SelectedBackground 元素被选择后;SelectedPressedBackground 点击已被选择元素时;
                                    PressedBackground 点击未被选择的元素时;SelectedPointerOverBackground 鼠标移动已被选择元素上时
                                -->
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListView.Resources>
            <ListView.ItemContainerStyle>
                <StaticResource ResourceKey="ListViewItemStyle1" />
            </ListView.ItemContainerStyle>
            <ItemsPanelTemplate>
                <Grid Background="White" />
            </ItemsPanelTemplate>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid
                        Margin="0,0,0,0"
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch"
                        Background="YellowGreen">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="1" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="1" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <TextBlock
                            x:Name="title_prj"
                            Grid.Column="0"
                            Margin="10"
                            Text="{Binding Path=name1}" />
                        <Border
                            Grid.Column="1"
                            Width="0.5"
                            Background="#808080" />
                        <TextBlock
                            x:Name="title_result"
                            Grid.Column="2"
                            Margin="10"
                            Text="{Binding Path=details}" />
                        <Border
                            Grid.Column="3"
                            Width="0.5"
                            Background="#808080" />
                        <TextBlock
                            x:Name="title_gray"
                            Grid.Column="4"
                            Margin="10"
                            Text="{Binding Path=backgroundcolor}" />

                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>

        </ListView>
    </Grid>
</Page>

 

 

cs

using System.Collections.ObjectModel;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace SuperTools.Views
{
    public sealed partial class BlankPage3 : Page
    { 
        public BlankPage3()
        {
            this.InitializeComponent();
            var categories = new ObservableCollection<dynamic>
            {
                new   {Name="name1",details="color1" ,backgroundcolor="#D90015"},
                new   {Name="name2",details="color2" ,backgroundcolor="#DC1C17"},
                new   {Name="name3",details="cplor3",backgroundcolor="#DE3A17" },
                new   {Name="name3",details="color4",backgroundcolor="#E25819" }
            };
            listView1.ItemsSource = categories;  
        }

        private void listView1_ItemClick(object sender, ItemClickEventArgs e)
        {
            listView1.Background= new SolidColorBrush(Colors.Transparent);
            dynamic clickedItem = e.ClickedItem;
            ListViewItem item = listView1.ContainerFromItem(clickedItem) as ListViewItem;
            if (item != null)
            {
                //item.Background = new SolidColorBrush(Colors.Red);//选中后的元素背景颜色 
            } 
        }
    }
}

 

目录
相关文章
|
Android开发
Android 常见对话框的简单使用(提示信息对话框、单选多选对话框、自定义对话框)
Android 常见对话框的简单使用(提示信息对话框、单选多选对话框、自定义对话框)
297 0
Android 常见对话框的简单使用(提示信息对话框、单选多选对话框、自定义对话框)
|
4月前
|
前端开发
如何为树形菜单项添加图标?
如何为树形菜单项添加图标?
|
6月前
|
前端开发
uniapp checkbox样式失效,选中框选中按钮不显示
uniapp checkbox样式失效,选中框选中按钮不显示
85 0
WPF 获取列表中控件的同时,选中其所在行
WPF 获取列表中控件的同时,选中其所在行
|
存储
PyQt5 技巧篇-复选框绑定行内容,全选、清空、展示选中的内容功能实现演示,设置复选框选中,检查复选框选中状态
PyQt5 技巧篇-复选框绑定行内容,全选、清空、展示选中的内容功能实现演示,设置复选框选中,检查复选框选中状态
432 0
PyQt5 技巧篇-复选框绑定行内容,全选、清空、展示选中的内容功能实现演示,设置复选框选中,检查复选框选中状态
|
搜索推荐
PyQt5 技巧篇-参数控制窗体右上角只显示关闭按钮实例演示
PyQt5 技巧篇-参数控制窗体右上角只显示关闭按钮实例演示
506 0
PyQt5 技巧篇-参数控制窗体右上角只显示关闭按钮实例演示
|
C#
自定义Behavior 实现Listbox自动滚动到选中项
原文:自定义Behavior 实现Listbox自动滚动到选中项 blend为我们提供方便的behavior来扩展我们的控件,写好之后就可以在blend中方便的使用了。 下面是自定义的behavior来实现Listbox自动滚动到选中项 其中this.AssociatedObject为使用该行为的控件。
1006 0
|
C#
wpf datagrid设置右键菜单打开时选中项的背景色
原文:wpf datagrid设置右键菜单打开时选中项的背景色 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huangli321456/article/details/53929433 ...
1404 0