可以将 IValueConverter 与 List 一起使用。它可以在我第一次调用菜单时使用。当我更新列表中的项目时,它不会再次调用 IValueConverter?例子:
<MenuItem Header="{Binding Path=DataContext.Documents, RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListView}}, Converter={StaticResource DocumentsToString}, Mode=OneWay}">
<MenuItem.Icon>
<Image Source="Images/upload.png" Style="{StaticResource ImageContextMenu}"/>
</MenuItem.Icon>
</MenuItem>
和 ValueConverters.cs
public class ListDocumentToStringConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var selectedDocuments = (ObservableCollection<Document>) value;
var result = "";
foreach (var document in selectedDocuments)
{
result += document.Name + "\t";
}
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}