How do I convert string values to integers and back using IValueConverter?
- I have a database that consists of two tables; table CompanyX and table DeptY.
- Table CompanyX has field ID(int), firstName, lastName, Email, Phone.
- Table DeptY has field pID(int), Roles.
- DeptY pID is foreign key To CompanyX ID. Every time I select someone in the Combobox, I want it to display as their ID in a DataGrid.
This is my ItemTemplate below:
<Application.Resources>
<DataTemplate x:Key="myTemplate">
<WrapPanel HorizontalAlignment="Stretch">
<TextBlock Text="{Binding FirstName}"/>
<Label />
<TextBlock Text="{Binding LastName}"/>
</WrapPanel>
</DataTemplate>
</Application.Resources>
This is my Combobox which is bound to the ItemTemplate:
<ComboBox Height="23" HorizontalAlignment="Right" Margin="0,90,267,0"
Name="comboID" ItemsSource="{Binding}" VerticalAlignment="Top"
Width="208" ItemTemplate="{StaticResource myTemplate}" />
And a DataGrid which displays:
<DataGridTemplateColumn x:Name="pIDColumn" Header="Person ID" Width="auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=pID, Converter= {StaticResource myConverter}}"/>
<DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="rolesColumn" Header="Roles" Width="auto" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Roles}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
IValueConverter which is not converting!!
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string a = (string)value;
int b;
int.TryParse(a, out b);
return b;
}
public object ConvertBack(object value, Type targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}