作成日: 2014/10/22 最終更新日: 2014/10/22
文書種別
使用方法
詳細
アンバウンド列に対して任意の値を表示するには、該当セルのセルプレゼンター(DataGridCell.Presenterプロパティ)を参照し、そこに含まれるコンテンツ要素に対して直接値を指定する必要があります。セルプレゼンターは、そのセルの内容を表示するために使用するUI要素を提供するクラスです。セルプレゼンターが生成されたときにはC1DataGridのLoadedCellPresenterイベントが発生します。イベント引数を参照することで、セルプレゼンターにアクセスできます。
以下のサンプルコードは、インデックス0の列がアンバウンド列である場合にLoadedCellPresenterイベントにてセルのテキストを設定しています。行インデックスをキーとしたディクショナリにセルの表示内容が格納され、DataGridTextColumn型の列のセルプレゼンターにアクセスしています。DataGridTextColumn型では、セルの値の表示にTextBlockが使用されています。アンバウンド列内の特定の行に対して値を設定するには、上記で示したディクショナリに対して値を設定します。
◎サンプルコード(XAML)
◎サンプルコード(Visual Basic)
◎サンプルコード(C#)
以下のサンプルコードは、インデックス0の列がアンバウンド列である場合にLoadedCellPresenterイベントにてセルのテキストを設定しています。行インデックスをキーとしたディクショナリにセルの表示内容が格納され、DataGridTextColumn型の列のセルプレゼンターにアクセスしています。DataGridTextColumn型では、セルの値の表示にTextBlockが使用されています。アンバウンド列内の特定の行に対して値を設定するには、上記で示したディクショナリに対して値を設定します。
◎サンプルコード(XAML)
<Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded">
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Name="btnAdd" Content="セル[60,0]値を設定するにはクリックします" Click="btnSetCellValue_Click" Grid.Row="1" Width="300"/>
<c1:C1DataGrid Name="c1DataGrid" ItemsSource="{Binding}" AutoGenerateColumns="True" Grid.Row="2" CanUserEditRows="True" VerticalScrollBarVisibility="Auto">
<c1:C1DataGrid.Columns>
<c1:DataGridTextColumn Header="Location" />
</c1:C1DataGrid.Columns>
</c1:C1DataGrid>
</Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Name="btnAdd" Content="セル[60,0]値を設定するにはクリックします" Click="btnSetCellValue_Click" Grid.Row="1" Width="300"/>
<c1:C1DataGrid Name="c1DataGrid" ItemsSource="{Binding}" AutoGenerateColumns="True" Grid.Row="2" CanUserEditRows="True" VerticalScrollBarVisibility="Auto">
<c1:C1DataGrid.Columns>
<c1:DataGridTextColumn Header="Location" />
</c1:C1DataGrid.Columns>
</c1:C1DataGrid>
</Grid>
◎サンプルコード(Visual Basic)
Imports C1.WPF
Imports C1.WPF.DataGrid
Imports System
Imports System.Windows.Controls
Partial Public Class KB81014
Inherits UserControl
Private unboundCollection As New System.Collections.Generic.Dictionary(Of Integer, String)()
Private unBoundCellValue As String
Private unboundFlagUpdateValue As Boolean
Private names As New System.Collections.ObjectModel.ObservableCollection(Of myNames)()
Public Sub New()
InitializeComponent()
For index As Integer = 0 To 99
names.Add(New myNames() With { _
.Name = "Name" & index.ToString() _
})
Next
Me.DataContext = names
End Sub
Private Sub LayoutRoot_Loaded(sender As Object, e As System.Windows.RoutedEventArgs)
' アンバウンド列に対してダミーのバインディングを設定します。
Dim column As C1.WPF.DataGrid.DataGridTextColumn = TryCast(Me.c1DataGrid.Columns(0), C1.WPF.DataGrid.DataGridTextColumn)
If column IsNot Nothing Then
column.Binding = New System.Windows.Data.Binding() With { _
.Path = New System.Windows.PropertyPath("[Temp]"), _
.Mode = System.Windows.Data.BindingMode.OneTime _
}
column.Tag = "tempBinding"
End If
' セルプレゼンタが読み込まれたときにアンバウンドセルの表示内容を更新します。
AddHandler Me.c1DataGrid.LoadedCellPresenter, AddressOf c1DataGrid_LoadedCellPresenter
' アンバウンドセルの編集をサポートする場合は、以下のイベントも処理します。
AddHandler Me.c1DataGrid.BeginningEdit, AddressOf c1DataGrid_BeginningEdit
AddHandler Me.c1DataGrid.BeganEdit, AddressOf c1DataGrid_BeganEdit
AddHandler Me.c1DataGrid.CancelingEdit, AddressOf c1DataGrid_CancelingEdit
AddHandler Me.c1DataGrid.CommittingEdit, AddressOf c1DataGrid_CommittingEdit
AddHandler Me.c1DataGrid.CommittedEdit, AddressOf c1DataGrid_CommittedEdit
End Sub
Private Sub c1DataGrid_CancelingEdit(sender As Object, e As DataGridEndingEditEventArgs)
Me.c1DataGrid.Refresh(True, False, True, False, False)
End Sub
Private Sub c1DataGrid_BeginningEdit(sender As Object, e As C1.WPF.DataGrid.DataGridBeginningEditEventArgs)
If e.Column.Tag IsNot Nothing Then
If Me.unboundCollection.ContainsKey(e.Row.Index) Then
unBoundCellValue = Me.unboundCollection(e.Row.Index)
End If
End If
End Sub
Private Sub c1DataGrid_BeganEdit(sender As Object, e As DataGridBeganEditEventArgs)
If e.Column.Tag IsNot Nothing Then
Dim editor As C1TextBoxBase = TryCast(e.EditingElement, C1TextBoxBase)
If editor IsNot Nothing Then
If Me.unboundCollection.ContainsKey(e.Row.Index) Then
editor.C1Text = Me.unboundCollection(e.Row.Index)
Else
editor.C1Text = String.Empty
End If
End If
End If
End Sub
Private Sub c1DataGrid_CommittingEdit(sender As Object, e As DataGridEndingEditEventArgs)
If e.Column.Tag IsNot Nothing Then
If e.Row.DataItem IsNot Nothing Then
unboundFlagUpdateValue = True
unBoundCellValue = DirectCast(e.EditingElement, C1TextBoxBase).C1Text
End If
End If
End Sub
Private Sub c1DataGrid_CommittedEdit(sender As Object, e As DataGridCellEventArgs)
If unboundFlagUpdateValue Then
TryCast(e.Cell.Presenter.Content, TextBlock).Text = unBoundCellValue
Me.unboundCollection(e.Cell.Row.Index) = unBoundCellValue
End If
unboundFlagUpdateValue = False
unBoundCellValue = Nothing
End Sub
Private Sub c1DataGrid_LoadedCellPresenter(sender As Object, e As DataGridCellEventArgs)
If e.Cell.Column.Tag IsNot Nothing Then
If Me.unboundCollection.ContainsKey(e.Cell.Row.Index) Then
Dim textBlock As TextBlock = TryCast(e.Cell.Presenter.Content, TextBlock)
If textBlock IsNot Nothing Then
textBlock.Text = Me.unboundCollection(e.Cell.Row.Index)
End If
End If
End If
End Sub
Private Sub btnSetCellValue_Click(sender As Object, e As System.Windows.RoutedEventArgs)
Me.unboundCollection(60) = "セル[60, 0]に値が追加されました。"
Me.c1DataGrid.Refresh(True, False, True, False, False)
End Sub
End Class
Public Class myNames
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = value
End Set
End Property
Private m_Name As String
End Class
Imports C1.WPF.DataGrid
Imports System
Imports System.Windows.Controls
Partial Public Class KB81014
Inherits UserControl
Private unboundCollection As New System.Collections.Generic.Dictionary(Of Integer, String)()
Private unBoundCellValue As String
Private unboundFlagUpdateValue As Boolean
Private names As New System.Collections.ObjectModel.ObservableCollection(Of myNames)()
Public Sub New()
InitializeComponent()
For index As Integer = 0 To 99
names.Add(New myNames() With { _
.Name = "Name" & index.ToString() _
})
Next
Me.DataContext = names
End Sub
Private Sub LayoutRoot_Loaded(sender As Object, e As System.Windows.RoutedEventArgs)
' アンバウンド列に対してダミーのバインディングを設定します。
Dim column As C1.WPF.DataGrid.DataGridTextColumn = TryCast(Me.c1DataGrid.Columns(0), C1.WPF.DataGrid.DataGridTextColumn)
If column IsNot Nothing Then
column.Binding = New System.Windows.Data.Binding() With { _
.Path = New System.Windows.PropertyPath("[Temp]"), _
.Mode = System.Windows.Data.BindingMode.OneTime _
}
column.Tag = "tempBinding"
End If
' セルプレゼンタが読み込まれたときにアンバウンドセルの表示内容を更新します。
AddHandler Me.c1DataGrid.LoadedCellPresenter, AddressOf c1DataGrid_LoadedCellPresenter
' アンバウンドセルの編集をサポートする場合は、以下のイベントも処理します。
AddHandler Me.c1DataGrid.BeginningEdit, AddressOf c1DataGrid_BeginningEdit
AddHandler Me.c1DataGrid.BeganEdit, AddressOf c1DataGrid_BeganEdit
AddHandler Me.c1DataGrid.CancelingEdit, AddressOf c1DataGrid_CancelingEdit
AddHandler Me.c1DataGrid.CommittingEdit, AddressOf c1DataGrid_CommittingEdit
AddHandler Me.c1DataGrid.CommittedEdit, AddressOf c1DataGrid_CommittedEdit
End Sub
Private Sub c1DataGrid_CancelingEdit(sender As Object, e As DataGridEndingEditEventArgs)
Me.c1DataGrid.Refresh(True, False, True, False, False)
End Sub
Private Sub c1DataGrid_BeginningEdit(sender As Object, e As C1.WPF.DataGrid.DataGridBeginningEditEventArgs)
If e.Column.Tag IsNot Nothing Then
If Me.unboundCollection.ContainsKey(e.Row.Index) Then
unBoundCellValue = Me.unboundCollection(e.Row.Index)
End If
End If
End Sub
Private Sub c1DataGrid_BeganEdit(sender As Object, e As DataGridBeganEditEventArgs)
If e.Column.Tag IsNot Nothing Then
Dim editor As C1TextBoxBase = TryCast(e.EditingElement, C1TextBoxBase)
If editor IsNot Nothing Then
If Me.unboundCollection.ContainsKey(e.Row.Index) Then
editor.C1Text = Me.unboundCollection(e.Row.Index)
Else
editor.C1Text = String.Empty
End If
End If
End If
End Sub
Private Sub c1DataGrid_CommittingEdit(sender As Object, e As DataGridEndingEditEventArgs)
If e.Column.Tag IsNot Nothing Then
If e.Row.DataItem IsNot Nothing Then
unboundFlagUpdateValue = True
unBoundCellValue = DirectCast(e.EditingElement, C1TextBoxBase).C1Text
End If
End If
End Sub
Private Sub c1DataGrid_CommittedEdit(sender As Object, e As DataGridCellEventArgs)
If unboundFlagUpdateValue Then
TryCast(e.Cell.Presenter.Content, TextBlock).Text = unBoundCellValue
Me.unboundCollection(e.Cell.Row.Index) = unBoundCellValue
End If
unboundFlagUpdateValue = False
unBoundCellValue = Nothing
End Sub
Private Sub c1DataGrid_LoadedCellPresenter(sender As Object, e As DataGridCellEventArgs)
If e.Cell.Column.Tag IsNot Nothing Then
If Me.unboundCollection.ContainsKey(e.Cell.Row.Index) Then
Dim textBlock As TextBlock = TryCast(e.Cell.Presenter.Content, TextBlock)
If textBlock IsNot Nothing Then
textBlock.Text = Me.unboundCollection(e.Cell.Row.Index)
End If
End If
End If
End Sub
Private Sub btnSetCellValue_Click(sender As Object, e As System.Windows.RoutedEventArgs)
Me.unboundCollection(60) = "セル[60, 0]に値が追加されました。"
Me.c1DataGrid.Refresh(True, False, True, False, False)
End Sub
End Class
Public Class myNames
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = value
End Set
End Property
Private m_Name As String
End Class
◎サンプルコード(C#)
using C1.WPF;
using C1.WPF.DataGrid;
using System;
using System.Windows.Controls;
public partial class KB81014 : UserControl
{
System.Collections.Generic.Dictionary unboundCollection = new System.Collections.Generic.Dictionary();
string unBoundCellValue;
bool unboundFlagUpdateValue;
System.Collections.ObjectModel.ObservableCollection names = new System.Collections.ObjectModel.ObservableCollection();
public KB81014()
{
InitializeComponent();
for (int index = 0; index < 100; index++)
names.Add(new myNames() { Name = "Name" + index.ToString() });
this.DataContext = names;
}
private void LayoutRoot_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
// アンバウンド列に対してダミーのバインディングを設定します。
C1.WPF.DataGrid.DataGridTextColumn column = this.c1DataGrid.Columns[0] as C1.WPF.DataGrid.DataGridTextColumn;
if (column != null)
{
column.Binding = new System.Windows.Data.Binding() { Path = new System.Windows.PropertyPath("[Temp]"), Mode = System.Windows.Data.BindingMode.OneTime };
column.Tag = "tempBinding";
}
// セルプレゼンタが読み込まれたときにアンバウンドセルの表示内容を更新します。
this.c1DataGrid.LoadedCellPresenter += c1DataGrid_LoadedCellPresenter;
// アンバウンドセルの編集をサポートする場合は、以下のイベントも処理します。
this.c1DataGrid.BeginningEdit += c1DataGrid_BeginningEdit;
this.c1DataGrid.BeganEdit += c1DataGrid_BeganEdit;
this.c1DataGrid.CancelingEdit += c1DataGrid_CancelingEdit;
this.c1DataGrid.CommittingEdit += c1DataGrid_CommittingEdit;
this.c1DataGrid.CommittedEdit += c1DataGrid_CommittedEdit;
}
void c1DataGrid_CancelingEdit(object sender, DataGridEndingEditEventArgs e)
{
this.c1DataGrid.Refresh(true, false, true, false, false);
}
void c1DataGrid_BeginningEdit(object sender, C1.WPF.DataGrid.DataGridBeginningEditEventArgs e)
{
if (e.Column.Tag != null)
{
if (this.unboundCollection.ContainsKey(e.Row.Index))
unBoundCellValue = this.unboundCollection[e.Row.Index];
}
}
void c1DataGrid_BeganEdit(object sender, DataGridBeganEditEventArgs e)
{
if (e.Column.Tag != null)
{
C1TextBoxBase editor = e.EditingElement as C1TextBoxBase;
if (editor != null)
{
if (this.unboundCollection.ContainsKey(e.Row.Index))
editor.C1Text = this.unboundCollection[e.Row.Index];
else
editor.C1Text = string.Empty;
}
}
}
void c1DataGrid_CommittingEdit(object sender, DataGridEndingEditEventArgs e)
{
if (e.Column.Tag != null)
{
if (e.Row.DataItem != null)
{
unboundFlagUpdateValue = true;
unBoundCellValue = ((C1TextBoxBase)(e.EditingElement)).C1Text;
}
}
}
void c1DataGrid_CommittedEdit(object sender, DataGridCellEventArgs e)
{
if (unboundFlagUpdateValue)
{
(e.Cell.Presenter.Content as TextBlock).Text = unBoundCellValue;
this.unboundCollection[e.Cell.Row.Index] = unBoundCellValue;
}
unboundFlagUpdateValue = false;
unBoundCellValue = null;
}
void c1DataGrid_LoadedCellPresenter(object sender, DataGridCellEventArgs e)
{
if (e.Cell.Column.Tag != null)
{
if (this.unboundCollection.ContainsKey(e.Cell.Row.Index))
{
TextBlock textBlock = e.Cell.Presenter.Content as TextBlock;
if (textBlock != null)
textBlock.Text = this.unboundCollection[e.Cell.Row.Index];
}
}
}
private void btnSetCellValue_Click(object sender, System.Windows.RoutedEventArgs e)
{
this.unboundCollection[60] = "セル[60, 0]に値が追加されました。";
this.c1DataGrid.Refresh(true, false, true, false, false);
}
}
public class myNames
{
public string Name { get; set; }
}
using C1.WPF.DataGrid;
using System;
using System.Windows.Controls;
public partial class KB81014 : UserControl
{
System.Collections.Generic.Dictionary
string unBoundCellValue;
bool unboundFlagUpdateValue;
System.Collections.ObjectModel.ObservableCollection
public KB81014()
{
InitializeComponent();
for (int index = 0; index < 100; index++)
names.Add(new myNames() { Name = "Name" + index.ToString() });
this.DataContext = names;
}
private void LayoutRoot_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
// アンバウンド列に対してダミーのバインディングを設定します。
C1.WPF.DataGrid.DataGridTextColumn column = this.c1DataGrid.Columns[0] as C1.WPF.DataGrid.DataGridTextColumn;
if (column != null)
{
column.Binding = new System.Windows.Data.Binding() { Path = new System.Windows.PropertyPath("[Temp]"), Mode = System.Windows.Data.BindingMode.OneTime };
column.Tag = "tempBinding";
}
// セルプレゼンタが読み込まれたときにアンバウンドセルの表示内容を更新します。
this.c1DataGrid.LoadedCellPresenter += c1DataGrid_LoadedCellPresenter;
// アンバウンドセルの編集をサポートする場合は、以下のイベントも処理します。
this.c1DataGrid.BeginningEdit += c1DataGrid_BeginningEdit;
this.c1DataGrid.BeganEdit += c1DataGrid_BeganEdit;
this.c1DataGrid.CancelingEdit += c1DataGrid_CancelingEdit;
this.c1DataGrid.CommittingEdit += c1DataGrid_CommittingEdit;
this.c1DataGrid.CommittedEdit += c1DataGrid_CommittedEdit;
}
void c1DataGrid_CancelingEdit(object sender, DataGridEndingEditEventArgs e)
{
this.c1DataGrid.Refresh(true, false, true, false, false);
}
void c1DataGrid_BeginningEdit(object sender, C1.WPF.DataGrid.DataGridBeginningEditEventArgs e)
{
if (e.Column.Tag != null)
{
if (this.unboundCollection.ContainsKey(e.Row.Index))
unBoundCellValue = this.unboundCollection[e.Row.Index];
}
}
void c1DataGrid_BeganEdit(object sender, DataGridBeganEditEventArgs e)
{
if (e.Column.Tag != null)
{
C1TextBoxBase editor = e.EditingElement as C1TextBoxBase;
if (editor != null)
{
if (this.unboundCollection.ContainsKey(e.Row.Index))
editor.C1Text = this.unboundCollection[e.Row.Index];
else
editor.C1Text = string.Empty;
}
}
}
void c1DataGrid_CommittingEdit(object sender, DataGridEndingEditEventArgs e)
{
if (e.Column.Tag != null)
{
if (e.Row.DataItem != null)
{
unboundFlagUpdateValue = true;
unBoundCellValue = ((C1TextBoxBase)(e.EditingElement)).C1Text;
}
}
}
void c1DataGrid_CommittedEdit(object sender, DataGridCellEventArgs e)
{
if (unboundFlagUpdateValue)
{
(e.Cell.Presenter.Content as TextBlock).Text = unBoundCellValue;
this.unboundCollection[e.Cell.Row.Index] = unBoundCellValue;
}
unboundFlagUpdateValue = false;
unBoundCellValue = null;
}
void c1DataGrid_LoadedCellPresenter(object sender, DataGridCellEventArgs e)
{
if (e.Cell.Column.Tag != null)
{
if (this.unboundCollection.ContainsKey(e.Cell.Row.Index))
{
TextBlock textBlock = e.Cell.Presenter.Content as TextBlock;
if (textBlock != null)
textBlock.Text = this.unboundCollection[e.Cell.Row.Index];
}
}
}
private void btnSetCellValue_Click(object sender, System.Windows.RoutedEventArgs e)
{
this.unboundCollection[60] = "セル[60, 0]に値が追加されました。";
this.c1DataGrid.Refresh(true, false, true, false, false);
}
}
public class myNames
{
public string Name { get; set; }
}
旧文書番号
81014