【问题标题】:UWP Swipeitem won't see any bindingsUWP Swipeitem 不会看到任何绑定
【发布时间】:2020-01-06 19:21:36
【问题描述】:

由于某种原因,我无法对 UWP Xaml SwipeItem 控件使用任何绑定。我已经以许多不同的方式和不同的 SwipeItem 属性尝试过它,但每次它都是空的。更奇怪的是任何类型的 x:Bind 到任何属性都会崩溃。

如果有人标记这个: SwipeItem XAML Binding ignored

作为一个重复的问题,所以不要这样做,否则我会吓坏的。这个问题甚至没有得到回答。

<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>

DataContext 只是一个简单的 DataModel,所有其他控件都可以很好地绑定。该命令来自静态资源,并且该命令可以正常触发。在此示例中,当尝试将 ANYTHING 绑定到 Text 或 CommandParamter 属性时,Binding 或 x:Bind 的任何组合都不会执行任何操作或崩溃。 SwipItem 控件一定有问题,我需要一种通过 CommandParameter 传递 DataContext 的方法。

【问题讨论】:

  • 当我使用 x:bind 将 DataModel 中的 String 属性绑定到 SwipeItem 的 Text 并将 DataModel 绑定到 CommandParamter 时,效果很好。那么您能否为我们提供更多详细信息以重现此问题?
  • 如果我执行任何 x:Bind 操作,它就会崩溃。我在它旁边的控件上尝试了 {BInding ElementName="Textbox", Path="Text"} 并且它为空。无论我做什么,swipeitem 中的任何属性都是空的。我已经编程了很长时间,并且已经完成了数千次绑定。我已经尽可能地简化了它,它在我尝试过的几十种场景中都不起作用。此示例在 UserControl 中,绑定是来自简单数据模型的数据上下文。 UC 中的所有其他控件绑定良好
  • 在一个新项目上再次尝试“使用:Microsoft.UI.Xaml.Controls”。 SwipeItem 不会显示来自任何东西的绑定。我可以开始工作的唯一绑定是 {Binding RelativeSource={RelativeSource Mode=Self}}。然后它将返回自身而不是 null。但其他一切都是空的。当我现在尝试 x:Bind 时,它只会显示它自己的属性。看不到其他任何东西。它在自己的小泡泡里,看不到其他人。

标签: c# binding uwp-xaml


【解决方案1】:

SwipeControl不是标准的itemControl,它没有dataTemplate,所以SwipeItem找不到父视图的DataContext,所以不能直接在xaml中直接使用Binding。看来你只能在代码中使用Binding。(下面我以LeftItems为例)。

在xml中:

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

在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;

}

【讨论】:

  • 哈!我知道有些事情很奇怪,我没有疯哈哈!非常感谢您对此进行验证并提供解决方案。实际上,无论如何,从代码中做对我来说会更好
猜你喜欢
  • 2019-05-02
  • 2015-12-21
  • 2023-02-14
  • 2018-08-31
  • 2019-02-17
  • 2019-01-28
  • 2020-10-27
  • 2017-11-08
  • 2023-03-11
相关资源
最近更新 更多