Windows phone 7 访问列表框的完整数据模板
我正在开发一个WindowsPhone8应用程序,其中我使用一个列表框来显示多个详细信息。我的问题是,我想在代码隐藏ie中访问datatemplate,我想访问完整的datatemplate,并访问datatemplate中声明的所有子项 我只想更改listbox的datatemplate中的元素的可见性。请提出建议Windows phone 7 访问列表框的完整数据模板,windows-phone-7,silverlight-4.0,windows-phone-8,Windows Phone 7,Silverlight 4.0,Windows Phone 8,我正在开发一个WindowsPhone8应用程序,其中我使用一个列表框来显示多个详细信息。我的问题是,我想在代码隐藏ie中访问datatemplate,我想访问完整的datatemplate,并访问datatemplate中声明的所有子项 我只想更改listbox的datatemplate中的元素的可见性。请提出建议 提前感谢听起来您需要一个数据模板,它根据绑定到的对象的属性显示或隐藏某些元素。实现这一点的一个更好的方法是按照以下思路做一些事情: class MyData { ...
提前感谢听起来您需要一个
数据模板
,它根据绑定到的对象的属性显示或隐藏某些元素。实现这一点的一个更好的方法是按照以下思路做一些事情:
class MyData
{
...
public string Email {get {...} set {...}}
...
}
由于用户可能有电子邮件地址,也可能没有电子邮件地址,因此您的数据模板可以使用转换器将电子邮件的字符串值转换为可用于显示或隐藏字段的可见性
值。转换器的外观类似于:
public class StringNotNullToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string text = value as string;
if (!string.IsNullOrEmpty(text))
{
return Visibility.Visible;
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
您可以在XAML中添加对它的引用,如:
<phone:PhoneApplicationPage.Resources>
<this:StringNotNullToVisibilityConverter x:Key="StringNotNullToVisibilityConverter"/>
</phone:PhoneApplicationPage.Resources>
本质上说是“显示电子邮件,但如果电子邮件为空则隐藏此字段”。我找到了解决此问题的方法
public static T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
try
{
var count = VisualTreeHelper.GetChildrenCount(parentElement);
if (count == 0)
return null;
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
{
return (T)child;
}
else
{
var result = FindFirstElementInVisualTree<T>(child);
if (result != null)
return result;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return null;
}
public static T findfirstelementvisualtree(DependencyObject parentElement),其中T:DependencyObject
{
尝试
{
var count=VisualTreeHelper.GetChildrenCount(parentElement);
如果(计数=0)
返回null;
for(int i=0;i
我使用此方法查找数据模板中的第一个元素,然后更改该元素的可见性。
下面是一个如何使用此方法的示例
ListBoxItem item = this.lstboxMedicationList.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
CheckBox tagregCheckBox = FindFirstElementInVisualTree<CheckBox>(item);
tagregCheckBox.Visibility = Visibility.Visible;
lstboxMedicationList.UpdateLayout();
here i is the index of ListBox item.
ListBoxItem item=this.lstBoxMedicalationList.ItemContainerGenerator.ContainerFromIndex(i)作为ListBoxItem;
复选框tagregCheckBox=FindFirstElementVisualTree(项目);
tagregCheckBox.Visibility=可见性.Visibility;
lstboxMedicalationList.UpdateLayout();
这里我是列表框项的索引。
为什么要以这种方式更新数据模板?它不会更改您已经看到的当前创建的列表项。它期望您实际希望单独绑定/更新每个列表项。
ListBoxItem item = this.lstboxMedicationList.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
CheckBox tagregCheckBox = FindFirstElementInVisualTree<CheckBox>(item);
tagregCheckBox.Visibility = Visibility.Visible;
lstboxMedicationList.UpdateLayout();
here i is the index of ListBox item.