wpf 使用 IValueConverter 将字符串转换为 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23565926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:41:48  来源:igfitidea点击:

Convert string to int using IValueConverter

c#wpfivalueconverter

提问by AndyRoxxx

How do I convert string values to integers and back using IValueConverter?

如何使用 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.
  • 我有一个由两个表组成的数据库;表 CompanyX 和表 DeptY。
  • 表 CompanyX 具有字段 ID(int)、firstName、lastName、Email、Phone。
  • 表 DeptY 有字段 pID(int), Roles。
  • DeptY pID 是 CompanyX ID 的外键。每次我在组合框中选择某人时,我都希望它在 DataGrid 中显示为他们的 ID。

This is my ItemTemplate below:

这是我的 ItemTemplate 下面:

<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:

这是我绑定到 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:

还有一个 DataGrid 显示:

<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!!

不转换的 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();
}

回答by GazTheDestroyer

You are converting the wrong way around. Convert()takes the binding source as input (intin your case), and outputs what the xaml is expecting (string).

您正在以错误的方式转换。Convert()将绑定源作为输入(int在您的情况下),并输出 xaml 期望的内容 ( string)。

But you don't even need a converter. You can bind straight to an intand WPF will automatically call ToString()on it to display it as text.

但您甚至不需要转换器。您可以直接绑定到一个int,WPF 将自动调用ToString()它以将其显示为文本。

回答by Wobbles

Like was said in the other answer, you are doing it backwards. The Convert side of an IValueConverter is meant for converting from the source property to what is in the bound element, in your case a textbox. You need to do your parsing on the ConvertBack side because that is what takes what is in the textbox (a string) and converts it back to a number.

就像在另一个答案中所说的那样,您是在倒退。IValueConverter 的 Convert 端用于从源属性转换为绑定元素中的内容,在您的情况下为文本框。您需要在 ConvertBack 端进行解析,因为这是将文本框(字符串)中的内容转换回数字的原因。

public class IntToString : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int ret = 0;
        return int.TryParse((string)value, out ret) ? ret : 0;
    }
}