我有一个 C# 和 XAML 项目,其中涉及绑定到名为 myList 的幻灯片的 ObservableCollection 的 GridView。幻灯片有两个感兴趣的属性 - 名称和描述。默认情况下,GridView 中的项目显示名称属性,但我想要一个按钮在显示名称和显示描述之间切换。
幻灯片摘录:
public class Slide : INotifyPropertyChanged {
private string _Name;
private string _Description;
public string Name {
get {
return this._Name;
} set {
//snipped for convenience: check that the property has changed and set it
}
}
public string Description {
get {
return this._Description;
} set {
//snipped for convenience: check that the property has changed and set it
}
}
/*etc...*/
}
XAML 现在看起来像这样:
<GridView /*...*/ ItemsSource="{Binding myList}">
<GridView.ItemTemplate>
<DataTemplate>
<Grid /*...*/ >
<TextBlock /*...*/ Text="{Binding Name}" />
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
当用户单击一个按钮时,我想以编程方式更改绑定到 DataTemplate 中 TextBlock 的 Text 字段的属性,从而更改 GridView 中的每个项目。我知道如何为单个 XAML 元素执行此操作,但我不确定如何为 GridView 中的动态内容执行此操作。有小费吗?
我剪掉了一堆看起来多余的代码,但如果您需要任何其他信息,请告诉我。
谢谢!