Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绑定响应。数据模板混淆_C#_Wpf_Binding - Fatal编程技术网

C# 绑定响应。数据模板混淆

C# 绑定响应。数据模板混淆,c#,wpf,binding,C#,Wpf,Binding,我还是个新手,正在尝试我的第一个严肃的数据绑定。我已经读了很多关于它是如何工作的,我只是在为这个具体的例子而挣扎。我已经试着阅读了所有我能找到的链接,但大多数资料在关键点上都有点不精确。下面是: -我的应用程序动态生成“List”类型的变量“PlayerList”,其中“Player”是一个复杂对象 -我想通过绑定将其显示在列表框中。显然,由于播放器是一个复杂的对象,我想为它创建一个数据模板。我在“Window1.xaml”中有类似的内容: <ListBox Name="List

我还是个新手,正在尝试我的第一个严肃的数据绑定。我已经读了很多关于它是如何工作的,我只是在为这个具体的例子而挣扎。我已经试着阅读了所有我能找到的链接,但大多数资料在关键点上都有点不精确。下面是:

-我的应用程序动态生成“List”类型的变量“PlayerList”,其中“Player”是一个复杂对象

-我想通过绑定将其显示在列表框中。显然,由于播放器是一个复杂的对象,我想为它创建一个数据模板。我在“Window1.xaml”中有类似的内容:

<ListBox 
    Name="ListBox_Players" 
    ItemsSource="{Binding Source={StaticResource PlayerListResource}}"
    ItemTemplate="{StaticResource PlayerTemplate}">
</ListBox>  

在“App.xaml”中有类似的内容:

<DataTemplate x:Key="PlayerTemplate">  <!-- DataType="{x:Type Player}" --> 
    <StackPanel Orientation="Horizontal">
      <TextBlock Text="{Binding Path=name}"/>
      <TextBlock Text=", "/>
      <TextBlock Text="{Binding Path=nrOfTabls}"/>
    </StackPanel>
</DataTemplate>

当然,这个模板稍后会变得更加冗长。正如您在上面看到的,我试图为PlayerList变量创建一个资源,但还没有管理好,即smthn。像这样

<src: XXX x:Key="PlayerListResource"/>

其中对于XXX,据我所知,我应该输入资源变量的类。我试过了

List<Player>, List<src:Player>
列表,列表
等等,但obv.XAML在“”字符方面有问题


我还有另一个问题:通过不声明资源而是直接绑定(即,在C中编写“ListBox_Players.ItemsSource=PlayerList;”)和删除“ItemTemplate”声明并覆盖Player类的ToString()方法来输出播放器的名称,我已经设法看到绑定是有效的(即,我在列表框中得到一个球员名单)但是,如果我再次插入模板,它只会显示“,”我的模板不起作用!

事实上,你得到的只是逗号,没有任何其他内容,这向我表明,
玩家
成员的名称与
数据模板
中的
路径=
中的名称不匹配(我曾经遇到过这个问题),或无法访问相关的
Player
成员

到目前为止,我刚刚测试了您所显示的代码,它似乎运行良好。我所做的唯一更改是更改这一行:

ItemsSource="{Binding Source={StaticResource PlayerListResource}}"
ItemsSource = "{Binding}"
关于这一行:

ItemsSource="{Binding Source={StaticResource PlayerListResource}}"
ItemsSource = "{Binding}"
这告诉程序它将在运行时获取ItemsSource

我的
Player
课程是:

class Player {
    public string name { get; set; }
    public int nrOfTabls { get; set; }
}
我的
MainWindow.xaml.cs
是:

public partial class MainWindow : Window {
    private ObservableCollection<Player> players_;

    public MainWindow() {
        InitializeComponent();

        players_ =new ObservableCollection<Player> () {
            new Player() {
                name = "Alex",
                nrOfTabls = 1,
            },
            new Player() {
                name = "Brett",
                nrOfTabls = 2,
            },
            new Player() {
                name="Cindy",
                nrOfTabls = 231,
            }
        };

        ListBox_Players.ItemsSource = players_;
    }
}
公共部分类主窗口:窗口{
私人可观测收集玩家;
公共主窗口(){
初始化组件();
玩家=新的可观察集合(){
新玩家(){
name=“Alex”,
nrOfTabls=1,
},
新玩家(){
name=“Brett”,
nrOfTabls=2,
},
新玩家(){
name=“Cindy”,
nrOfTabls=231,
}
};
ListBox_Players.ItemsSource=Players;
}
}

您能否发布播放器的代码以及与上述XAML对应的C#代码?(例如,如果上述XAML位于MailWindow.XAML中,则发布MainWindow.XAML.cs)嘿,ikh,my.cs很长,但是关于这个问题的部分没有什么特别的。正如我在下面写的,它似乎是通过用属性替换类中的字段来解决的。虽然我没有找到一个来源告诉我,我可以绑定的对象实例的内部内容是prorepties,而不仅仅是字段。这确实是还有一个非常有说服力的理由总是使用属性!但是另一个问题呢,即,如果我想添加对类列表“PlayerList”的引用。我如何在XAML中实现它?嘿,ikh,谢谢你的努力!如果你看看你的代码,你做的正是我在问题的最后几行中推测的:你的“name”和“nrOfTabl”s是属性而不是字段。几分钟前我试过了,瞧,绑定成功了。所以你似乎只能绑定对象实例的属性,而不是字段。这通常是真的吗?@ToaoG哇,我不知道这对属性不起作用!反射处理属性与字段不同,这可能就是原因。只是想澄清一下,因为您写的是相反的,所以它对属性有效,但对字段无效。