【发布时间】:2012-06-02 02:51:13
【问题描述】:
可以将 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();
}
}
【问题讨论】:
-
转换器仅在
Documents属性获得新值时调用;如果Documents属性引用的集合保持不变,则不会调用转换器,即使Document项目已添加到该集合。您真的想将所有文档名称写入一个MenuItem吗?或者您想为每个文档创建一个MenuItem?如果是后者,请将您的收藏分配给Menu.ItemsSource。 -
我使用 Document.SelectedCount 并且如果我单击 Document 中的另一个项目,它会再次调用转换器
标签: wpf data-binding ivalueconverter