我想綁定存儲在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