0

For some reason I can't use any binding with a UWP Xaml SwipeItem Control. I've tried it in many different ways and with different SwipeItem properties but every time it is null. What's even stranger is any type of x:Bind to any property and it will crash.

if anyone marks this: SwipeItem XAML Binding ignored

as a duplicate question it isn't so don't do it or I'll freak out. That question wasn't even answered.

<SwipeControl>

        <SwipeControl.LeftItems>
            <SwipeItems Mode="Execute">
                <SwipeItem Text="{Binding}" Background="{StaticResource MIIGreen}" BehaviorOnInvoked="Close"/>
            </SwipeItems>
        </SwipeControl.LeftItems>

        <SwipeControl.RightItems>
            <SwipeItems Mode="Execute">
                <SwipeItem Background="{StaticResource MIIRed}" BehaviorOnInvoked="Close" Command="{StaticResource CommandEnclosureNotInstalled}" CommandParameter="{Binding}"/>
            </SwipeItems>
        </SwipeControl.RightItems>

</SwipeControl>

the DataContext is just a simple DataModel and all other controls are binding fine. the command is from a staticresource and the command is firing just fine. in this example any combination of Binding or x:Bind either does nothing or crashes when trying to bind ANYTHING to Text or CommandParamter properties. There has to be something wrong with SwipItem controls, I need a way to pass the DataContext through the CommandParameter.

  • When I use x:bind to bind the String property in DataModel to the Text of SwipeItem and bind the DataModel to the CommandParamter, it works well. So can you show more details for us to reproduce this issue? – Faywang - MSFT Sep 04 '19 at 07:10
  • If I do any x:Bind at all it crashes. I've tried {BInding ElementName="Textbox", Path="Text"} on a control next to it and it is null. no matter what I do it is null for any property in the swipeitem. I've been programming a long time and have done thousands of bindings. I've simplified this as much as it can and it doesn't work in any of the dozens of scenarios I've tried. This example is in a UserControl, the binding is a datacontext from a simple data model. all other controls in the UC bind fine – beatnikthedan Sep 04 '19 at 14:19
  • Tried it again "using:Microsoft.UI.Xaml.Controls" on a new project. SwipeItem won't show bindings from anything. The only binding I can get to work is {Binding RelativeSource={RelativeSource Mode=Self}}. then it will return itself and not null. but everything else is null. When I try x:Bind now it only brings up its own properties. can't see anything else. it's in it's own little bubble and can't see anyone else. – beatnikthedan Sep 04 '19 at 14:30

1 Answers1

1

SwipeControl is not a standard itemControl, it does not have a dataTemplate, so SwipeItem can't find the DataContext of the parent view, so you can't directly use Binding directly in xaml. It seems you can only use Binding in code.(Below I give example of LeftItems).

in xaml:

<SwipeControl Margin="50" LeftItems="{Binding leftItems}">
</SwipeControl>

in cs:

public SwipeItems leftItems { get; set; }
public MainPage()
{
    this.InitializeComponent();
    SwipeItem leftItem = new SwipeItem();
    Binding myBinding = new Binding();
    myBinding.Source = viewmodel;
    myBinding.Path = new PropertyPath("MyText"); //the property in viewmodel
    BindingOperations.SetBinding(leftItem, SwipeItem.CommandParameterProperty, myBinding);
    BindingOperations.SetBinding(leftItem, SwipeItem.TextProperty, myBinding);

    Binding commandBinding = new Binding();
    commandBinding.Source = new FavoriteCommand(); //command class
    BindingOperations.SetBinding(leftItem, SwipeItem.CommandProperty, commandBinding);

    leftItems = new SwipeItems() {
        leftItem
    };
    this.DataContext = this;

}
Faywang - MSFT
  • 5,798
  • 1
  • 5
  • 8
  • Hah! I knew something was wonky and I wasn't crazy haha! Thanks a lot for verifying this and providing a solution. Actually, doing it from code will work better for me anyway – beatnikthedan Sep 06 '19 at 21:54