Sim*_*ely 10 .net c# data-binding xaml windows-8
我创建了一个简单的C#Windows 8网格应用程序.
如果您不熟悉此布局,请在此处对其进行简要说明:
我想要的是简单的 - 一些习惯ItemDetailPages.我希望能够点击的一些项目GroupDetailPage和GroupedItemsPage并导航到自定义.xaml文件,一个在那里我可以包括一个以上的图像.
我确信有一个简单的方法,我错过了,我也确信这些信息对很多人都有用,所以我会在这个问题上提供赏金.
到目前为止,我一直在努力做到这一点:
我CustomDataItem在SampleDataSource.cs课堂上创建了一个:
/// <summary>
/// Generic item data model.
/// </summary>
public class CustomDataItem : SampleDataCommon
{
public CustomDataItem(String uniqueId, String title, String subtitle, String imagePath, String description, String content, SampleDataGroup group)
: base(uniqueId, title, subtitle, imagePath, description)
{
this._content = content;
this._group = group;
}
private string _content = string.Empty;
public string Content
{
get { return this._content; }
set { this.SetProperty(ref this._content, value); }
}
private SampleDataGroup _group;
public SampleDataGroup Group
{
get { return this._group; }
set { this.SetProperty(ref this._group, value); }
}
}
Run Code Online (Sandbox Code Playgroud)
但是,显然,加入了 ObservableCollection
private ObservableCollection<SampleDataGroup> _allGroups = new ObservableCollection<SampleDataGroup>();
public ObservableCollection<SampleDataGroup> AllGroups
{
get { return this._allGroups; }
}
Run Code Online (Sandbox Code Playgroud)
使用不同的数据类型是不可能的.那么在这种情况下我该怎么办?
非常感谢.
我有一个简单的网格应用程序;如何使组项目页面中的元素之一链接到自定义项目详细信息页面?
好的,让我们看一下使用 Visual Studio 中的“网格应用程序”模板生成的应用程序。
组项目页面上元素的数据类是类SampleDataItem。您可以做的是添加某种类型的数据字段(bool、int或其他)来指示如何处理导航。在这个例子中,我们为了保持简单,所以我们添加了一个bool来指示导航是否是自定义的。
public class SampleDataItem : SampleDataCommon
{
// add flag as last param
public SampleDataItem(String uniqueId, String title, String subtitle,
String imagePath, String description, String content, SampleDataGroup group,
bool isCustomNav = false)
: base(uniqueId, title, subtitle, imagePath, description)
{
this._content = content;
this._group = group;
this.IsCustomNav = isCustomNav;
}
// to keep it simple this doesn't handle INotifyPropertyChange,
// as does the rest of the properties in this class.
public bool IsCustomNav { get; set; }
...
}
Run Code Online (Sandbox Code Playgroud)
因此,当您添加SampleDataItem要显示的新对象时,只需isCustomNav在构造函数中设置该字段即可。
现在我们要做的就是更改分组项页面 (GroupedItemsPage.xaml.cs) 上网格中现有的单击事件处理程序:
void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
// Navigate to the appropriate destination page, configuring the new page
// by passing required information as a navigation parameter
var item = (SampleDataItem)e.ClickedItem;
var itemId = item.UniqueId;
if (item.IsCustomNav == false)
{
// default
this.Frame.Navigate(typeof(ItemDetailPage), itemId);
}
else
{
// custom page
this.Frame.Navigate(typeof(ItemDetailPage2), itemId);
}
}
Run Code Online (Sandbox Code Playgroud)
我们上面所做的就是获取所选项目,然后测试我们之前添加的导航标志。基于此,我们导航到原始的ItemDetailPage或名为 的新的ItemDetailPage2。正如我之前提到的,导航标志不一定是bool. 它可以是int或enum或某种其他类型,告诉我们导航到哪里。
请注意,如果您希望在 上有类似的行为GroupDetailsPage,您只需以相同的方式更新单击事件处理程序即可。
希望有帮助。