以编程方式更新UWP绑定
我遇到了一个问题,如果在列表中选择了一个项目,我希望它在我的网格中更新我的项目。该绑定通过以下方式完成:以编程方式更新UWP绑定
<ScrollViewer Grid.Row="1">
<ItemsControl x:Name="RightGridItemsControl" ItemsSource="{Binding News}" ItemTemplate="{StaticResource RightGridTemplate}"/>
</ScrollViewer>
当物品行星被选中,我想更新ItemsSource绑定到一个新列表。这是在我的DataModel中指定的。
如何以编程方式更新?我试过这样的事情,但它需要一个DependencyObject
,并不能找出它的含义。这也看起来像WPF而不是UWP。
`var myBinding = new Binding
{
Source = Planets,
Mode = BindingMode.OneWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
BindingOperations.SetBinding(new , ItemsControl.ItemsSourceProperty, myBinding);`
作为'SetBinding'
的连接器的第一项,我该怎么说?
感谢
可以这样设置绑定:
BindingOperations.SetBinding(
RightGridItemsControl, ItemsControl.ItemsSourceProperty, myBinding);
或像这样:
RightGridItemsControl.SetBinding(ItemsControl.ItemsSourceProperty, myBinding);
还要注意的是目前还存在于你的绑定没有财产路径。如果在您的XAML中有News
属性,那么Binding应该如下所示,没有Mode = BindingMode.OneWay
(默认值),也没有UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
,这对单向绑定没有影响。
var myBinding = new Binding
{
Source = Planets,
Path = new PropertyPath("News")
};
如何引用'RightGridItemsControl'?它仅在XAML中设置为字符串,并且我没有任何智能感知。 –
我也使用viewmodel,而不是背后的代码。 –
'x:Name =“RightGridItemsControl”'在XAML的类中创建一个字段。 – Clemens
第一个参数应该是'RightGridItemsControl',别忘了在'Binding'类中设置'Path'属性。 – tao
如何在我的RightGridItemsControl的viewmodel中获得一个引用? –