如何解决与收藏品的绑定减少到其属性之一
我想做这样的事情:<HierarchicalDataTemplate
x:Key=\"BuildingTemplate\"
ItemsSource=\"{Binding Path=RoomAccesses.Select(p => p.Room)}\"
ItemTemplate=\"{StaticResource ZoneTemplate}\">
<TextBlock Text=\"{Binding Path=Name}\" />
</HierarchicalDataTemplate>
当然,RoomAccesses.Select(p => p.Room)会给出语法错误,但您可以理解。我希望将roomaccesses-object中的所有房间都绑定在这里。
您有任何想法如何正确执行此操作吗?
谢谢!
解决方法
您还可以使用ѭ1来完成其他操作,例如这是一个简单的属性选择转换器:
public class SelectConverter : IValueConverter
{
public object Convert(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture)
{
if (!(value is IEnumerable)) throw new Exception(\"Input is not enumerable\");
IEnumerable input = ((IEnumerable)value);
var propertyName = parameter as string;
PropertyInfo propInfo = null;
List<object> list = new List<object>();
foreach (var item in input)
{
if (propInfo == null)
{
propInfo = item.GetType().GetProperty(propertyName);
if (propInfo == null) throw new Exception(String.Format(\"Property \\\"{0}\\\" not found on enumerable element type\",propertyName));
}
list.Add(propInfo.GetValue(item,null));
}
return list;
}
public object ConvertBack(object value,System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
XAML使用示例:
<ListBox ItemsSource=\"{Binding Data,Converter={StaticResource SelectConverter},ConverterParameter=Occupation}\"/>
,在您的DataContext中公开Rooms属性:
public IEnumerable<Room> Rooms
{
get { return RoomAccesses.Select(p => p.Room); }
}
并绑定到Rooms
而不是RoomAccesses
,为什么不保留绑定,如ItemsSource = \“ {Binding Path = RoomAccesses} \”,然后在数据模板中处理.Room属性呢?我的意思是使用PropertyPath很容易做到。
,在此示例中,您要绑定什么?
如果您可以编辑要绑定到的类,则可以向该类中添加一个属性,如下所示:
public IEnumberable<string> RoomsAccessed // replace string with the type of Room
{
get { return RoomAccesses.Select(p => p.Room); }
}
然后将您的绑定路径更新为仅RoomAccessed
(或任何您想调用的路径)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。