本文介绍了使用IValueConverter t0将字符串值转换为int WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用IValueConverter将字符串值转换为整数并返回。
我有一个由两个表组成的数据库,表CompanyX和表DeptY。
表CompanyX有字段ID,firstName,lastName,电子邮件,电话。
表DeptY有字段dID,角色。
DeptY dID是外国的关键到公司X ID。
//为了简化故事,这是我的ItemTemplate:
how do I convert string values to integers and back using IValueConverter.
I have a database that consist of two tables, table CompanyX and table DeptY.
Table CompanyX has field ID, firstName, lastName, Email, Phone.
Table DeptY has field dID, Roles.
DeptY dID is foreign key To CompanyX ID.
//To cut the story short this is my ItemTemplate below:
<application.resources>
<datatemplate x:key="myTemplate" xmlns:x="#unknown">
<wrappanel horizontalalignment="Stretch">
<textblock text="{Binding FirstName}" />
<Label />
<textblock text="{Binding LastName}" />
</wrappanel>
</datatemplate>
</application.resources>
//我的组合框:
//My ComboBox:
<combobox height="23" horizontalalignment="Right" margin="0,90,267,0" name="cboID" itemssource="{Binding}" displaymemberpath="" verticalalignment="Top" width="208" itemtemplate="{StaticResource myTemplate}" />
//我的DataGrid:
//My DataGrid:
<datagridtemplatecolumn.celltemplate>
<datatemplate>
<textblock text="{Binding Path=TID, Converter={StaticResource myConverter}}" />
</datatemplate>
</datagridtemplatecolumn.celltemplate>
//我的IValueConverter:
//And my IValueConverter:
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();
}
有人可以帮助吗
Can someone help please
推荐答案
这篇关于使用IValueConverter t0将字符串值转换为int WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!