我的selectionchanged事件没有更新视图,我缺少什么?

时间:2017-04-26 15:36:42

标签: c# web-services listview template10 selectionchanged

所有字段在数据绑定时都标记为TwoWay,但显而易见我有错误。我所拥有的是一个页面,显示了在视图的一侧添加新设备的视图,以及另一侧的设备列表。我要做的是从listview中选择项目时,它将更新值在TextBox中进行查看和编辑。

当我创建新设备时,保存选项(下面的代码中未显示)当前有效,并将刷新列表。但是,现在我完成时会回到框架。我想点击保存时刷新ListView。

来自XAML页面的值

    private Device _ActiveDevice;
    private int _HostName;
    //All Variables Created for DataBinding to XAML page

    //public int HostName { get { return _HostName; } set { _ActiveDevice.HostName = value; } }
    public int HostName { get; set; }
    public string DriveModel { get; set; }
    public string DriveSN { get; set; }

        public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> suspensionState)
    {

        Value = (suspensionState.ContainsKey(nameof(Value))) ? suspensionState[nameof(Value)]?.ToString() : parameter?.ToString();
        await Task.CompletedTask;

        var uri = new Uri("http://localhost:2463/api/Devices");
        HttpClient client = new HttpClient();

        try
        {
            var JsonResponse = await client.GetStringAsync(uri);
            var devicesResult = JsonConvert.DeserializeObject<List<Device>>(JsonResponse);
            Devices = devicesResult;

            _ActiveDevice = JsonConvert.DeserializeObject<List<Device>>(JsonResponse)[0];
        }
        catch
        {
            MessageDialog dialog = new MessageDialog("Unable to Access WebService at this Time!");
            await dialog.ShowAsync();
        }

        //client.Dispose();
    }    

    public void deviceList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        var device = ((sender as ListView).SelectedItem as Device);
        _ActiveDevice = device;

        HostName = device.HostName;
        DriveModel = device.DriveModel;
        DriveSN = device.DriveSN;
    }

来自ViewModel的代码

Sub AdvancedSearchComplete()
    Dim rsts As Outlook.Results
    Dim i As Integer
    Dim strF As String
    Dim strS As String
    strS = "Tasks"
    StrName = InputBox("Search String?")
    strF = InStr(LCase("urn:schemas:tasks:subject"), StrName)

    Set sch = Application.AdvancedSearch(strS, strF, , "Search1")
End Sub

2 个答案:

答案 0 :(得分:0)

您必须从INotifyPropertyChanged继承视图模型,以使绑定知道值的更新。

public class MainViewModel: INotifyPropertyChanged
{

    public int HostName { get => hostName; set { hostName = value; OnPropertyChanged("HostName"); } }
    private int hostName;

    ...

    void OnPropertyChanged(String prop)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
    }
    public event PropertyChangedEventHandler PropertyChanged;

}

将绑定(以及所有其他绑定)更新为:

<TextBox PlaceholderText="Host Name" Text="{Binding ViewModel.HostName, UpdateSourceTrigger=PropertyChanged}" Name="hostNameTB" AcceptsReturn="True" />

答案 1 :(得分:0)

我离开了..我的解决方案更多的是Rufo先生提出的建议.. XAML是正确的,但我需要设置属性的Get和Set来更新属性,然后确保选择的设备是更新每个属性。

private int _HostName;
public int HostName { get { return _HostName; } set { Set(ref _HostName, value); } }
public void deviceList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        var device = ((sender as ListView).SelectedItem as Device);
        //_ActiveDevice = device;

        HostName = device.HostName;
        DriveModel = device.DriveModel;
        DriveSN = device.DriveSN;