我想绑定存储在ViewModel中的DataTemplate中的数据。我已经尝试了几种方法,但没有成功,WPF的解决方案似乎不能像WinSource的RelativeSource的AncestorType属性一样工作。WinRT在DataTemplate中绑定ViewMode的数据
<Page.DataContext>
<local:ViewModel x:Name="ViewModel" />
</Page.DataContext>
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView ItemsSource="{x:Bind ViewModel.names}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:mydatatype">
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<!--Here I want a TextBlock to show the number-->
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
这里是视图模型
public class ViewModel
{
public int Number = 42;
public List<mydatatype> names = new List<mydatatype>();
public ViewModel()
{
names.Add(new mydatatype("name1"));
names.Add(new mydatatype("name2"));
}
}
public class mydatatype
{
public string Name { get; set; }
public mydatatype(string name)
{
this.Name = name;
}
}
请提供更多代码。因为现在没有足够的信息。例如,我没有看到“名称”在您ViewModel –
检查了这一点 http://stackoverflow.com/questions/33070705/with-compiled-bindings-xbind-why-do-i-have-to-call-绑定更新 – NotAGenie
@AlexejSommer我更新了ViewModel,很抱歉造成了不便。 – NickThomsan