我有一个使用collectionsource的列表视图,并希望根据ListViewItems在列表中的索引是否与视图模型中保存的值相匹配来设置背景颜色。为此,我将listVeiwItem的背景绑定到ViewModel中的值,并使用转换器确定背景色。为了进行此确定,需要向转换器传递ListViewItem的索引。如何使用XAML获取索引?
这是ListView使用的数据模板的XAML:
<DataTemplate x:Key="ILMemberTemplate">
<StackPanel Orientation="Horizontal" Background="{Binding Path=ListIndex, Mode=OneWay, Converter={StaticResource ParticipantBackground}, ConverterParameter={???}}">
<TextBlock
Width="200"
TextAlignment="Left"
Foreground="{Binding Path=IsPC, Mode=OneWay, Converter={StaticResource ParticipantColour}}"
Text="{Binding Path=Name, Mode=OneWay}"/>
<TextBlock
Width="40"
TextAlignment="Center"
Foreground="{Binding Path=IsPC, Mode=OneWay, Converter={StaticResource ParticipantColour}}"
Text="{Binding Path=Initiative, Mode=OneWay}"/>
</StackPanel>
</DataTemplate>
答案 0 :(得分:0)
您无法在XAML中获取索引并将其作为 items: [{
title: 'Please select a date:',
width: 330,
bodyPadding: 10,
items: [{
xtype: 'datepicker',
disabledDays: [0,6],
minDate: Ext.Date.add(new Date(), Ext.Date.DAY, -42),
maxDate: Ext.Date.add(new Date(), Ext.Date.DAY, -7),
listeners: {
select: function (picker, date) {
console.log(date);
}
}
传递。这是不可能的。但是您可以使用多转换器并绑定到ConverterParameter
和父ListIndex
容器,例如:
ListViewItem
XAML:
class MultiConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var listIndex = values[0];
//...
ListViewItem item = value as ListViewItem;
ListView lv = FindParent<ListView>(item);
ICollectionView view = lv.ItemsSource as ICollectionView;
IEnumerator e = view.GetEnumerator();
int index = 0;
while (e.MoveNext())
{
if (Equals(item.DataContext, e.Current))
return index;
else
index++;
}
//return some brush based on the indexes..
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
var parent = VisualTreeHelper.GetParent(dependencyObject);
if (parent == null) return null;
var parentT = parent as T;
return parentT ?? FindParent<T>(parent);
}
}