绑定到静态类的静态列表

Luk*_*kas 0 c# wpf binding list

我有静态类,当前的交易信息如下:

public static class BKM 
{
   public static List<Ticket> Tickets {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

我想绑定到XAML中的Tickets.Count属性.

当我输入这样的东西

<TextBlock Text="{Binding Source={x:Static p:BKM.Tickets.Count}}" />
Run Code Online (Sandbox Code Playgroud)

其中p是

xmlns:p="clr-namespace:TicketApplication"
Run Code Online (Sandbox Code Playgroud)

我收到错误

错误22不支持嵌套类型:BKM.Tickets.
错误21找不到类型'BKM.Tickets'.请注意,类型名称区分大小写.
错误23无法在目标类型上找到成员"计数".

Jon*_*eet 5

我怀疑一个问题是,你想要的来源BKM.Tickets,但你想要的路径Count.试试这个:

<TextBlock Text="{Binding Source={x:Static p:BKM.Tickets} Path=Count}" />
Run Code Online (Sandbox Code Playgroud)

正如斯里兰姆所说,你也应该建立Tickets一个财产,例如

public static List<Ticket> Tickets { get; set; }
Run Code Online (Sandbox Code Playgroud)

您还应该考虑摆脱全球状态,这很难进行测试和推理.