以编程方式更新UWP绑定

时间:2016-11-21 03:18:10

标签: c# .net wpf data-binding uwp

我遇到了一个问题,如果在列表中选择了某个项目,我希望它更新我的网格中的项目。绑定由:

完成
<ScrollViewer Grid.Row="1">
                <ItemsControl x:Name="RightGridItemsControl" ItemsSource="{Binding News}" ItemTemplate="{StaticResource RightGridTemplate}"/>
</ScrollViewer>

当一个项目,例如选择了Planet,我想将ItemsSource绑定更新为新列表。这在我的DataModel中指定。

如何以编程方式更新此内容?我尝试过这样的事情,但它需要一个DependencyObject并且无法找到它的含义。这也看起来像WPF而不是UWP。

`var myBinding = new Binding
                    {
                        Source = Planets,
                        Mode = BindingMode.OneWay,
                        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                    };
                    BindingOperations.SetBinding(new , ItemsControl.ItemsSourceProperty, myBinding);`

我应该将'SetBinding'的结构的第一项作为什么?

1 个答案:

答案 0 :(得分:2)

您可以像这样设置Binding:

BindingOperations.SetBinding(
    RightGridItemsControl, ItemsControl.ItemsSourceProperty, myBinding);

或者像这样:

RightGridItemsControl.SetBinding(ItemsControl.ItemsSourceProperty, myBinding);

另请注意,目前Binding中没有属性路径。如果您的XAML中存在News属性,那么Binding应该如下所示,没有Mode = BindingMode.OneWay,这是默认值,而没有UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,这对于单向约束。

var myBinding = new Binding
{
    Source = Planets,
    Path = new PropertyPath("News")
};
相关问题