Ok, this is kind of odd but this is basically what I need to do. I have a WPF control that is bound to a Document object. The Document object has a Pages property. So in my ViewModel, I have a CurrentDocument property, and a CurrentPage property.
Now, I have a combobox that I have bound to the CurrentDocument.Pages property and updates the CurrentPage property.
<ComboBox ItemsSource="{Binding CurrentDocument.Pages}"
DisplayMemberPath="???"
SelectedItem="{Binding CurrentPage, Mode=TwoWay}">
</ComboBox>
With me so far? All of this is fine except that I need the DisplayMemberPath to show "Page 1", "Page 2", etc.....
I tried creating a converter such as this:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string pageNumber = "Page {0}";
return string.Format(pageNumber, value);
}
And tried to bind DisplayMemberPath to it like this:
DisplayMemberPath="{Binding RelativeSource={RelativeSource Self}, Path=Index, Converter={StaticResource pgTitleConv}}"
But it still wouldn't show up in the combo box text!!!
There is no "Index" property but I don't know how to do this...How do I access the index of the item that the combobox is binding to...??????
try this:
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource pgTitleConv}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
and in your valueconverter, if you can access the pages collection, you can use CurrentDocument.Pages.IndexOf(value) to get the index of the bound item. I'm sure there is a better way though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With