実現したいこと
何故タイトルの様なエラーが出てしまっているのか、何がエラーの原因なのかが不明である為、
これを解決できる方法などを知り、解決したい。
<編集による追記項目>
ご教授頂きたいこと・やってほしいこととしては、
1.まずこのエラーの内容は何を意味しているのか。
2.エラーに対して、どこかのクラスなどにDataContextn等を設置すればよいだけなのか?
3.そもそも記述方法がうまくない等で直した方が良い場所があるかどうか。
感覚などでもよいので、教えて頂けると助かります。
前提
既存のプログラムのListやクラス、WPF内のDataGridやView等の外観を変更し処理などを追加して欲しいと言われたが、
あまりWPFの経験がない為手探りで改修を行っており、
解決に時間がかかってしまっているエラーが発生してしまった。
発生している問題・エラーメッセージ
C#プログラムで画面遷移時に System.Windows.Data Error: 17 : Cannot get 'Test' value (type 'TestPageViewModel') from '' (type 'ViewModelLocator'). BindingExpression:Path=Test; DataItem='ViewModelLocator' (HashCode=41816592); target element is 'TestPage' (Name=''); といったエラーが発生してしまっている。
該当のソースコード
一部省略して書かせて頂きました。必要そうな部分が抜けている等ありましたら、ご指摘・ご教授下さい。
C#
1// 画面Page 2 public partial class TestPage : Page 3 { 4 public static List<MainItem> itemList = new List<MainItem>(); 5 public static List<TestItem> testItemList = new List<TestItem>(); 6 static TestPage () 7 { 8 cls_System cls_System = new cls_System(); 9 } 10 11 public TestPage () 12 { 13 InitializeComponent(); 14 15 ObservableCollection<TestItem> item = new ObservableCollection<Testtem>(); 16 17 for (int i = 0; i < itemList.Count; i++) 18 { 19 for (int j = 0; j < itemList[i]..Count; j++) 20 { 21 item.Add( 22 new TestItem 23 { 24 A = itemList[i].testList[j].A, 25 B = itemList[i].testList[j].B 26 }); 27 } 28 } 29 Test.ItemsSource = item; 30 } 31 32// 画面.xaml 33<Page 34 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 35 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 36 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 37 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 38 xmlns:sys="clr-namespace:System;assembly=mscorlib" 39 xmlns:Converters="clr-namespace:TestPG.Converters" 40 x:Class="TestPG.TestPage" 41 mc:Ignorable="d" 42 d:DesignHeight="462" d:DesignWidth="1000" > 43 44 <Page.Resources> 45 <Converters:ProductStatusToColorConverter x:Key="StatusToColorConverter"/> 46 </Page.Resources> 47 <Page.DataContext> 48 <Binding Path="Test" Source="{StaticResource Locator}" /> 49 </Page.DataContext> 50 51 <Grid> 52 <Grid.ColumnDefinitions> 53 <ColumnDefinition Width="9*"/> 54 <ColumnDefinition Width="43*"/> 55 <ColumnDefinition Width="160"/> 56 </Grid.ColumnDefinitions> 57 <Grid.RowDefinitions> 58 <RowDefinition Height="50"/> 59 <RowDefinition Height="*"/> 60 </Grid.RowDefinitions> 61 <Viewbox HorizontalAlignment="Left" Grid.ColumnSpan="2"> 62 <TextBlock Text="Xamlサンプル" VerticalAlignment="Center" FontWeight="Bold" /> 63 </Viewbox> 64 <Viewbox Grid.Column="2"> 65 <TextBlock Text="{Binding Mode=OneWay, Source={x:Static sys:DateTime.Now}, StringFormat=yyyy/MM/dd HH:mm, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True}" VerticalAlignment="Center" x:Name="Date"/> 66 </Viewbox> 67 <TextBlock Text="{Binding BlankMessage}" Padding="60" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2"/> 68 <DataGrid AutoGenerateColumns="False" IsReadOnly="True" x:Name="Test" ItemsSource="{Binding TestItem}" Grid.ColumnSpan="3" Grid.Row="1" Width="auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" FontSize="20" > 69 <DataGrid.Columns> 70 <DataGridTextColumn Width="*" Header="A" Binding="{Binding B}"></DataGridTextColumn> 71 <DataGridTextColumn Width="*" Header="B" Binding="{Binding B}"></DataGridTextColumn> 72 </DataGrid.Columns> 73 </DataGrid> 74 </Grid> 75</Page> 76 77// TestItemクラス 78namespace TestPG.Model 79{ 80 public class TestItem 81 { 82 public string A{ get; set; } 83 public string B{ get; set; } 84 } 85} 86 87// cls_System.cs エラーが発生している箇所 88cls_system{ 89 public void GetTestData(ref List<TesIttem> TestItem, ref string ErrorMessage){ 90/** 91DBとのデータ送受信処理 92*/ 93 TestPage.TestItemList.Add(data); 94 TestPage.itemList.Add(data); 95 96// 該当エラー箇所 サーバとの接続のためのインスタンス作成時にエラーが発生? 97 IPLinkService iPLinkService = new IPLinkService(); 98/** 99処理 100*/ 101 } 102} 103 104// IPLinkService.Designer.cs 105using System; 106namespace TestPG 107{ 108 partial class IPLinkService 109 { 110 private System.ComponentModel.IContainer components = null; 111 protected override void Dispose(bool disposing) 112 { 113 if (disposing && (components != null)) 114 { 115 components.Dispose(); 116 } 117 base.Dispose(disposing); 118 } 119 120 private void InitializeComponent() 121 { 122 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(IPLinkService)); 123 this.axIPLink1 = new AxIPLINKLib6.AxIPLink(); 124 ((System.ComponentModel.ISupportInitialize)(this.axIPLink1)).BeginInit(); 125 this.SuspendLayout(); 126 this.axIPLink1.Enabled = true; 127 this.axIPLink1.Location = new System.Drawing.Point(40, 58); 128 this.axIPLink1.Name = "axIPLink1"; 129 this.axIPLink1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axIPLink1.OcxState"))); 130 this.axIPLink1.Size = new System.Drawing.Size(36, 36); 131 this.axIPLink1.TabIndex = 0; 132 this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); 133 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 134 this.Controls.Add(this.axIPLink1); 135 this.Name = "IPLinkService"; 136 //以下の箇所でエラー 137 ((System.ComponentModel.ISupportInitialize)(this.axIPLink1)).EndInit(); 138 this.ResumeLayout(false); 139 } 140 141 public AxIPLINKLib6.AxIPLink axIPLink1; 142 } 143} 144 145
試したこと
何を直せばよいのか見当がつかなかった為、
Xaml内のPage.DataContextでBindingしているPathやSoureceを変更したり追加したりした。
DataGridで使用しているItemSourceのBindingしているリストの場所や記述を変更してみたりした。
補足情報(FW/ツールのバージョンなど)
FA-Pannel6 IPLink
VisualStudio2017

あなたの回答
tips
プレビュー