无法将属性绑定到TextBox

时间:2018-02-11 17:55:43

标签: c# wpf xaml mvvm data-binding

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),已经使用DataContext进行了测试ObjectDataProvider,但它没有用。我想我忘了什么,但不知道应该是什么。

1 个答案:

答案 0 :(得分:0)

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

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

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

请注意,您的RootObject a目前在ExecuteMyMethod结尾处被销毁,您可能希望在此更改逻辑以保留对a.Coord值的引用不知。

相关问题