c# - x :Bind works but Binding doesn't (opposite to most Q&A found)

标签 c# xaml win-universal-app uwp uwp-xaml

我在 StackOverflow 上发现的大多数问答都是关于绑定(bind)如何工作的,但 x:Bind 不是,这通常由 Bindings.Update() 解决。但是,我的问题是,在 GridView 中,ItemSource="{x:Bind _myList}" 有效,但 ItemSource="{Binding _myList}" 没有。

为什么?我如何使 Binding 工作? (而不是 x:Bind)

这里有一些代码:

类:

public class MyClass
{ 
    public string prop1 {get; set;}
    public string prop2 {get; set;} 
}

public class MyList : List<MyClass>
{ 
    public void Populate()
    // Add items 
}

代码隐藏

public MyList _myList = new MyList();
_myList.Populate();
DataContext = this;
Bindings.Update();

XAML(在这里不起作用,但如果 ItemSource: 更改为 x:Bind _myList 则起作用)

<GridView ItemSource="{Binding _myList}">
 <GridView.ItemTemplate>
  <DataTemplate>
   <StackPanel>
    <TextBlock Text="{Binding prop1}"/> <TextBlock Text="{Binding prop2}/>
   </StackPanel>
  </DataTemplate>
 </GridView.ItemTemplate>
</GridView>

最佳答案

问题是您的 _myList 是字段,而不是属性。所以,改成

public MyList _myList { get; set; } = new MyList();

关于c# - x :Bind works but Binding doesn't (opposite to most Q&A found),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38863989/

相关文章:

c# - 构建通用应用程序 - 允许客户特定的选项

c# - 如何增加表达到的数量?

c# - 在 Google Play 控制台中分析或收集 ANR 的 Xamarin 堆栈跟踪

C# 数据集、分页和大量数据

c# - Silverlight:在 XAML 中声明数据集合?

.net - 如何使矩形延伸到整个网格?

c# - 在 UWP 页面中查找所有 TextBox 控件

WPF:如何从 DLL 资源文件设置图像源?

c# - W10 UWP - 将远程图像设置为桌面墙纸/锁屏

c# - 窗口手机8.1 : Change Background Color of Button