Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.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# 数据不存在';在文本={Binding}上不显示?_C#_Silverlight_Xaml_Windows Phone 7_Binding - Fatal编程技术网

C# 数据不存在';在文本={Binding}上不显示?

C# 数据不存在';在文本={Binding}上不显示?,c#,silverlight,xaml,windows-phone-7,binding,C#,Silverlight,Xaml,Windows Phone 7,Binding,我是silverlight和Windows7手机开发的新手。我不确定我错过了什么,但很明显,我错过了一些东西,因为它没有按预期工作 我的目标是显示一个生物列表,只有它们的名字和生命值。但是整个文本={Binding}的东西显然不起作用。所以我想知道你们中是否有人能帮我解决这个问题 当我说它不工作时,是因为数据在生物列表中,但不在页面/文本块中-它显示了适当数量的生物,但只是没有数据 XAML 看起来应该是Text={Binding Name}或Text={Binding HitPoints} 编

我是silverlight和Windows7手机开发的新手。我不确定我错过了什么,但很明显,我错过了一些东西,因为它没有按预期工作

我的目标是显示一个生物列表,只有它们的名字和生命值。但是整个文本={Binding}的东西显然不起作用。所以我想知道你们中是否有人能帮我解决这个问题

当我说它不工作时,是因为数据在生物列表中,但不在页面/文本块中-它显示了适当数量的生物,但只是没有数据

XAML


看起来应该是Text={Binding Name}或Text={Binding HitPoints}

编辑:但是,Text={Binding Path=Name}或Text={Binding Path=HitPoints}也可以


编辑2:对不起,我没有注意到你的评论。我的电脑中没有VS,所以我不能自己尝试,但请尝试将数据模板上的设置为“生物”

将您的绑定更新为以下内容。我已经把这个生物从捆绑路径上扔了下来。那么它应该会起作用

<StackPanel Orientation="Vertical" Height="40">
  <TextBlock Width="100" FontSize="22" Text="Name:" Height="40"/>
  <TextBlock Width="100" Text="{Binding Path=Name}" Height="40"/>
</StackPanel>
<StackPanel Orientation="Vertical" Height="40">
   <TextBlock Width="100" FontSize="22" Text="Hitpoints:" Height="40"/>
   <TextBlock Width="100" Text="{Binding Path=HitPoints}" Height="40"/>
 </StackPanel>


由于您要绑定到一个生物列表,因此不需要输入bioture.Name。您应该能够将其更改为Text={Binding Name}和Text={Binding Hitpoints}

始终使用直接绑定绑定到DataContext,并且在将ItemsSource设置为列表时,DataContext将成为列表中它将表示的每一行的每个项目。所以你在这里的想法是完全正确的

然而,ContentControl的行为也是如此。设置
ContentControl
Content
时,基本上覆盖了内容的DataContext。因此,
DataContext
被设置为你的StackPanel,它将自身呈现为你的StackPanel,但你也将尝试绑定到你的StackPanel,而不再绑定到你的生物对象

因此,您可能需要这样做:

将您的内容StackPanel移动到DataTemplate,将此DataTemplate设置为按钮上的ContentTemplate,并将按钮的内容设置为生物对象的绑定,如下所示:

<Button Width="400" Height="120" Content="{Binding}">
    <Button.ContentTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal" Height="80" Width="200">
            <StackPanel Orientation="Vertical" Height="40">
            <TextBlock Width="100" FontSize="22" Text="Name:" Height="40"/>
            <TextBlock Width="100" Text="{Binding Path=Name}" Height="40"/>
            </StackPanel>
            <StackPanel Orientation="Vertical" Height="40">
            <TextBlock Width="100" FontSize="22" Text="Hitpoints:" Height="40"/>
            <TextBlock Width="100" Text="{Binding Path=HitPoints}" Height="40"/>
            </StackPanel>
        </StackPanel>
    </DataTemplate>
    </Button.ContentTemplate>
</Button>

我处理这些情况的首选方法是为视图设置一个集合。它看起来像这样

public class CreatureList : ObservableCollection<Creature>
{
    // at least implement the constructor
}
在类的构造函数中,将属性绑定到指定的XAML定义

<ResourceDictionary>
    <local:CreatureList x:Key="creatures" />
</ResourceDictionary>
<ListBox Name="creatureListBox" ItemsSource="{Binding Source={StaticResource creatures}}">
    <!-- Template definition for each entry -->
</ListBox>
public DamageTrackerPage() {
    InitializeComponent();
    creatureList = FindResource("creatures") as CreatureList;
}
现在,当您向列表中添加条目或从列表中删除条目时,更改将自动更新到您的窗口中


至少我在WPF中是这样做的,但我相信WinPhone应用程序也应该是一样的。

你到底在哪里调用LoadBioters?我不太确定,但似乎去掉bindng表达式中的类名就可以了。所以它应该是Text={Binding Path=Name}。@Felice Pollano我更新了我的帖子,包含了关于LoadBioter发生在哪里的信息@我尝试了你的建议,但似乎没有任何改变:(还有其他想法吗?编辑:顺便问一下,它怎么知道我希望访问该生物类并将其用作template@Hans-Henrik绑定表达式将在项目即将显示时求值。然后求值器将使用当前对象上的指定名称调用属性。对象的类型实际上不是relevant。当找不到属性时,将出现错误。由于您正在xaml中设置与creatureList的绑定,请删除listBox.ItemsSource=creatureList。此外,在调用LoadBioters后将datacontext设置为该值(仅因为尚未设置属性更改通知)。现在它的文本={binding Name},但它仍然无法工作,我是否遗漏了什么?是因为该类位于另一个文件夹中吗?DataTemplate没有DataType:(好的,尝试以下操作:1)使creatureList成为代码隐藏类的属性。2)在调用LoadBioters后,将DataContext设置为该属性。3)在列表框的xaml中,设置ItemsSource={Binding creatureList}看看这是否有效。很抱歉,我希望这台计算机上有VS可以自己尝试。不起作用。。我愿意重做我所有的工作,如果你们中的任何人可以指导或删除一个链接,以指导如何做到这一点,因为我在这里疯了:P但我只是重新粘贴我的代码在我的第一篇文章,让你可以看到我这样做了什么。
<ResourceDictionary>
    <local:CreatureList x:Key="creatures" />
</ResourceDictionary>
<ListBox Name="creatureListBox" ItemsSource="{Binding Source={StaticResource creatures}}">
    <!-- Template definition for each entry -->
</ListBox>
public partial class DamageTrackerPage : PhoneApplicationPage
{
    private readonly CreatureList creatureList;
}
public DamageTrackerPage() {
    InitializeComponent();
    creatureList = FindResource("creatures") as CreatureList;
}