【发布时间】:2009-07-27 21:41:55
【问题描述】:
好的,这有点奇怪,但这基本上是我需要做的。我有一个绑定到 Document 对象的 WPF 控件。 Document 对象有一个 Pages 属性。所以在我的 ViewModel 中,我有一个 CurrentDocument 属性和一个 CurrentPage 属性。
现在,我有一个已绑定到 CurrentDocument.Pages 属性并更新 CurrentPage 属性的组合框。
<ComboBox ItemsSource="{Binding CurrentDocument.Pages}"
DisplayMemberPath="???"
SelectedItem="{Binding CurrentPage, Mode=TwoWay}">
</ComboBox>
到目前为止和我在一起?所有这一切都很好,除了我需要 DisplayMemberPath 来显示“第 1 页”、“第 2 页”等.....
我尝试创建这样的转换器:
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}}"
但它仍然不会出现在组合框文本中!!!
没有“索引”属性,但我不知道如何执行此操作...如何访问组合框绑定到的项目的索引...??????
【问题讨论】:
标签: wpf data-binding combobox