WPF初心者です。
よろしくお願いいたします。
以前ListView内の文字をtextboxに表示する方法を質問させていただいた者です。
また質問させてください。
ListView内の文字をtextbox内で変更を行い、
編集ボタンを押すと編集可能になり、保存ボタンを押すと、
ListView内の文字が変わる方法を模索しています。
XAML
1<Window x:Class="WpfApp27.Window1" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="Window1" Height="400" Width="690.238"> 5 <Grid> 6 <ListView x:Name="listView1" ItemsSource="{Binding}" Margin="0,0,197,0"> 7 <ListView.Resources> 8 9 <!--得意先列ヘッダ用テンプレート--> 10 <DataTemplate x:Key="customerHeaderTemplate"> 11 <StackPanel> 12 <Label Content="得意先コード"/> 13 <Label Content="得意先名"/> 14 </StackPanel> 15 </DataTemplate> 16 17 <!--得意先セル用テンプレート--> 18 <DataTemplate x:Key="customerCellTemplate"> 19 <StackPanel> 20 <Label Content="{Binding Path=CustomerCode}"/> 21 <Label Content="{Binding Path=CustomerName}"/> 22 </StackPanel> 23 </DataTemplate> 24 25 <!--伝票種列ヘッダ用テンプレート--> 26 <DataTemplate x:Key="kindHeaderTemplate"> 27 <StackPanel> 28 <Label Content="伝票種"/> 29 <Label Content="伝票番号フラグ"/> 30 </StackPanel> 31 </DataTemplate> 32 33 <!--伝票種セル用テンプレート--> 34 <DataTemplate x:Key="kindCellTemplate"> 35 <StackPanel> 36 <Label Content="{Binding Path=Kind}"/> 37 <Label Content="{Binding Path=NoFlag}"/> 38 </StackPanel> 39 </DataTemplate> 40 41 </ListView.Resources> 42 <ListView.View> 43 <GridView> 44 <!--列を追加。--> 45 <!--DisplayMemberBinding を使って、列に表示する Slip クラスのプロパティを指定しています--> 46 <GridViewColumn Header="伝票日付" DisplayMemberBinding="{Binding Path=Date}"/> 47 48 <!--伝票種列ヘッダ用のテンプレートと伝票種セル用のテンプレートを指定--> 49 <GridViewColumn HeaderTemplate="{StaticResource kindHeaderTemplate}" CellTemplate="{StaticResource kindCellTemplate}"/> 50 51 <GridViewColumn Header="伝票番号" DisplayMemberBinding="{Binding Path=No}"/> 52 53 <!--得意先列ヘッダ用のテンプレートと得意先セル用のテンプレートを指定--> 54 <GridViewColumn HeaderTemplate="{StaticResource customerHeaderTemplate}" CellTemplate="{StaticResource customerCellTemplate}"/> 55 </GridView> 56 </ListView.View> 57 </ListView> 58 <TextBox HorizontalAlignment="Left" Height="23" Margin="534,94,0,0" TextWrapping="Wrap" Text="{Binding Path=SelectedItem.CustomerName,ElementName=listView1}" VerticalAlignment="Top" Width="120"/> 59 <Button Content="保存" HorizontalAlignment="Left" Margin="597,161,0,0" VerticalAlignment="Top" Width="75"/> 60 <Button Content="編集" HorizontalAlignment="Left" Margin="514,161,0,0" VerticalAlignment="Top" Width="75"/> 61 </Grid> 62</Window>
C#
1<Window x:Class="WpfApp27.Window1" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="Window1" Height="400" Width="690.238"> 5 <Grid> 6 <ListView x:Name="listView1" ItemsSource="{Binding}" Margin="0,0,197,0"> 7 <ListView.Resources> 8 9 <!--得意先列ヘッダ用テンプレート--> 10 <DataTemplate x:Key="customerHeaderTemplate"> 11 <StackPanel> 12 <Label Content="得意先コード"/> 13 <Label Content="得意先名"/> 14 </StackPanel> 15 </DataTemplate> 16 17 <!--得意先セル用テンプレート--> 18 <DataTemplate x:Key="customerCellTemplate"> 19 <StackPanel> 20 <Label Content="{Binding Path=CustomerCode}"/> 21 <Label Content="{Binding Path=CustomerName}"/> 22 </StackPanel> 23 </DataTemplate> 24 25 <!--伝票種列ヘッダ用テンプレート--> 26 <DataTemplate x:Key="kindHeaderTemplate"> 27 <StackPanel> 28 <Label Content="伝票種"/> 29 <Label Content="伝票番号フラグ"/> 30 </StackPanel> 31 </DataTemplate> 32 33 <!--伝票種セル用テンプレート--> 34 <DataTemplate x:Key="kindCellTemplate"> 35 <StackPanel> 36 <Label Content="{Binding Path=Kind}"/> 37 <Label Content="{Binding Path=NoFlag}"/> 38 </StackPanel> 39 </DataTemplate> 40 41 </ListView.Resources> 42 <ListView.View> 43 <GridView> 44 <!--列を追加。--> 45 <!--DisplayMemberBinding を使って、列に表示する Slip クラスのプロパティを指定しています--> 46 <GridViewColumn Header="伝票日付" DisplayMemberBinding="{Binding Path=Date}"/> 47 48 <!--伝票種列ヘッダ用のテンプレートと伝票種セル用のテンプレートを指定--> 49 <GridViewColumn HeaderTemplate="{StaticResource kindHeaderTemplate}" CellTemplate="{StaticResource kindCellTemplate}"/> 50 51 <GridViewColumn Header="伝票番号" DisplayMemberBinding="{Binding Path=No}"/> 52 53 <!--得意先列ヘッダ用のテンプレートと得意先セル用のテンプレートを指定--> 54 <GridViewColumn HeaderTemplate="{StaticResource customerHeaderTemplate}" CellTemplate="{StaticResource customerCellTemplate}"/> 55 </GridView> 56 </ListView.View> 57 </ListView> 58 <TextBox HorizontalAlignment="Left" Height="23" Margin="534,94,0,0" TextWrapping="Wrap" Text="{Binding Path=SelectedItem.CustomerName,ElementName=listView1}" VerticalAlignment="Top" Width="120"/> 59 <Button Content="保存" HorizontalAlignment="Left" Margin="597,161,0,0" VerticalAlignment="Top" Width="75"/> 60 <Button Content="編集" HorizontalAlignment="Left" Margin="514,161,0,0" VerticalAlignment="Top" Width="75"/> 61 </Grid> 62</Window>
現状でもTextbox内に選択した項目は表示されて、
編集可能なのですが、一回終了すると内容の保存は行われないので、
何か良い方法を知っている方がいらっしゃれば教えていただければ幸いです。
よろしくお願いいたします。
MVVMパターンに見えますが、永続化はModelの責務です。どのように実装(予定)ですか?
回答1件
あなたの回答
tips
プレビュー