
C# .NET6 WPFの質問です。
C#
1public partial class MainWindow : Window 2 { 3 public MainWindow() 4 { 5 InitializeComponent(); 6 7 8 } 9 } 10 11 public class Model 12 { 13 public string? Label { get; set; } 14 public string? Longitude { get; set; } 15 public string? Latitude { get; set; } 16 } 17 18 public class ViewModel 19 { 20 public ObservableCollection<Model> Models { get; set; } 21 public ViewModel() 22 { 23 this.Models = new ObservableCollection<Model>(); 24 this.Models.Add(new Model() { Label = "Tokyo", Latitude = "35.5090627", Longitude = "139.2093882" }); 25 } 26 27 }
xaml
1 <Grid.DataContext> 2 <local:ViewModel /> 3 </Grid.DataContext> 4 <syncfusion:SfMap x:Name="Maps"> 5 <syncfusion:SfMap.Layers> 6 <syncfusion:ShapeFileLayer Uri="./Japan.shp" 7 Markers="{Binding Models}"> 8 </syncfusion:ShapeFileLayer> 9 </syncfusion:SfMap.Layers> 10 </syncfusion:SfMap>
Grid.DataContextの中にあるViewModelをコード側から動的に変更したいです。
しかし、
C#
1var Point = new ViewModel(); 2 Point.Models.Add(new Model() { Label = "Tokyo", Latitude = "35.5090627", Longitude = "139.2093882" });
このようにインスタンスを作って追加しても変更できませんでした。どのようにすれば変更できるでしょうか。

> AddExecute();と書いてもビルドでエラーが発生します。
ViewModel 云々とか関係なくて、もっとごく基本的なところがダメで、(1) ViewModel クラスの private インスタンスメソッドだから、(2) そこを解決しても AddExecute(); では引数がないからということでは?
public void AddExecute(string Label, string Latitude, string Longitude)にしても「現在のコンテキストに'AddExecute'という名前は存在しません」というエラーが出ます。引数を指定しても同じことが発生しました。とりあえず、その部分のコードを修正します。

インスタンスメソッドだから呼び出すにはそれが定義されているクラスのインスタンスを生成する必要があると思うのですが・・・

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