C# Windows应用商店应用绑定列表视图来自列表和来自;“自我”;
C# Windows应用商店应用绑定列表视图来自列表和来自;“自我”;,c#,windows,xaml,binding,store,C#,Windows,Xaml,Binding,Store,我在窗口存储应用程序中将数据绑定到ListView时遇到问题。我想要的是,将listview绑定到一个集合,并在listview数据模板中显示项目(这是可行的)。我遇到的问题是,当我想在这个模板中显示一个变量时,它并没有在集合中定义,但在类中是公共的 让我展示一些代码,以解决我的问题。 我在XAML中定义了布局: <Page.Resources> <DataTemplate x:Key="template"> <StackPanel Orie
我在窗口存储应用程序中将数据绑定到ListView时遇到问题。我想要的是,将listview绑定到一个集合,并在listview数据模板中显示项目(这是可行的)。我遇到的问题是,当我想在这个模板中显示一个变量时,它并没有在集合中定义,但在类中是公共的
让我展示一些代码,以解决我的问题。
我在XAML中定义了布局:
<Page.Resources>
<DataTemplate x:Key="template">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}"/>
<TextBlock Text="{Binding Test}" Margin="20 0" />
</StackPanel>
</DataTemplate>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView ItemTemplate="{StaticResource template}" ItemsSource="{Binding Items}" x:Name="listView" Margin="40">
</ListView>
</Grid>
背后的代码是:
public List<string> Items { get; set; }
public string Test { get; set; }
public MainPage()
{
this.InitializeComponent();
Items = new List<string>()
{
"test 1", "test 2" , "test 3"
};
Test = "TEST!";
listView.DataContext = this;
}
公共列表项{get;set;}
公共字符串测试{get;set;}
公共主页()
{
this.InitializeComponent();
Items=新列表()
{
“测试1”、“测试2”、“测试3”
};
Test=“Test!”;
listView.DataContext=这个;
}
我需要设置什么,将TextBlock正确绑定测试变量?试试这个
<Page.Resources>
<DataTemplate x:Key="template">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}"/>
<TextBlock Text="{Binding ElementName=listView, Path=DataContext.Test}" Margin="20 0" />
</StackPanel>
</DataTemplate>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView ItemTemplate="{StaticResource template}" ItemsSource="{Binding Items}" x:Name="listView" Margin="40">
</ListView>
</Grid>
绑定到网格的DataContext.Test或ListView的DataContext.Test