ListViewã使ã£ãDataGridãã©ãã®ä½æ-æºåç·¨-
ã¾ãã¯ãå ¬éããã¦ãããµã³ãã«ãæã«å ¥ãããã¨ããå§ãã¾ããæ¬æ¥ã§ããã°ä¸è¨ã®ãã¼ã¸ãããã¦ã³ãã¼ãã§ããã¯ãã§ãããç¾å¨ã¯ã¾ã ã§ããªãããã§ãã
07/10追è¨
è±èªãã¼ã¸ã§ã¯ãã¦ã³ãã¼ãå¯è½ã§ãã
11/06/17追è¨
2011å¹´6æç¾å¨ã§ã¯ããã¡ãã®ãã¼ã¸ãããã¦ã³ãã¼ãå¯è½ã§ãã
ã¨ããããã§ããVS 2005 SP1用MSDNライブラリ2007年6月版ãããããã¯ãWindows SDK日本語版ããã¤ã³ã¹ãã¼ã«ãããã¡ããããµã³ãã«ãæã«ããã¾ãããã
ããã¥ã¡ã³ãã¯ä¸è¨ã®å ´æã«ãªãã¾ãã
MSDNã©ã¤ãã©ãª
ms-help://MS.MSDNQTR.v80.ja/MS.MSDN.v80/MS.NETDEVFX.v20.ja/wpf_samples/html/03893980-1aa0-4dd8-9b81-5645f933722d.htm
Windows SDK
ms-help://MS.MSSDK.1041/MS.NETFX30SDK.1041/wpf_samples/html/03893980-1aa0-4dd8-9b81-5645f933722d.htm
ãã¦ã³ãã¼ãããæç¹ã§æ°ãã¤ããã¨æãã¾ããããã®ãµã³ãã«ã¯C#ããç¨æããã¦ãã¾ãããVBã®ããã¸ã§ã¯ãã§ã使ãããããã¾ãã¯EditBoxã³ã³ããã¼ã«ãã³ã³ãã¼ãã³ãåãããã¨æãã¾ãã
Visual Studio 2005ãèµ·åããï¼»æ°ããããã¸ã§ã¯ãï¼½ããC#ã®ãCustomControlLibrary (WPF)ããé¸æããï¼»OKï¼½ãã¿ã³ãæ¼ãã¾ããããã¸ã§ã¯ãåã¯ä»»æã®ãã®ã§æ§ãã¾ããããããã§ã¯ãEditBoxãã¨ãã¾ãã
ï¼»ããã¸ã§ã¯ãï¼½-ï¼»æ¢åé
ç®ã®è¿½å ï¼½ã§ãå
ã»ã©æã«å
¥ãããµã³ãã«å
ã«å«ã¾ãããEditBox.csãã¨ãEditBoxAdorner.csããããã¸ã§ã¯ãã¸è¿½å ãã¾ããããã¸ã§ã¯ãã«æ¢å®ã§ä½æããã¦ãããUserControl1.xamlãã¯ä»åã¯å¿
è¦ãªãã®ã§åé¤ãã¦ããã¾ãã
ããã¸ã§ã¯ãããã«ãããã¨ãEditBox.dllããä½æããã¾ãã
ListViewã使ã£ãDataGridãã©ãã®ä½æ-ä½æç·¨-
ä½æããEditBoxã³ã³ããã¼ã«ã使ããç·¨éæ©è½ãæã¤ListViewã³ã³ããã¼ã«ãä½æãã¦ããã¾ãããµã³ãã«ãListViewEditableãã§ã¯ãObjectDataProviderã使ã£ã¦ãªãã¸ã§ã¯ãã«ãã¤ã³ããã¦ãã¾ãããããã§ã¯ãã¼ã¿ãã¼ã¹ã使ã£ã¦DataTableã¨ãã¤ã³ããããã®ãä½æãã¾ãã
ãã®ããããã¼ã«ã«ã³ã³ãã¥ã¼ã¿ã®SQL Server Express Editionã«NorthWindサンプルデータãã¤ã³ã¹ãã¼ã«ãããç°å¢ãåæã¨ãã¦ãã¾ãã
ä»åº¦ã¯C#ã§ã¯ãªããVisual Basicã®WPFã¢ããªã±ã¼ã·ã§ã³ããã¸ã§ã¯ããæ°è¦ã«ä½æãã¾ãããã¡ããExpression BlendãVisual Sutido 2005ã®ã©ã¡ãã§ãæ§ãã¾ããã
å
ã»ã©ä½æãããEditBox.dllãã¸ã®åç
§ã追å ãã¾ããExpression Blendã§ããã¸ã§ã¯ããæ°è¦ä½æããå ´åã«ã¯ãSystem.Dataã¢ã»ã³ããªã¸ã®åç
§ãå
¥ã£ã¦ãã¾ããã®ã§ããã¡ãã®ã¢ã»ã³ããªã¸ã®åç
§ã追å ãã¾ãã
ä¸è¨ã®å
容ãå
¥åãã¾ãã
Window1.xaml
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Editing="clr-namespace:Editing;assembly=EditBox"
x:Class="Window1"
x:Name="Window"
Title="Window1"
Width="450" Height="380" Loaded="Window_Loaded">
<Window.Resources>
<Style TargetType="{x:Type Editing:EditBox}" >
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Editing:EditBox}">
<TextBlock x:Name="PART_TextBlockPart"
Text="{Binding Path=Value,Mode=Twoway,RelativeSource =
{RelativeSource TemplatedParent}}"
Width="{Binding Path=Width,Mode=Twoway,RelativeSource =
{RelativeSource TemplatedParent}}">
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<StackPanel x:Name="LayoutRoot">
<ListView IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}"
x:Name="ListView1" Width="Auto" Height="Auto">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=LastName}"
Header="苗字" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=FirstName}"
Header="名前" Width="100"/>
<GridViewColumn Header="役職" Width="200" x:Name="gvColumn3">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Editing:EditBox Value="{Binding Path=Title, Mode=TwoWay}"
Height="25" Width="{Binding Path=Width, ElementName=gvColumn3}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Button HorizontalAlignment="Left" Margin="10"
Width="75" Height="25" Content="更新" Click="Button_Click"/>
</StackPanel>
</Window>
Window1.xaml.vb
Imports System.IO
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Navigation
Imports System.Data
Imports System.Data.SqlClient
Partial Public Class Window1
Public Sub New()
MyBase.New()
Me.InitializeComponent()
' オブジェクト作成に必要なコードをこの点の下に挿入します。
End Sub
Dim table As New DataTable()
Dim dataAdapter As SqlDataAdapter
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Dim connectionString As String = _
"Integrated Security=SSPI;Persist Security Info=False;" + _
"Initial Catalog=Northwind;Data Source=.\sqlexpress"
Dim selectCommand As String = "SELECT * FROM Employees"
dataAdapter = New SqlDataAdapter(selectCommand, connectionString)
Dim cmdBuilder As New SqlCommandBuilder(dataAdapter)
dataAdapter.Fill(table)
ListView1.DataContext = table
End Sub
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
dataAdapter.Update(table)
End Sub
End Class
ãã®ãµã³ãã«ã§ã¯ããµã³ãã«ãListViewEditableãã§ã®ä½¿ç¨æ¹æ³ã«2ç¹ã»ã©è¿½å ãã¦ããé¨åãããã¾ãã
ã©ã¡ããxmalã³ã¼ãé¨åã§ãããã²ã¨ã¤ã¯ã§ããã³ãã¬ã¼ãã®è¦ªã®Valueããããã£ã¨ãã¤ã³ããã¦ããé¨åã§ãããããã«Mode=TwoWayã追å ãã¦ãã¾ããããã追å ããªãã¨ãå ¥åããå¤ããã¼ã¿ãã¼ã¹ã¸åæ ããããã¨ãã§ãã¾ããã
ããä¸ã¤ã¯
{RelativeSource TemplatedParent}}">
Height="25" Width="{Binding Path=Width, ElementName=gvColumn3}"/>
æ´æ°ãã¿ã³ãæ¼ããã¨ã§ããã¼ã¿ãã¼ã¹ä¸ã®ãã¼ã¿ãæ´æ°ãããã¨ãã§ãã¾ããç°¡åãªãã®ã§ããã°ããã®æ¹æ³ã使ã£ã¦DataGridã³ã³ããã¼ã«ã®ãããªUIãå®ç¾ãããã¨ãå¯è½ã ã¨æãã¾ãã