[英]Binding to Code Behind will not work
我有一個按鈕,它調用一個方法,用List<string>
路徑填充List<string>
。 每次調用此方法以顯示生成的所有圖像時,我都會嘗試更新Windows 8應用程序。 目前它不會顯示任何內容,盡管只是將圖像路徑硬編碼到List<string>
我顯示圖像的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();
}
}
我做錯了什么/需要在這里改變? 非常感謝 :)。
您需要設置MainPage的DataContext。 把它放在你的MainPage 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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.