在组合框中绑定物品使用转换器 [英] Use converter on bound items in combobox
问题描述
我有这势必会像这样的数据表列的组合框:
i have a combobox which is bound to a datatable column like this:
ComboBox.DataContext = DataDataTable;
ComboBox.DisplayMemberPath = DataDataTable.Columns["IDNr"].ToString();
的
IDNR在列总是使用4个字母后面的ID号(例如BLXF1234)。
我需要而不信(我需要1234显示在组合框),以显示在ComboBox中的项目
The IDNr in the Column always starts with 4 letters followed with the ID Number (ex. BLXF1234) . I need to display the items in Combobox without the Letters (i need 1234 to be displayed in the combobox).
所以我写了一个转换器:
So i wrote a converter :
class IDPrefixValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
string s = value.ToString();
if (s.Contains("BL"))
{
return s.Substring(4);
}
else
{
return s;
}
}
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
没有,我怎么能告诉组合框使用转换器显示的项目?
I在XAML尝试这样的:
No, how can i tell the combobox to use the converter to display the items ? i tried this in the Xaml:
ItemsSource="{Binding}"
DisplayMemberPath="{Binding Converter={StaticResource IDPrefixValueConverter}}"
但仍然没有工作...任何想法?
谢谢
But still not working ...any ideas ? Thanks
推荐答案
您可以修改的ItemTemplate
的组合框
并使用您的转换器:
You can modify the ItemTemplate
of the ComboBox
and use your converter:
<ComboBox ItemsSource="{Binding}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource IDPrefixValueConverter}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
每个项目绑定到的ItemsSource $ C $项目C>。在你能够执行你想要转换的结合使用转换器。
Each item is bound to the items in the ItemsSource
. By using the converter in the binding you are able to perform the conversion you want.
这篇关于在组合框中绑定物品使用转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!