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

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

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

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

WPF

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

Q&A

1回答

5373閲覧

DataGridにViewModelをバインドしたい

shinpass

総合スコア15

C#

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

WPF

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

0グッド

1クリップ

投稿2018/07/27 03:05

編集2018/07/27 03:06

イメージ説明

ボタンを押すたびにテキストボックスの文字をDataGridに追加するソフトを作成したいと思っています。

xaml

1<Window x:Class="WpfApp2.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp2" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="450" Width="800"> 9 <Window.DataContext> 10 <local:TestViewModel/> 11 </Window.DataContext> 12 <Grid> 13 <TextBox HorizontalAlignment="Left" Height="40" Margin="95,55,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="190"/> 14 <TextBox HorizontalAlignment="Left" Height="40" Margin="95,135,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="190"/> 15 <Button Content="Button" HorizontalAlignment="Left" Height="40" Margin="355,135,0,0" VerticalAlignment="Top" Width="155"/> 16 <DataGrid ItemsSource="{Binding Path=TestModels}" AutoGenerateColumns="False" 17 HorizontalAlignment="Left" Height="135" Margin="95,230,0,0" VerticalAlignment="Top" Width="415"> 18 <DataGrid.Columns> 19 <DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="1番目"/> 20 <DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="2番目"/> 21 </DataGrid.Columns> 22 </DataGrid> 23 24 </Grid> 25</Window> 26

ViewModel

1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6 7using System.Collections.ObjectModel; 8using Prism.Mvvm; 9 10namespace WpfApp2 11{ 12 public class TestViewModel : BindableBase 13 { 14 private int hoge1; 15 public int Hoge1 16 { 17 get { return hoge1; } 18 set { this.SetProperty(ref this.hoge1, value); } 19 } 20 21 private int hoge2; 22 public int Hoge2 23 { 24 get { return hoge2; } 25 set { this.SetProperty(ref this.hoge2, value); } 26 } 27 28 private ObservableCollection<TestModel> testModels; 29 public ObservableCollection<TestModel> TestModels 30 { 31 get { return testModels; } 32 set { this.SetProperty(ref this.testModels, value); } 33 } 34 35 public TestViewModel() 36 { 37 testModels = new ObservableCollection<TestModel>(); 38 } 39 } 40} 41

model

1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6 7using System.Collections.ObjectModel; 8using Prism.Mvvm; 9 10namespace WpfApp2 11{ 12 public class TestModel:BindableBase 13 { 14 private int first; 15 public int First 16 { 17 get { return first; } 18 set { this.SetProperty(ref this.first, value); } 19 } 20 21 private int second; 22 public int Second 23 { 24 get { return second; } 25 set { this.SetProperty(ref this.second, value); } 26 } 27 28 public TestModel() 29 { 30 31 } 32 } 33} 34

xamlのDataGridTextColumn BindingにmodelのFirstプロパティをバインドしようとしたのですが、できませんでした。
インテリセンスではCountプロパティしか表示されていませんでした。

どこを直せばいいのか?
ViewModelとModelに分けているのがだめなのか?
DatGridへのバインドがだめなのか?
まったくわからない状態です。
アドバイスお願いします。

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

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

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

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

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

guest

回答1

0

バインドしようとしたのですが、できませんでした。

とは、どのような意味ですか? どのようにbindしようとしたのですか? その結果どうなったのですか? エラーで落ちた? エラーは表示されないけど値が表示されない? 具体的な状況を書いたほうが回答しやすいかと思います。

パッとみた限りですと、

XAML

1 <DataGridTextColumn Binding="{x:Null}" ...

これだとnullをbindしてるわけですから何も表示されないと思うのですが。

firstをBindしたいのなら

XAML

1 <DataGridTextColumn Binding="{Binding Path=First}"...

じゃないですか?こうしたのに表示されないということですか?

投稿2018/07/27 04:16

ebiryo

総合スコア797

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

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

shinpass

2018/07/27 04:54

Binding Path=Firstにしようとしたのですが、 Hoge1, Hoge2, TestModelsの3つしか表示されませんでした。
ebiryo

2018/07/27 06:33 編集

「表示されませんでした」ではなく、手打ちで入力して実行するとどうなります?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問