简体   繁体   English

XAML绑定到代码隐藏类属性集合为空(WPF)

[英]XAML binding to a codebehind class property collection is blank (WPF)

My codebehind defines a simple class with properties and a constructor as so: 我的代码背后定义了一个具有属性和构造函数的简单类:

public class Question
{
    public string[] Answers
    {
        get; set;
    }
    public int CorrectAnswerIndex
    {
        get; set;
    }
    public Question(string[] answers, int correctIndex)
    {
        this.Answers = answers;
        this.CorrectAnswerIndex = correctIndex;
    }
}

There then exists a public object of that type that gets initialised in the window's constructor like so: 然后,存在一个该类型的公共对象,该对象在窗口的构造函数中初始化,如下所示:

 CurrentQuestion = new Question(
     new string[] { "First", "Second", "Third", "Fourth" }, 2
 );

I then have the following XAML in an attempt to print out all of the possible answers from said question. 然后,我尝试使用以下XAML来打印出上述问题的所有可能答案。

<Grid Margin="150,150,150,150" DataContext="local:CurrentQuestion">
        <ListBox ItemsSource="{Binding Answers}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
</Grid>

The local namespace is defined previously as the CLR namespace. 本地名称空间先前定义为CLR名称空间。

However, my list emerges entirely empty. 但是,我的清单完全空白。 There are no binding errors at runtime. 在运行时没有绑定错误。

What's going on here? 这里发生了什么? It seems a simple example that just won't run. 看来这是一个简单的示例,它将无法运行。 I feel I've missed something "obvious." 我觉得我错过了一些“显而易见的”事情。

This will look at ListBox.DataContext for a property named Answers , and try to use that for the ItemsSource . 这将在ListBox.DataContext查找名为Answers的属性,并尝试将其用于ItemsSource

<ListBox ItemsSource="{Binding Answers}">

ListBox.DataContext will be inherited from the parent Grid . ListBox.DataContext将从父Grid继承。 Unfortunately, the grid's DataContext is a string, and strings don't have a property called Answers . 不幸的是,网格的DataContext是一个字符串,并且字符串没有名为Answers的属性。 So the Binding can't do anything and gives you null . 因此, Binding无法执行任何操作,并且为您提供了null

<Grid Margin="150,150,150,150" DataContext="local:CurrentQuestion">
    <ListBox ItemsSource="{Binding Answers}">

XAML implicit conversions are a Do-What-I-Mean thing, thus a source of much confusion. XAML隐式转换是“按我所想”的事情,因此引起很多混乱。 There are times when you can put local:CurrentQuestion in an attribute value and have it be taken as a data type -- but this is not one of those times. 有时您可以将local:CurrentQuestion放在属性值中,并将其作为数据类型-但这不是其中一种情况。 And a data type isn't what you meant to provide anyway. 而且,数据类型并不是您要提供的。 You wanted a property by that name. 您想要一个以此名称命名的物业。 But local: is a namespace, a literal CLR namespace like System.Windows.Controls , not a reference to an object. 但是local:是一个名称空间,是文字的CLR名称空间,例如System.Windows.Controls ,而不是对对象的引用。

Here's how the XAML in a UserControl can bind to a property of the UserControl . 下面是在XAML中如何UserControl可以绑定到的属性UserControl If it's a Window , change UserControl to Window . 如果是Window ,请将UserControl更改为Window

<Grid Margin="150,150,150,150">
    <ListBox 
        ItemsSource="{Binding CurrentQuestion.Answers, RelativeSource={RelativeSource AncestorType=UserControl}}">

I'm just guessing that CurrentQuestion is a property of the UserControl . 我只是在猜测CurrentQuestionUserControl的属性。 Let me know if it's somewhere else. 让我知道是否在其他地方。

You're also probably going to run into problems when you update CurrentQuestion , unless it's a dependency property. 除非它是依赖项属性,否则在更新CurrentQuestion时,您可能还会遇到问题。 If it's a plain old CLR property like this, the UI won't be notified when its value changes: 如果它是像这样的普通旧CLR属性,则其值更改时不会通知UI:

public Question CurrentQuestion { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM