Skip to content

Instantly share code, notes, and snippets.

@wonderful-panda
Created January 29, 2015 15:00
Show Gist options
  • Select an option

  • Save wonderful-panda/6a4cdfa1913585af5f85 to your computer and use it in GitHub Desktop.

Select an option

Save wonderful-panda/6a4cdfa1913585af5f85 to your computer and use it in GitHub Desktop.
[WPF]ListView.SelectedItemsとListViewItem.IsSelectedの挙動を確認するテストプログラム
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace WpfTraning
{
public class Item : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string Name { get; private set; }
bool _isSelected = false;
public bool IsSelected
{
get { return _isSelected; }
set
{
if (value == _isSelected)
return;
_isSelected = value;
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
}
}
public Item(string name)
{
this.Name = name;
}
}
public class ViewModel
{
public ObservableCollection<Item> Items { get; private set; }
public ViewModel()
{
this.Items = new ObservableCollection<Item>(
from i in Enumerable.Range(0, 50) select new Item(i.ToString("d2"))
);
}
}
}
<Window x:Class="WpfTraning.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:WpfTraning"
Height="300" Width="400">
<Window.DataContext>
<my:ViewModel/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListView x:Name="lvw" ItemsSource="{Binding Items}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
</GridView>
</ListView.View>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
<ItemsControl Grid.Column="1" Grid.RowSpan="2" ItemsSource="{Binding Items}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Margin="2" Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Margin="4">
<TextBlock Text="{Binding Name}" />
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected}" Value="true">
<Setter Property="Background" Value="Orange" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<StackPanel Grid.Row="1">
<StackPanel Orientation="Horizontal" Margin="5">
<TextBlock Text="SelectedItems.Count:" />
<TextBlock Text="{Binding ElementName=lvw, Path=SelectedItems.Count}" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="5">
<TextBox Name="textBoxName" Width="40" />
<Button Name="ToggleSelection" Click="ToggleSelection_Click">Toggle selection</Button>
</StackPanel>
</StackPanel>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
namespace WpfTraning
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ToggleSelection_Click(object sender, RoutedEventArgs e)
{
foreach (var item in ((ViewModel)this.DataContext).Items.Where(x => x.Name == textBoxName.Text))
item.IsSelected = !item.IsSelected;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment