SwipeItem XAML Binding ignored

落爺英雄遲暮 提交于 2020-02-07 03:19:16

问题


I am having trouble getting any binding to work for a SwipeItem within a RadListView (which is similar to a standard ListView). In particular, I am trying to bind to the Command property; however, I have attempted to bind to other properties, e.g., Text, but to no avail.

<telerikDataControls:RadListView ItemsSource ="{Binding Users, Mode=OneWay}">
    <telerikDataControls:RadListView.ItemTemplate>
        <DataTemplate>
             <SwipeControl>
                    <SwipeControl.RightItems>
                        <SwipeItems>
                            <SwipeItem Text="Delete"
                                       Background="Red"
                                       Foreground="White"
                                       Command="{Binding DeleteCommand}">
                                <SwipeItem.IconSource>
                                    <SymbolIconSource Symbol="Delete"/>
                                </SwipeItem.IconSource>
                            </SwipeItem>
                        </SwipeItems>
                    </SwipeControl.RightItems>
                    <Grid>
                        <TextBlock Text="{Binding Name"/>
                    </Grid>
             </SwipeControl>
        </DataTemplate>
    </telerikDataControls:RadListView.ItemTemplate>
</telerikDataControls:RadListView>

Users is set in the constructor of the ViewModel for the View; it is an ObservableCollection of UserViewModel, each of which has the properties I am trying to use (with PropertyChanged events).

The Name binding works in the Grid further down in the template and I have checked the DataContext of the SwipeControl and it is a UserViewModel. In my testing I have set up an Event on the SwipeItem:

 <SwipeItem Text="Delete"
            Background="Red"
            Foreground="White"
            Command="{Binding DeleteCommand}"
            Invoked="SwipeItem_Invoked">

and handled it in the code-behind:

private void SwipeItem_Invoked(SwipeItem sender, SwipeItemInvokedEventArgs args)
{
    UserViewModel userToDelete = (UserViewModel)args.SwipeControl.DataContext;
}

I can see in here that sender.Command is null.

Obviously, the quick solution is to use the Event pattern; however, I am trying to keep it MVVM and avoid code-behind as much as possible. I have never had issues binding to properties before so imagine I am just doing something fundamentally wrong.

Thanks.

Edit:

public class UserViewModel : MvxNotifyPropertyChanged // MvvmCross
{
    public IMvxAsyncCommand DeleteCommand { get; }

    private string _name;
    public string Name // this property is bound in the Grid but will not bind in the SwipeItem
    {
        get => _name;
        set => SetProperty(ref _name, value);
    } 
}

回答1:


If the Invoked event is firing, but you're unable to get the Command binding to work, then you could try using XAML Behaviors (aka Blend Behaviors) to wire your command up to the event that is firing. This should then allow you to keep your code MVVM friendly.

First, install the Microsoft.Xaml.Behaviors.Uwp.Managed nuget package, then alter your XAML to the following:

<SwipeItem xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
           xmlns:core="using:Microsoft.Xaml.Interactions.Core"
           Text="Delete"
           Background="Red"
           Foreground="White">
    <interactivity:Interaction.Behaviors>
        <core:EventTriggerBehavior EventName="Invoked">
            <core:InvokeCommandAction Command="{Binding Path=DeleteCommand}" />
        </core:EventTriggerBehavior>
    </interactivity:Interaction.Behaviors>
    <SwipeItem.IconSource>
        <SymbolIconSource Symbol="Delete"/>
    </SwipeItem.IconSource>
</SwipeItem>

To keep the answer concise, I've defined the interactivity and core xml namespaces within the SwipeItem element - but these would normally be moved to the top-level element in your XAML.




回答2:


As your Command is inside the DataTemplate its BindingContext is Item so defining the Command in the ViewModel won't work. There are various ways to resolve this, e.g. you could define Command in the item or to bind to the proper context.



来源:https://stackoverflow.com/questions/53560468/swipeitem-xaml-binding-ignored

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!