Reputation: 4105
I'm still new to wpf so this might be a pretty simple question but I wasn't able to find a solution anywhere
I have a Combobox that I've bound to an ObservableCollection of bytes. Once I populate the list I want the values to be displayed in hex format with '0x' in the beginning
so for e.g if the list contains
0
120
255
then the combobox should display
0x00
0x78
0xFF
How do I do this without any code behind and in the simplest way possible?
Note - I tried using the ItemStringFormat property but I wasn't able to get it to display in the way I wanted
Upvotes: 2
Views: 1647
Reputation: 61369
This should do what you want:
<ComboBox ItemsSource="{Binding Path=testArray}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ., StringFormat=0x{0:X2}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Basically, you are replacing the default "String" item with the above item template that allows you to use a more usable custom formatting string.
The "." Binding binds to the whole item object (in this case, the byte), and the format string is the same kind of string you could pass to String.Format in code-behind.
StringFormat documentation can be found at: http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.stringformat(v=vs.110).aspx
The number format strings can be found at:http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx
Upvotes: 5