Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.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#_Windows_Xaml_Windows 8 - Fatal编程技术网

C# 绑定到代码隐藏将不起作用

C# 绑定到代码隐藏将不起作用,c#,windows,xaml,windows-8,C#,Windows,Xaml,Windows 8,我有一个按钮,它调用一个方法,用图像路径填充列表。每次调用此方法以显示生成的所有图像时,我都尝试更新Windows 8应用程序。 目前它不会显示任何内容,尽管只是将图像路径硬编码到列表中 显示图像的XAML代码为: <ItemsControl ItemsSource="{Binding Path=test}" Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="5" HorizontalContentAlignment="

我有一个按钮,它调用一个方法,用图像路径填充
列表。每次调用此方法以显示生成的所有图像时,我都尝试更新Windows 8应用程序。
目前它不会显示任何内容,尽管只是将图像路径硬编码到
列表中

显示图像的XAML代码为:

        <ItemsControl ItemsSource="{Binding Path=test}" Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="5"
      HorizontalContentAlignment="Stretch">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Image Source="{Binding}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

在Xaml.cs中:

public sealed partial class MainPage : TestApp.Common.LayoutAwarePage
{

    public List<string> test = new List<string>();
    public MainPage()
    {
        test.Add("C:\\Users\\user\\Pictures\\image.jpg");
        this.InitializeComponent();
    }
}
公共密封部分类主页面:TestApp.Common.LayoutAwarePage
{
公共列表测试=新列表();
公共主页()
{
test.Add(“C:\\Users\\user\\Pictures\\image.jpg”);
this.InitializeComponent();
}
}

我做错了什么/需要在这里改变?非常感谢:)。

您需要设置主页的DataContext。把这个放在你的主页xaml上

DataContext="{Binding RelativeSource={RelativeSource Self}}"
编辑:代码隐藏上需要有属性,而不是字段:

public List<string> test {get; set;}
public MainPage()
{
    test = new List<string>();
    test.Add("C:\\Users\\user\\Pictures\\image.jpg");
    this.InitializeComponent();
}
公共列表测试{get;set;}
公共主页()
{
测试=新列表();
test.Add(“C:\\Users\\user\\Pictures\\image.jpg”);
this.InitializeComponent();
}

非常感谢您的回答……我已经添加了这个,但图像仍然没有显示:(您的输出窗口中是否有任何绑定错误?items控件中是否至少显示了项目,但没有图像?很遗憾,没有显示任何内容。这是从输出窗口生成的错误消息(非常感谢您告诉我检查这一点,我根本没有意识到输出了绑定问题!)错误:BindingExpression路径错误:“test”属性未在“TestApp.MainPage”上找到。BindingExpression:path='test'DataItem='TestApp.MainPage';目标元素是“Windows.UI.Xaml.Controls.ItemsControl”(Name='null'));目标属性为“ItemsSource”(类型为“Object”)。哦……这是因为您的代码背后有一个公共字段……而不是公共属性。我将编辑我的答案以澄清这一点。非常感谢。非常感谢:)