好的,这有点奇怪,但基本上是我需要做的事情。我有一个绑定到文档对象的WPF控件。文档对象有一个页面属性。因此在我的ViewModel中,我有一个CurrentDocument属性和一个CurrentPage属性。
现在,我有一个下拉框,它绑定到CurrentDocument.Pages属性并更新CurrentPage属性。
<ComboBox ItemsSource="{Binding CurrentDocument.Pages}"
DisplayMemberPath="???"
SelectedItem="{Binding CurrentPage, Mode=TwoWay}">
</ComboBox>
到目前为止还可以理解吗?所有的东西都很好,除了我需要DisplayMemberPath来显示"Page1"、"Page2"等等...
我尝试创建这样一个转换器:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string pageNumber = "Page {0}";
return string.Format(pageNumber, value);
}
然后尝试将 DisplayMemberPath 绑定到它:
DisplayMemberPath="{Binding RelativeSource={RelativeSource Self}, Path=Index, Converter={StaticResource pgTitleConv}}"
但是它仍然不会显示在组合框文本中!!!
没有"Index"属性,但我不知道如何做这个...我该如何访问组合框绑定的项目的索引...??????