使用IValueConverter t0将字符串值转换为int WPF [英] using IValueConverter t0 convert string values to int WPF

查看:238
本文介绍了使用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将值转换为整数和返回值。

我有一个由两个表组成的数据库,表CompanyX和表DeptY。

表CompanyX有字段ID(int),firstName,lastName,Email,Phone。

表DeptY有字段pID(int),角色。

DeptY pID是外键到CompanyX ID。

每次我在Combobox中选择某人时,我希望它在DataGrid中显示为ID。



//为了简化故事,这是我的ItemTemplate:

< Application.Resources>

< DataTemplate x:Key =myTemplate >

< WrapPanel Horizo​​ntalAlignment =Stretch>

< TextBlock Text ={Binding FirstName}/>

<标签/>

< TextBlock Text ={Binding LastName}/>

< / WrapPanel>

< ; / DataTemplate>

< /Application.Resources>



这是我的Combobox绑定到ItemTemplate:



< ComboBox Height =23Horizo​​ntalAlignment =右Margin =0,90,267,0Name =comboIDItemsSource ={Binding}VerticalAlignment =TopWidth =208ItemTemplate ={StaticResource myTemplate}/>



和一个DataGrid显示:



< DataGridTemplateColumn x:Name =pIDColumnHeader =Person IDWidth = auto>

< DataGridTemplateColumn.CellTemplate>

< DataTemplate>

< TextBlock Text ={Binding Path = pID ,Converter = {StaticResource myConverter}}/>

< / DataTemplate>

< /DataGridTemplateColumn.CellTemplate>

< / DataGridTemplateColumn>

< DataGridTemplateColumn x:Name =rolesColumnHeader =RolesWidth =auto>

< DataGridTemplateColumn.CellTemplate>

< DataTemplate>

< TextBlock Text ={Binding Path = Roles}/>

< / DataTemplate>

< /DataGridTemplateColumn.CellTemplate>

< / DataGridTemplateColumn>



IValueConverter不转换!!



公共对象转换(对象值,类型targetType,对象参数,System.Globalization.CultureInfo文化)

{

string a =(string)value;

int b;

int.TryParse(a,out b);

返回b;

}

公共对象ConvertBack(对象值,类型targetTypes ,对象参数,System.Globalization.CultureInfo文化)

{

抛出新的NotImplementedException();

}



有人可以帮助
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(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 displayed as ID in a DataGrid.

//To cut the story short 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();
}

can someone please help


这篇关于使用IValueConverter t0将字符串值转换为int WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆