Quantcast
Channel: かずきのBlog@hatena
Viewing all articles
Browse latest Browse all 1387

UWPでListView/GridViewのItemのIsSelectedプロパティをバインディングする

$
0
0

WPFだとStyleにBinding書けばいけるんですけど、UWPだと出来ない!由々しき事態。ということでUWPだとどうやるかというとListViewとかを拡張してごにょごにょする必要があります。

stackoverflow.com

こんな感じでListViewを拡張します。ListViewItemが作られるときにさくっと手を入れてバインドを作る感じです。

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace App66
{
    publicclass ListViewEx : ListView
    {
        protectedoverridevoid PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            base.PrepareContainerForItemOverride(element, item);
            var binding = new Binding();
            binding.Path = new PropertyPath("IsSelected");
            binding.Mode = BindingMode.TwoWay;
            binding.Source = item;
            ((ListViewItem)element).SetBinding(ListViewItem.IsSelectedProperty, binding);
        }
    }
}

あとは、IsSelectedを持ったクラスのコレクションとItemsSourceをBindingすればOK。

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using Windows.UI.Popups;

namespace App66
{
    publicclass MainPageViewModel
    {
        public ObservableCollection<Person> People { get; } = new ObservableCollection<Person>();

        publicvoid AddPerson()
        {
            this.People.Add(new Person { Name = Guid.NewGuid().ToString() });
        }

        public async void Dump()
        {
            var message = $"{string.Join(", ", this.People.Where(x => x.IsSelected).Select(x => x.Name).ToArray())} selected.";
            var dlg = new MessageDialog(message);
            await dlg.ShowAsync();
        }
    }

    publicclass Person : INotifyPropertyChanged
    {
        publicevent PropertyChangedEventHandler PropertyChanged;

        publicstring Name { get; set; }

        privatestaticreadonly PropertyChangedEventArgs IsSelectedPropertyChangedEventArgs = new PropertyChangedEventArgs(nameof(IsSelected));

        privatebool isSelected;

        publicbool IsSelected
        {
            get { returnthis.isSelected; }
            set
            {
                if (this.isSelected == value) { return; }
                this.isSelected = value;
                this.PropertyChanged?.Invoke(this, IsSelectedPropertyChangedEventArgs);
            }
        }


        publicoverridestring ToString()
        {
            returnthis.Name;
        }
    }
}
<Page x:Class="App66.MainPage"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="using:App66"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:System="using:System"mc:Ignorable="d"><Page.DataContext><local:MainPageViewModel /></Page.DataContext><Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"><Grid.RowDefinitions><RowDefinition Height="Auto" /><RowDefinition /></Grid.RowDefinitions><StackPanel Orientation="Horizontal"><Button Content="Add"Click="{x:Bind ViewModel.AddPerson}" /><Button Content="Dump"Click="{x:Bind ViewModel.Dump}" /></StackPanel><local:ListViewEx x:Name="ListView"Grid.Row="1"SelectionMode="Multiple"ItemsSource="{x:Bind ViewModel.People}"></local:ListViewEx></Grid></Page>

なかなかにメンドイ。


Viewing all articles
Browse latest Browse all 1387

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>