作成日: 2015/10/21 最終更新日: 2015/10/21
文書種別
使用方法
詳細
C1DataGridでカスタムフィルタを実装するには、カスタムフィルタ用のユーザーコントロールを作成し、そのユーザーコントロールをC1DataGridのフィルタに設定します。
リストボックスにて条件を指定するカスタムフィルタの実装例を以下に示します。
また、製品付属サンプルのDataGridSamplesにも、いくつかのカスタムフィルタの実装例をご紹介しています。
◎サンプルコード(XAML)
◇MainWindow.xaml
◇CustomFilter.xaml
◎サンプルコード(Visual Basic)
◇MainWindow.xaml.vb
◇CustomFilter.xaml.vb
◎サンプルコード(C#)
◇MainWindow.xaml.cs
◇CustomFilter.xaml.cs
リストボックスにて条件を指定するカスタムフィルタの実装例を以下に示します。
また、製品付属サンプルのDataGridSamplesにも、いくつかのカスタムフィルタの実装例をご紹介しています。
◎サンプルコード(XAML)
◇MainWindow.xaml
<Grid>
<c1:C1DataGrid x:Name="c1DataGrid1" ItemsSource="{Binding}" AutoGenerateColumns="False">
<c1:C1DataGrid.Columns>
<c1:DataGridTextColumn Header="Name" Binding="{Binding Name}"
FilterLoading="DataGridNameColumn_FilterLoading">
<c1:DataGridTextColumn.Filter>
<c1:DataGridContentFilter>
<c1:DataGridFilterList>
<local:CustomFilter/>
</c1:DataGridFilterList>
</c1:DataGridContentFilter>
</c1:DataGridTextColumn.Filter>
</c1:DataGridTextColumn>
</c1:C1DataGrid.Columns>
</c1:C1DataGrid>
</Grid>
<c1:C1DataGrid x:Name="c1DataGrid1" ItemsSource="{Binding}" AutoGenerateColumns="False">
<c1:C1DataGrid.Columns>
<c1:DataGridTextColumn Header="Name" Binding="{Binding Name}"
FilterLoading="DataGridNameColumn_FilterLoading">
<c1:DataGridTextColumn.Filter>
<c1:DataGridContentFilter>
<c1:DataGridFilterList>
<local:CustomFilter/>
</c1:DataGridFilterList>
</c1:DataGridContentFilter>
</c1:DataGridTextColumn.Filter>
</c1:DataGridTextColumn>
</c1:C1DataGrid.Columns>
</c1:C1DataGrid>
</Grid>
◇CustomFilter.xaml
<UserControl.Resources>
<DataTemplate x:Key="itemTemplate">
<CheckBox Content="{Binding Data.Name}" IsThreeState="False"
IsChecked="{Binding IsChecked, Mode=TwoWay}"/>
</DataTemplate>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<CheckBox x:Name="checkBox1" Content="すべて選択" Grid.Row="0" Margin="5"
IsChecked="{Binding SelectAll, Mode=TwoWay}" />
<ListBox x:Name="listBox1" Grid.Row="1" ItemTemplate="{StaticResource itemTemplate}" />
</Grid>
<DataTemplate x:Key="itemTemplate">
<CheckBox Content="{Binding Data.Name}" IsThreeState="False"
IsChecked="{Binding IsChecked, Mode=TwoWay}"/>
</DataTemplate>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<CheckBox x:Name="checkBox1" Content="すべて選択" Grid.Row="0" Margin="5"
IsChecked="{Binding SelectAll, Mode=TwoWay}" />
<ListBox x:Name="listBox1" Grid.Row="1" ItemTemplate="{StaticResource itemTemplate}" />
</Grid>
◎サンプルコード(Visual Basic)
◇MainWindow.xaml.vb
Class MainWindow
Public Sub New()
InitializeComponent()
DataContext = SampleData.GetSampleData()
End Sub
Private Sub DataGridNameColumn_FilterLoading(sender As Object, e As DataGridFilterLoadingEventArgs)
Dim filter = TryCast(DirectCast(DirectCast(e.Filter, DataGridContentFilter).Content, DataGridFilterList).Items(0), CustomFilter)
If Not IsNothing(filter) Then
Dim source = From data In TryCast(DataContext, IEnumerable).Cast(Of SampleData)()
Select New ItemElement(data)
filter.Source = source.ToList()
End If
End Sub
End Class
Public Class SampleData
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(value As String)
If _name <> value Then
_name = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Name"))
End If
End Set
End Property
Public Shared Function GetSampleData() As ObservableCollection(Of SampleData)
Dim list As ObservableCollection(Of SampleData) = New ObservableCollection(Of SampleData)()
list.Add(New SampleData() With {.Name = "Suzuki"})
list.Add(New SampleData() With {.Name = "Tanaka"})
list.Add(New SampleData() With {.Name = "Hashimoto"})
list.Add(New SampleData() With {.Name = "Ueda"})
list.Add(New SampleData() With {.Name = "Yokoyama"})
Return list
End Function
End Class
Public Sub New()
InitializeComponent()
DataContext = SampleData.GetSampleData()
End Sub
Private Sub DataGridNameColumn_FilterLoading(sender As Object, e As DataGridFilterLoadingEventArgs)
Dim filter = TryCast(DirectCast(DirectCast(e.Filter, DataGridContentFilter).Content, DataGridFilterList).Items(0), CustomFilter)
If Not IsNothing(filter) Then
Dim source = From data In TryCast(DataContext, IEnumerable).Cast(Of SampleData)()
Select New ItemElement(data)
filter.Source = source.ToList()
End If
End Sub
End Class
Public Class SampleData
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(value As String)
If _name <> value Then
_name = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Name"))
End If
End Set
End Property
Public Shared Function GetSampleData() As ObservableCollection(Of SampleData)
Dim list As ObservableCollection(Of SampleData) = New ObservableCollection(Of SampleData)()
list.Add(New SampleData() With {.Name = "Suzuki"})
list.Add(New SampleData() With {.Name = "Tanaka"})
list.Add(New SampleData() With {.Name = "Hashimoto"})
list.Add(New SampleData() With {.Name = "Ueda"})
list.Add(New SampleData() With {.Name = "Yokoyama"})
Return list
End Function
End Class
◇CustomFilter.xaml.vb
Public Class CustomFilter
Inherits UserControl
Implements IDataGridFilterUnity
Private _FilterSettings As Boolean = False
Private _Source As List(Of ItemElement) = Nothing
Private _State As DataGridFilterState = Nothing
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Sub New()
InitializeComponent()
listBox1.SetBinding(C1TreeView.ItemsSourceProperty, New Binding("Source") With {.Source = Me})
End Sub
Public Property SelectAll As Boolean?
Get
If Not IsNothing(Me.Source) AndAlso Me.Source.All(Function(g) g.IsChecked) Then
Return True
ElseIf Not IsNothing(Me.Source) AndAlso Me.Source.All(Function(g) Not g.IsChecked) Then
Return False
Else
Return Nothing
End If
End Get
Set(value As Boolean?)
Dim newValue As Boolean = If(CType(value, System.Nullable(Of Boolean)), False)
For Each p In Me.Source
p.IsChecked = newValue
Next
End Set
End Property
Public Property Filter As DataGridFilterState Implements IDataGridFilterUnity.Filter
Get
Return _State
End Get
Set(value As DataGridFilterState)
If _State IsNot value Then
_State = value
Me.SetFilterState(value)
End If
End Set
End Property
Public Property Source As List(Of ItemElement)
Get
Return _Source
End Get
Set(value As List(Of ItemElement))
If _Source IsNot value Then
If Not IsNothing(_Source) Then
For Each p In _Source
RemoveHandler p.PropertyChanged, New PropertyChangedEventHandler(AddressOf Me.ItemPropertyChanged)
Next
End If
If Not IsNothing(value) Then
For Each p In value
AddHandler p.PropertyChanged, New PropertyChangedEventHandler(AddressOf Me.ItemPropertyChanged)
Next
End If
_Source = value
Me.OnPropertyChanged("Source")
End If
End Set
End Property
Private Sub ItemPropertyChanged(sender As Object, e As PropertyChangedEventArgs)
If Not _FilterSettings Then
_State = Me.GetFilterState()
Me.OnPropertyChanged("Filter")
End If
End Sub
Protected Overloads Sub OnPropertyChanged(propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Private Function GetFilterState() As DataGridFilterState
Me.OnPropertyChanged("SelectAll")
If Me.Source.All(Function(g) g.IsChecked) Then
Return New DataGridFilterState() With {
.FilterInfo = New List(Of DataGridFilterInfo)() From {
New DataGridFilterInfo() With {
.FilterOperation = DataGridFilterOperation.All,
.FilterType = DataGridFilterType.MultiValue
}
}
}
ElseIf Source.All(Function(g) Not g.IsChecked) Then
Return New DataGridFilterState() With {
.FilterInfo = New List(Of DataGridFilterInfo)() From {
New DataGridFilterInfo() With {
.FilterOperation = DataGridFilterOperation.None,
.FilterType = DataGridFilterType.MultiValue
}
}
}
Else
Dim checkedItems = Source.FindAll(Function(g) g.IsChecked).Select(Function(pe) pe.Data.Name).ToList()
Return New DataGridFilterState() With {
.FilterInfo = New List(Of DataGridFilterInfo)() From {
New DataGridFilterInfo() With {
.FilterOperation = DataGridFilterOperation.IsOneOf,
.FilterType = DataGridFilterType.MultiValue,
.Value = checkedItems
}
}
}
End If
End Function
Private Sub SetFilterState(filter As DataGridFilterState)
Me._FilterSettings = True
If Not IsNothing(filter) AndAlso
Not IsNothing(filter.FilterInfo) AndAlso
filter.FilterInfo.Count > 0 AndAlso
Not IsNothing(filter.FilterInfo(0)) AndAlso
filter.FilterInfo(0).FilterOperation = DataGridFilterOperation.IsOneOf AndAlso
TypeOf filter.FilterInfo(0).Value Is List(Of ItemElement) Then
For Each p In Me.Source
p.IsChecked = False
Next
For Each p In DirectCast(filter.FilterInfo(0).Value, List(Of ItemElement))
p.IsChecked = True
Next
Else
For Each p In Me.Source
p.IsChecked = True
Next
End If
_FilterSettings = False
End Sub
End Class
Public Class ItemElement
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Sub New(data As SampleData)
Me.Data = data
End Sub
Private _Data As SampleData
Public Property Data() As SampleData
Get
Return _Data
End Get
Set(value As SampleData)
_Data = value
End Set
End Property
Private _IsChecked As String = False
Public Property IsChecked() As Boolean
Get
Return _IsChecked
End Get
Set(value As Boolean)
If _IsChecked <> value Then
_IsChecked = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("IsChecked"))
End If
End Set
End Property
Protected Sub OnPropertyChanged(propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
Inherits UserControl
Implements IDataGridFilterUnity
Private _FilterSettings As Boolean = False
Private _Source As List(Of ItemElement) = Nothing
Private _State As DataGridFilterState = Nothing
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Sub New()
InitializeComponent()
listBox1.SetBinding(C1TreeView.ItemsSourceProperty, New Binding("Source") With {.Source = Me})
End Sub
Public Property SelectAll As Boolean?
Get
If Not IsNothing(Me.Source) AndAlso Me.Source.All(Function(g) g.IsChecked) Then
Return True
ElseIf Not IsNothing(Me.Source) AndAlso Me.Source.All(Function(g) Not g.IsChecked) Then
Return False
Else
Return Nothing
End If
End Get
Set(value As Boolean?)
Dim newValue As Boolean = If(CType(value, System.Nullable(Of Boolean)), False)
For Each p In Me.Source
p.IsChecked = newValue
Next
End Set
End Property
Public Property Filter As DataGridFilterState Implements IDataGridFilterUnity.Filter
Get
Return _State
End Get
Set(value As DataGridFilterState)
If _State IsNot value Then
_State = value
Me.SetFilterState(value)
End If
End Set
End Property
Public Property Source As List(Of ItemElement)
Get
Return _Source
End Get
Set(value As List(Of ItemElement))
If _Source IsNot value Then
If Not IsNothing(_Source) Then
For Each p In _Source
RemoveHandler p.PropertyChanged, New PropertyChangedEventHandler(AddressOf Me.ItemPropertyChanged)
Next
End If
If Not IsNothing(value) Then
For Each p In value
AddHandler p.PropertyChanged, New PropertyChangedEventHandler(AddressOf Me.ItemPropertyChanged)
Next
End If
_Source = value
Me.OnPropertyChanged("Source")
End If
End Set
End Property
Private Sub ItemPropertyChanged(sender As Object, e As PropertyChangedEventArgs)
If Not _FilterSettings Then
_State = Me.GetFilterState()
Me.OnPropertyChanged("Filter")
End If
End Sub
Protected Overloads Sub OnPropertyChanged(propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Private Function GetFilterState() As DataGridFilterState
Me.OnPropertyChanged("SelectAll")
If Me.Source.All(Function(g) g.IsChecked) Then
Return New DataGridFilterState() With {
.FilterInfo = New List(Of DataGridFilterInfo)() From {
New DataGridFilterInfo() With {
.FilterOperation = DataGridFilterOperation.All,
.FilterType = DataGridFilterType.MultiValue
}
}
}
ElseIf Source.All(Function(g) Not g.IsChecked) Then
Return New DataGridFilterState() With {
.FilterInfo = New List(Of DataGridFilterInfo)() From {
New DataGridFilterInfo() With {
.FilterOperation = DataGridFilterOperation.None,
.FilterType = DataGridFilterType.MultiValue
}
}
}
Else
Dim checkedItems = Source.FindAll(Function(g) g.IsChecked).Select(Function(pe) pe.Data.Name).ToList()
Return New DataGridFilterState() With {
.FilterInfo = New List(Of DataGridFilterInfo)() From {
New DataGridFilterInfo() With {
.FilterOperation = DataGridFilterOperation.IsOneOf,
.FilterType = DataGridFilterType.MultiValue,
.Value = checkedItems
}
}
}
End If
End Function
Private Sub SetFilterState(filter As DataGridFilterState)
Me._FilterSettings = True
If Not IsNothing(filter) AndAlso
Not IsNothing(filter.FilterInfo) AndAlso
filter.FilterInfo.Count > 0 AndAlso
Not IsNothing(filter.FilterInfo(0)) AndAlso
filter.FilterInfo(0).FilterOperation = DataGridFilterOperation.IsOneOf AndAlso
TypeOf filter.FilterInfo(0).Value Is List(Of ItemElement) Then
For Each p In Me.Source
p.IsChecked = False
Next
For Each p In DirectCast(filter.FilterInfo(0).Value, List(Of ItemElement))
p.IsChecked = True
Next
Else
For Each p In Me.Source
p.IsChecked = True
Next
End If
_FilterSettings = False
End Sub
End Class
Public Class ItemElement
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Sub New(data As SampleData)
Me.Data = data
End Sub
Private _Data As SampleData
Public Property Data() As SampleData
Get
Return _Data
End Get
Set(value As SampleData)
_Data = value
End Set
End Property
Private _IsChecked As String = False
Public Property IsChecked() As Boolean
Get
Return _IsChecked
End Get
Set(value As Boolean)
If _IsChecked <> value Then
_IsChecked = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("IsChecked"))
End If
End Set
End Property
Protected Sub OnPropertyChanged(propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
◎サンプルコード(C#)
◇MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = SampleData.GetSampleData();
}
private void DataGridNameColumn_FilterLoading(object sender, DataGridFilterLoadingEventArgs e)
{
var filter = ((DataGridFilterList)((DataGridContentFilter)e.Filter).Content).Items[0] as CustomFilter;
if (filter != null)
{
var source = from data in (this.DataContext as IEnumerable).Cast()
select new ItemElement(data);
filter.Source = source.ToList();
}
}
}
public class SampleData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _name = string.Empty;
public string Name
{
get { return this._name; }
set
{
if (this._name != value)
{
this._name = value;
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
}
public static ObservableCollection GetSampleData()
{
ObservableCollection list = new ObservableCollection();
list.Add(new SampleData() { Name = "Suzuki" });
list.Add(new SampleData() { Name = "Tanaka" });
list.Add(new SampleData() { Name = "Hashimoto" });
list.Add(new SampleData() { Name = "Ueda" });
list.Add(new SampleData() { Name = "Yokoyama" });
return list;
}
}
{
public MainWindow()
{
InitializeComponent();
this.DataContext = SampleData.GetSampleData();
}
private void DataGridNameColumn_FilterLoading(object sender, DataGridFilterLoadingEventArgs e)
{
var filter = ((DataGridFilterList)((DataGridContentFilter)e.Filter).Content).Items[0] as CustomFilter;
if (filter != null)
{
var source = from data in (this.DataContext as IEnumerable).Cast
select new ItemElement(data);
filter.Source = source.ToList();
}
}
}
public class SampleData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _name = string.Empty;
public string Name
{
get { return this._name; }
set
{
if (this._name != value)
{
this._name = value;
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
}
public static ObservableCollection
{
ObservableCollection
list.Add(new SampleData() { Name = "Suzuki" });
list.Add(new SampleData() { Name = "Tanaka" });
list.Add(new SampleData() { Name = "Hashimoto" });
list.Add(new SampleData() { Name = "Ueda" });
list.Add(new SampleData() { Name = "Yokoyama" });
return list;
}
}
◇CustomFilter.xaml.cs
public partial class CustomFilter : UserControl, IDataGridFilterUnity
{
private bool _FilterSettings = false;
private List _Source = null;
private DataGridFilterState _State = null;
public event PropertyChangedEventHandler PropertyChanged;
public CustomFilter()
{
InitializeComponent();
this.listBox1.SetBinding(C1TreeView.ItemsSourceProperty, new Binding("Source") { Source = this });
}
public bool? SelectAll
{
get
{
if (this.Source != null && this.Source.All(element => element.IsChecked))
return true;
else if (this.Source != null && this.Source.All(element => !element.IsChecked))
return false;
else
return null;
}
set
{
bool newValue = (bool?)value ?? false;
foreach (var element in this.Source)
element.IsChecked = newValue;
}
}
DataGridFilterState IDataGridFilterUnity.Filter
{
get { return this._State; }
set
{
if (this._State != value)
{
this._State = value;
this.SetFilterState(value);
}
}
}
public List Source
{
get { return this._Source; }
set
{
if (this._Source != value)
{
if (this._Source != null)
{
foreach (var element in this._Source)
element.PropertyChanged -= this.ItemPropertyChanged;
}
if (value != null)
{
foreach (var element in value)
element.PropertyChanged += this.ItemPropertyChanged;
}
this._Source = value;
this.OnPropertyChanged("Source");
}
}
}
private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (!_FilterSettings)
{
this._State = this.GetFilterState();
this.OnPropertyChanged("Filter");
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private DataGridFilterState GetFilterState()
{
this.OnPropertyChanged("SelectAll");
if (this.Source.All(element => element.IsChecked))
{
return new DataGridFilterState
{
FilterInfo = new List
{
new DataGridFilterInfo
{
FilterOperation = DataGridFilterOperation.All,
FilterType = DataGridFilterType.MultiValue,
}
}
};
}
else if (Source.All(element => !element.IsChecked))
{
return new DataGridFilterState
{
FilterInfo = new List
{
new DataGridFilterInfo
{
FilterOperation = DataGridFilterOperation.None,
FilterType = DataGridFilterType.MultiValue,
}
}
};
}
else
{
var checkedItems = Source.FindAll(element => element.IsChecked).Select(pe => pe.Data.Name).ToList();
return new DataGridFilterState
{
FilterInfo = new List
{
new DataGridFilterInfo
{
FilterOperation = DataGridFilterOperation.IsOneOf,
FilterType = DataGridFilterType.MultiValue,
Value = checkedItems,
}
}
};
}
}
private void SetFilterState(DataGridFilterState filter)
{
this._FilterSettings = true;
if (filter != null &&
filter.FilterInfo != null &&
filter.FilterInfo.Count > 0 &&
filter.FilterInfo[0] != null &&
filter.FilterInfo[0].FilterOperation == DataGridFilterOperation.IsOneOf &&
filter.FilterInfo[0].Value is List)
{
foreach (var element in Source)
element.IsChecked = false;
foreach (var element in ((List)filter.FilterInfo[0].Value))
element.IsChecked = true;
}
else
{
foreach (var element in Source)
element.IsChecked = true;
}
this._FilterSettings = false;
}
}
public class ItemElement : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ItemElement(SampleData data)
{
this.Data = data;
}
public SampleData Data { get; private set; }
private bool _IsChecked = false;
public bool IsChecked
{
get { return this._IsChecked; }
set
{
if (this._IsChecked != value)
{
this._IsChecked = value;
this.OnPropertyChanged("IsChecked");
}
}
}
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
{
private bool _FilterSettings = false;
private List
private DataGridFilterState _State = null;
public event PropertyChangedEventHandler PropertyChanged;
public CustomFilter()
{
InitializeComponent();
this.listBox1.SetBinding(C1TreeView.ItemsSourceProperty, new Binding("Source") { Source = this });
}
public bool? SelectAll
{
get
{
if (this.Source != null && this.Source.All(element => element.IsChecked))
return true;
else if (this.Source != null && this.Source.All(element => !element.IsChecked))
return false;
else
return null;
}
set
{
bool newValue = (bool?)value ?? false;
foreach (var element in this.Source)
element.IsChecked = newValue;
}
}
DataGridFilterState IDataGridFilterUnity.Filter
{
get { return this._State; }
set
{
if (this._State != value)
{
this._State = value;
this.SetFilterState(value);
}
}
}
public List
{
get { return this._Source; }
set
{
if (this._Source != value)
{
if (this._Source != null)
{
foreach (var element in this._Source)
element.PropertyChanged -= this.ItemPropertyChanged;
}
if (value != null)
{
foreach (var element in value)
element.PropertyChanged += this.ItemPropertyChanged;
}
this._Source = value;
this.OnPropertyChanged("Source");
}
}
}
private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (!_FilterSettings)
{
this._State = this.GetFilterState();
this.OnPropertyChanged("Filter");
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private DataGridFilterState GetFilterState()
{
this.OnPropertyChanged("SelectAll");
if (this.Source.All(element => element.IsChecked))
{
return new DataGridFilterState
{
FilterInfo = new List
{
new DataGridFilterInfo
{
FilterOperation = DataGridFilterOperation.All,
FilterType = DataGridFilterType.MultiValue,
}
}
};
}
else if (Source.All(element => !element.IsChecked))
{
return new DataGridFilterState
{
FilterInfo = new List
{
new DataGridFilterInfo
{
FilterOperation = DataGridFilterOperation.None,
FilterType = DataGridFilterType.MultiValue,
}
}
};
}
else
{
var checkedItems = Source.FindAll(element => element.IsChecked).Select(pe => pe.Data.Name).ToList();
return new DataGridFilterState
{
FilterInfo = new List
{
new DataGridFilterInfo
{
FilterOperation = DataGridFilterOperation.IsOneOf,
FilterType = DataGridFilterType.MultiValue,
Value = checkedItems,
}
}
};
}
}
private void SetFilterState(DataGridFilterState filter)
{
this._FilterSettings = true;
if (filter != null &&
filter.FilterInfo != null &&
filter.FilterInfo.Count > 0 &&
filter.FilterInfo[0] != null &&
filter.FilterInfo[0].FilterOperation == DataGridFilterOperation.IsOneOf &&
filter.FilterInfo[0].Value is List
{
foreach (var element in Source)
element.IsChecked = false;
foreach (var element in ((List
element.IsChecked = true;
}
else
{
foreach (var element in Source)
element.IsChecked = true;
}
this._FilterSettings = false;
}
}
public class ItemElement : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ItemElement(SampleData data)
{
this.Data = data;
}
public SampleData Data { get; private set; }
private bool _IsChecked = false;
public bool IsChecked
{
get { return this._IsChecked; }
set
{
if (this._IsChecked != value)
{
this._IsChecked = value;
this.OnPropertyChanged("IsChecked");
}
}
}
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
旧文書番号
81481