访问FlipView un XAML中的控件
在我的Windows 8应用程序中,我正在尝试更改FlipView的数据模板中的文本块的文本 我的FlipView数据模板(简化…):访问FlipView un XAML中的控件,xaml,controls,datatemplate,Xaml,Controls,Datatemplate,在我的Windows 8应用程序中,我正在尝试更改FlipView的数据模板中的文本块的文本 我的FlipView数据模板(简化…): 我尝试了这个解决方案: 因此,我的.cs: var _Container = flipView.ItemContainerGenerator.ContainerFromItem(flipView.SelectedItem); var _Children = AllChildren(_Container);
我尝试了这个解决方案:
因此,我的.cs:
var _Container = flipView.ItemContainerGenerator.ContainerFromItem(flipView.SelectedItem);
var _Children = AllChildren(_Container);
var myTextBlock= _Children.OfType<TextBlock>().FirstOrDefault(c => c.Name.Equals("test"));
myTextBlock.Text = "test";
var\u Container=flipView.ItemContainerGenerator.ContainerFromItem(flipView.SelectedItem);
var _Children=AllChildren(_容器);
var myTextBlock=_Children.OfType().FirstOrDefault(c=>c.Name.Equals(“test”);
myTextBlock.Text=“测试”;
使用以下方法:
public List<Control> AllChildren(DependencyObject parent)
{
var _List = new List<Control>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var _Child = VisualTreeHelper.GetChild(parent, i);
if (_Child is Control)
_List.Add(_Child as Control);
_List.AddRange(AllChildren(_Child));
}
return _List;
}
public列出所有子对象(DependencyObject父对象)
{
var_List=新列表();
for(int i=0;i
但我得到一个NullReferenceException错误:“{”对象引用未设置为对象的实例。“}”
所以它找不到我的文本块
谢谢你好,朋友,我检查了你的代码..我发现了一个非常不明显的错误..那是关于控件关键字..实际上,这是你想在flipview中搜索的控件类型..比如textblock,文本框等…你只需像这样更改AllChildren函数,然后一切都会正常工作
public List<TextBlock> AllChildren(DependencyObject parent)
{
var _List = new List<TextBlock>();
int j = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var _Child = VisualTreeHelper.GetChild(parent, i);
if (_Child is TextBlock)
_List.Add(_Child as TextBlock);
_List.AddRange(AllChildren(_Child));
}
return _List;
}
public列出所有子对象(DependencyObject父对象)
{
var_List=新列表();
int j=VisualTreeHelper.GetChildrenCount(父级);
for(int i=0;i
希望它能对您有所帮助。嘿,它对您有用吗?但我的FlipView中的gridView也有同样的问题,我又遇到了同样的问题:(
public List<TextBlock> AllChildren(DependencyObject parent)
{
var _List = new List<TextBlock>();
int j = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var _Child = VisualTreeHelper.GetChild(parent, i);
if (_Child is TextBlock)
_List.Add(_Child as TextBlock);
_List.AddRange(AllChildren(_Child));
}
return _List;
}