質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

1回答

521閲覧

ListViewを使ってBindeinしてきたデータの変更方法

zumizumi

総合スコア13

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2018/04/05 06:43

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内に選択した項目は表示されて、
編集可能なのですが、一回終了すると内容の保存は行われないので、
何か良い方法を知っている方がいらっしゃれば教えていただければ幸いです。

よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hihijiji

2018/04/05 07:57

MVVMパターンに見えますが、永続化はModelの責務です。どのように実装(予定)ですか?
guest

回答1

0

ベストアンサー

ListView の選択された項目を TextBox とバインドさせていますが、これだとうまくいかないと思います。
編集ボタンをクリックしたら ListView の選択された項目とバインドされたデータのコピーを作成して TextBox とバインドさせ、保存を押した時にそのコピーを元データへ反映させてください。

もしくは項目データに IEditableObject を実装させ、編集で BeginEdit() し、保存で EndEdit() する方法もあります。
これなら直接バインドしても大丈夫です。

また TextBox で編集されたデータが ListView に反映されないのは元のデータに INotifyPropertyChanged が実装されていないせいかもしれません。

投稿2018/04/05 07:51

Zuishin

総合スコア28660

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

zumizumi

2018/04/05 08:14

素早い回答ありがとうございます! 早速実装できました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問