【问题标题】:Can't bind properties to TextBox无法将属性绑定到文本框
【发布时间】:2018-02-11 17:55:42
【问题描述】:

RootObject.cs

public class RootObject 
{
    private const string api = "";
    private const string mode = "json";
    private const string url = "http://api.openweathermap.org/data/2.5/weather?q=";

    public Coord coord { get; set; }

    public void Get(string city)
    {
        JObject jsonData = JObject.Parse(new WebClient().DownloadString(url + city + "&appid=" + api + "&mode=" + mode));

        coord = new Coord(jsonData.SelectToken("coord"));

    }
}

Coord.cs

public class Coord
{
    private double lon;
    public double Lon
    {
        get { return lon; }
        set { lon = value;}
    }

    private double lat;
    public double Lat
    {
        get { return lat; }
        set { lat = value;}
    }

    public Coord(JToken coorddata)
    {
        this.Lon = Convert.ToDouble(coorddata.SelectToken("lon"));
        this.Lat = Convert.ToDouble(coorddata.SelectToken("lat"));
    }
}

ViewModel.cs

public class ViewModel 
{
    public ICommand MyCommand { get; set; }

    public ViewModel()
    {
        MyCommand = new RelayCommand(ExecuteMyMethod, CanExecuteMyMethod);
    }

    private string city;
    public string City
    {
        get { return city; }
        set { city = value; }
    }

    private bool CanExecuteMyMethod(object parameter)
    {
        if (string.IsNullOrEmpty(City))
        {
            return false;
        }
        else
        {
            if (City != "")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    private void ExecuteMyMethod(object parameter)
    {
        RootObject a = new RootObject();
        a.Get(City);
    }
}

MainWindow.xaml

<Window.Resources>
  <vm:ViewModel x:Key="viewModel"></vm:ViewModel>
</Window.Resources>
<Grid >
    <StackPanel>
        <StackPanel DataContext="{Binding Source={StaticResource viewModel}}">
            <TextBox Width="100" Height="30" Text="{Binding City, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
            <Button Width="100" Height="30" Command="{Binding MyCommand }">pussh</Button>
        </StackPanel>
        <StackPanel DataContext="{Binding Source={???}}">
            <Label></Label>
            <TextBox Width="100" Height="100" Text="{Binding Path=Lon}"></TextBox>
            <TextBox Width="100" Height="50" Text="{Binding Path=Lat}"></TextBox>
        </StackPanel>
    </StackPanel>
</Grid>

我是 MVVM 方面的新手。
我尝试将属性LatLon 绑定到XAML 中的文本框(单击按钮后将显示LatLon),已经使用DataContextObjectDataProvider 进行了测试,但它不起作用.我想我忘记了一些东西,但不知道它应该是什么。

【问题讨论】:

    标签: c# wpf xaml mvvm data-binding


    【解决方案1】:

    您需要阅读INotifyPropertyChanged interface,如果您希望在 ViewModel/Model 属性更改时更新您的 Binding,它是您的 ViewModels 和 Models 需要实现的接口。一旦您对INotifyPropertyChanged 的工作原理有了基本的了解,您就必须决定是直接将LatLon 属性添加到您的ViewModel,还是为您的RootObject 创建第二个ViewModel 并绑定到它。

    另外,一个好的做法是将DataContext设置为页面的根元素(即:Window),如果不指定,DataContext由children继承。

    很遗憾,如果没有INotifyPropertyChanged,您将无法使您的Binding 显示任何更新后的值到LatLon

    请注意,您的 RootObject a 目前正在 ExecuteMyMethod 的末尾被销毁,您可能需要在此处更改逻辑以以某种方式保留对 a.Coord 值的引用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-12
      • 2011-01-13
      • 2023-04-06
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 2018-07-02
      相关资源
      最近更新 更多