答案 0 :(得分:2)
答案 1 :(得分:1)
一种选择是绑定到资源。
SomeClass
。xmlns:local="clr-namespace:BindingTest"
。<local:SomeClass x:key="context"/>
。DataContext="{Binding Source={StaticResource context}}"
,虽然您也可以在代码背后执行此操作。最后,我将textbox.text绑定到资源。 Text="{Binding Source={StaticResource context}, Path=Foo}"
。
<UserControl.Resources>
<local:SomeClass x:Key="context" />
</UserControl.Resources>
<StackPanel x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource context}}">
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding}" />
<TextBlock Text="{Binding Source={StaticResource context}, Path=Foo}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
背后的代码:
using System.Collections.Generic;
using Microsoft.Phone.Controls;
namespace BindingTest
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
}
public class SomeClass
{
private IList<string> _data = new List<string>() { "foo", "bar", "baz" };
public IList<string> Items
{
get
{
return _data;
}
}
public string Foo { get { return "the moon"; } }
}
}
答案 2 :(得分:0)