作成日: 2014/10/24 最終更新日: 2014/10/24
文書種別
使用方法
詳細
チェックボックス型列において、チェックボックスのOn/Off切り替え時のイベントを取得するには編集時のチェックボックスエディタのCheckedおよびUnchechedイベントを使用します。
以下のサンプルコードは、XAMLにEventSetterを指定してイベントを発生させています。
◎サンプルコード(XAML)
◎サンプルコード(Visual Basic)
◎サンプルコード(C#)
以下のサンプルコードは、XAMLにEventSetterを指定してイベントを発生させています。
◎サンプルコード(XAML)
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="*"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<TextBlock TextWrapping="Wrap" Text="[DataGrid for WPF]チェックボックス列でチェック時のイベントを発生させる方法" Grid.Row="0"/>
<c1:C1DataGrid x:Name="c1DataGrid1" ItemsSource="{Binding}" AutoGenerateColumns="False" Grid.Row="1">
<c1:C1DataGrid.Columns>
<c1:DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="150"/>
<c1:DataGridTextColumn Header="Location" Binding="{Binding Location}" Width="150"/>
<c1:DataGridCheckBoxColumn Header="Eligible" Binding="{Binding Eligible, Mode=TwoWay}" Width="150" >
<c1:DataGridCheckBoxColumn.CellEditingContentStyle>
<Style TargetType="CheckBox">
<Setter Property="Tag" Value="{Binding Name, Mode=OneWay}" />
<EventSetter Event="Checked" Handler="CheckBox_Checked" />
<EventSetter Event="Unchecked" Handler="CheckBox_Unchecked" />
</Style>
</c1:DataGridCheckBoxColumn.CellEditingContentStyle>
</c1:DataGridCheckBoxColumn>
</c1:C1DataGrid.Columns>
</c1:C1DataGrid>
<TextBlock Grid.Row="2" x:Name="textBlock1" />
</Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="*"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<TextBlock TextWrapping="Wrap" Text="[DataGrid for WPF]チェックボックス列でチェック時のイベントを発生させる方法" Grid.Row="0"/>
<c1:C1DataGrid x:Name="c1DataGrid1" ItemsSource="{Binding}" AutoGenerateColumns="False" Grid.Row="1">
<c1:C1DataGrid.Columns>
<c1:DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="150"/>
<c1:DataGridTextColumn Header="Location" Binding="{Binding Location}" Width="150"/>
<c1:DataGridCheckBoxColumn Header="Eligible" Binding="{Binding Eligible, Mode=TwoWay}" Width="150" >
<c1:DataGridCheckBoxColumn.CellEditingContentStyle>
<Style TargetType="CheckBox">
<Setter Property="Tag" Value="{Binding Name, Mode=OneWay}" />
<EventSetter Event="Checked" Handler="CheckBox_Checked" />
<EventSetter Event="Unchecked" Handler="CheckBox_Unchecked" />
</Style>
</c1:DataGridCheckBoxColumn.CellEditingContentStyle>
</c1:DataGridCheckBoxColumn>
</c1:C1DataGrid.Columns>
</c1:C1DataGrid>
<TextBlock Grid.Row="2" x:Name="textBlock1" />
</Grid>
◎サンプルコード(Visual Basic)
Partial Public Class KB81024
Inherits UserControl
Public Sub New()
InitializeComponent()
c1DataGrid1.ItemsSource = SampleData.GetSampleData()
End Sub
Private Sub CheckBox_Checked(sender As Object, e As Windows.RoutedEventArgs)
If TypeOf sender Is CheckBox Then
Me.textBlock1.Text = CType(sender, CheckBox).Tag + "のチェックがOnになりました"
End If
End Sub
Private Sub CheckBox_Unchecked(sender As Object, e As Windows.RoutedEventArgs)
If TypeOf sender Is CheckBox Then
Me.textBlock1.Text = CType(sender, CheckBox).Tag + "のチェックがOffになりました"
End If
End Sub
End Class
Public Class SampleData
Private m_Name As String
Private m_Location As String
Private m_Eligible As Boolean
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = value
End Set
End Property
Public Property Location() As String
Get
Return m_Location
End Get
Set(value As String)
m_Location = value
End Set
End Property
Public Property Eligible() As Boolean
Get
Return m_Eligible
End Get
Set(value As Boolean)
m_Eligible = value
End Set
End Property
Public Shared Function GetSampleData() As Collections.ObjectModel.ObservableCollection(Of SampleData)
Dim result As New Collections.ObjectModel.ObservableCollection(Of SampleData)
result.Add(New SampleData() With {.Name = "John", .Location = "India", .Eligible = True})
result.Add(New SampleData() With {.Name = "Michael", .Location = "US", .Eligible = False})
result.Add(New SampleData() With {.Name = "Teena", .Location = "Japan", .Eligible = False})
result.Add(New SampleData() With {.Name = "Suzi", .Location = "Brazil", .Eligible = False})
Return result
End Function
End Class
Inherits UserControl
Public Sub New()
InitializeComponent()
c1DataGrid1.ItemsSource = SampleData.GetSampleData()
End Sub
Private Sub CheckBox_Checked(sender As Object, e As Windows.RoutedEventArgs)
If TypeOf sender Is CheckBox Then
Me.textBlock1.Text = CType(sender, CheckBox).Tag + "のチェックがOnになりました"
End If
End Sub
Private Sub CheckBox_Unchecked(sender As Object, e As Windows.RoutedEventArgs)
If TypeOf sender Is CheckBox Then
Me.textBlock1.Text = CType(sender, CheckBox).Tag + "のチェックがOffになりました"
End If
End Sub
End Class
Public Class SampleData
Private m_Name As String
Private m_Location As String
Private m_Eligible As Boolean
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = value
End Set
End Property
Public Property Location() As String
Get
Return m_Location
End Get
Set(value As String)
m_Location = value
End Set
End Property
Public Property Eligible() As Boolean
Get
Return m_Eligible
End Get
Set(value As Boolean)
m_Eligible = value
End Set
End Property
Public Shared Function GetSampleData() As Collections.ObjectModel.ObservableCollection(Of SampleData)
Dim result As New Collections.ObjectModel.ObservableCollection(Of SampleData)
result.Add(New SampleData() With {.Name = "John", .Location = "India", .Eligible = True})
result.Add(New SampleData() With {.Name = "Michael", .Location = "US", .Eligible = False})
result.Add(New SampleData() With {.Name = "Teena", .Location = "Japan", .Eligible = False})
result.Add(New SampleData() With {.Name = "Suzi", .Location = "Brazil", .Eligible = False})
Return result
End Function
End Class
◎サンプルコード(C#)
public partial class KB81024 : UserControl
{
public KB81024()
{
InitializeComponent();
c1DataGrid1.ItemsSource = SampleData.GetSampleData();
}
private void CheckBox_Checked(object sender, System.Windows.RoutedEventArgs e)
{
if (sender is CheckBox)
{
this.textBlock1.Text = ((CheckBox)sender).Tag + "のチェックがOnになりました";
}
}
private void CheckBox_Unchecked(object sender, System.Windows.RoutedEventArgs e)
{
if (sender is CheckBox)
{
this.textBlock1.Text = ((CheckBox)sender).Tag + "のチェックがOffになりました";
}
}
}
public class SampleData
{
private string m_Name;
private string m_Location;
private bool m_Eligible;
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
public string Location
{
get { return m_Location; }
set { m_Location = value; }
}
public bool Eligible
{
get { return m_Eligible; }
set { m_Eligible = value; }
}
public static System.Collections.ObjectModel.ObservableCollection GetSampleData()
{
System.Collections.ObjectModel.ObservableCollection result = new System.Collections.ObjectModel.ObservableCollection();
result.Add(new SampleData
{
Name = "John",
Location = "India",
Eligible = true
});
result.Add(new SampleData
{
Name = "Michael",
Location = "US",
Eligible = false
});
result.Add(new SampleData
{
Name = "Teena",
Location = "Japan",
Eligible = false
});
result.Add(new SampleData
{
Name = "Suzi",
Location = "Brazil",
Eligible = false
});
return result;
}
}
{
public KB81024()
{
InitializeComponent();
c1DataGrid1.ItemsSource = SampleData.GetSampleData();
}
private void CheckBox_Checked(object sender, System.Windows.RoutedEventArgs e)
{
if (sender is CheckBox)
{
this.textBlock1.Text = ((CheckBox)sender).Tag + "のチェックがOnになりました";
}
}
private void CheckBox_Unchecked(object sender, System.Windows.RoutedEventArgs e)
{
if (sender is CheckBox)
{
this.textBlock1.Text = ((CheckBox)sender).Tag + "のチェックがOffになりました";
}
}
}
public class SampleData
{
private string m_Name;
private string m_Location;
private bool m_Eligible;
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
public string Location
{
get { return m_Location; }
set { m_Location = value; }
}
public bool Eligible
{
get { return m_Eligible; }
set { m_Eligible = value; }
}
public static System.Collections.ObjectModel.ObservableCollection
{
System.Collections.ObjectModel.ObservableCollection
result.Add(new SampleData
{
Name = "John",
Location = "India",
Eligible = true
});
result.Add(new SampleData
{
Name = "Michael",
Location = "US",
Eligible = false
});
result.Add(new SampleData
{
Name = "Teena",
Location = "Japan",
Eligible = false
});
result.Add(new SampleData
{
Name = "Suzi",
Location = "Brazil",
Eligible = false
});
return result;
}
}
旧文書番号
81024