简体   繁体   English

WP8-将命令绑定到列表中的按钮和开关

[英]WP8 - Binding commands to buttons and switches in list

I am using MVVM pattern and I have LongListSelector in my page but I am not sure how to do these bindings: 我正在使用MVVM模式,并且我的页面中有LongListSelector,但是我不确定如何执行以下绑定:

  • I want in each row a button which do something with object in that row. 我希望每一行都有一个按钮,该按钮可以对该行中的对象进行处理。 I have this prepare in ViewModel: 我在ViewModel中准备了这个:

     private RelayCommand<Module> goToTrackingPageCommand; public RelayCommand<Module> GoToTrackingPageCommand { get { return goToTrackingPageCommand ?? (goToTrackingPageCommand = new RelayCommand<Module>( NavigateToTrackingPage)); } } private void NavigateToTrackingPage(Module module) { App.Current.SelectedModule = module; navigationService.NavigateTo(new Uri("/Views/ModuleTrackingPage.xaml"), UriKind.Relative); } 

    And I am trying to bind it like this: 我试图像这样绑定它:

     <Button x:Name="ShowButton" Content="Show" Command="{Binding GoToTrackingPageCommand}" CommandParameter="{Binding}"/> 

    It's not working because button is in datatemplate and when there is binding it goes to selected Module object but not to ViewModel. 它不起作用,因为按钮在数据模板中,并且在绑定时,它转到选定的Module对象,而不是ViewModel。 So my first question is how can I fix it? 所以我的第一个问题是如何解决?

  • Second one is little complicated I guess but hope both get easy solution. 第二个我不太复杂,但希望两者都能轻松解决。 I want have in each row ToggleSwitch too and when value is changed I want to call http request. 我也希望每一行中都有ToggleSwitch,并且当值更改时,我想调用http请求。 I have this in datatemplate: 我在datatemplate中有这个:

     <toolkit:ToggleSwitch x:Name="LockSwitch" IsChecked="{Binding IsLock}" /> 

    I could change binding to TwoWay but I change value in object and I want to call method in ViewModel with Module argument. 我可以更改对TwoWay的绑定,但是我更改对象中的值,并且想在ViewModel中使用Module参数调用方法。 So have can I change this binding? 那么我可以更改此绑定吗? Should I someway call method in ViewModel from my object? 我是否应该从对象中调用ViewModel中的方法? Or should I somehow tell ViewModel that this object has changed this value? 还是应该以某种方式告诉ViewModel该对象已更改此值? Or should I bind Checked and Unchecked event? 还是应该绑定Checked和Unchecked事件?

Regarding the Buttons: You can access the "parent datacontext" with an elementName binding and set the command parameter: 关于按钮:可以使用elementName绑定访问“父datacontext”并设置命令参数:

<Button Command="{Binding DataContext.GoToXyCommand, ElementName=LayoutRoot}" CommandParameter="{Binding}" />

Regarding your second question: 关于第二个问题:

First, I would check if a toggle-button is the right solution, if changing the value triggers a process with a possibly longer duration. 首先,我将检查切换按钮是否是正确的解决方案,如果更改值会触发可能持续较长时间的过程。

An example where WP does this is enabling/disabling Air-Plane Mode. WP执行此操作的一个示例是启用/禁用Air-Plane模式。

I would do it the same way: 我将以相同的方式进行操作:

  • Bind to the property via TwoWayBinding 通过TwoWayBinding绑定到属性
  • When the property is changed, start the updating process, disable the toggle button and show a progress indicator. 更改属性后,开始更新过程,禁用切换按钮并显示进度指示器。

EDIT: Here is and example from a ViewModel I recently used. 编辑:这是我最近使用的ViewModel的示例。

    public bool IsUpdatingPushEnabled
    {
        get { return _isUpdatingPushEnabled; }
        set { SetProperty(ref _isUpdatingPushEnabled, value); }
    }

    public bool IsPushEnabled
    {
        get { return _isPushEnabled; }
        set
        {
            if (!IsUpdatingPushEnabled)
            {
                SetProperty(ref _isPushEnabled, value);
                var t = SetPushAsync();
            }
        }
    }

    private async Task SetPushAsync()
    {
        IsUpdatingPushEnabled = true;

        try
        {
            var result = await _settingService.EnablePushAsync(IsPushEnabled);
            SetProperty(ref _isPushEnabled, result, "IsPushEnabled");
        }
        catch
        {
            //...
        }

        IsUpdatingPushEnabled = false;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM