如何将ObjectID转换为String以进行文本框绑定?

时间:2015-11-22 14:25:36

标签: c# wpf mvvm data-binding objectid

在将ObjectID从我的模型绑定到文本框文本属性时,我遇到了绑定错误。

我从谷歌搜索错误细节知道,错误是由于文本框期望字符串值,而ObjectID是十六进制值。

因此,在ObjectID属性中,我编辑了setter以将ID转换为字符串类型。 (但是我收到转换错误,说明我无法将ObjectID隐式转换为String):

        set
        {
            id = Convert.ToString(value);
        }

有没有人知道如何将ID转换为字符串类型,以满足文本框中的文本属性绑定?

我得到的窗口数据错误如下:

System.Windows.Data Error: 1 : Cannot create default converter to perform 'two-way' conversions between types 'MongoDB.Bson.ObjectId' and 'System.String'. Consider using Converter property of Binding. BindingExpression:Path=SelectedItem.Id; DataItem='DataGrid' (Name='customersgrid'); target element is 'TextBox' (Name='iDTbx'); target property is 'Text' (type 'String')

System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='5565d8adba02d54a4a78be96' BindingExpression:Path=SelectedItem.Id; DataItem='DataGrid' (Name='customersgrid'); target element is 'TextBox' (Name='iDTbx'); target property is 'Text' (type 'String')

xaml绑定的示例如下:

<Window x:Class="MongoDBApp.Views.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:email_validator="clr-namespace:MongoDBApp.Validator"
        Title="Orders Dashbord"
        Width="800"
        Height="500">



    <Grid>
        <TabControl>
            <TabItem Header="Customer">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="70" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height=".50*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width=".1*" />
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="2*" />
                        <ColumnDefinition Width=".5*" />
                    </Grid.ColumnDefinitions>

                    <DataGrid Name="customersgrid"
                              Grid.Row="0"
                              Grid.RowSpan="3"
                              Grid.Column="1"
                              Grid.ColumnSpan="3"
                              AutoGenerateColumns="False"
                              ItemsSource="{Binding Customers}"
                              SelectedItem="{Binding SelectedCustomer}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding Id}" Header="ID" />

                        </DataGrid.Columns>
                    </DataGrid>


                    //ID textbox
                    <TextBox x:Name="iDTbx"
                             Grid.Row="4"
                             Grid.Column="2"
                             Grid.ColumnSpan="2"
                             Width="120"
                             Height="23"
                             HorizontalAlignment="Right"
                             VerticalAlignment="Top"
                             BorderBrush="AliceBlue"
                             IsReadOnly="True"
                             Text="{Binding SelectedItem.Id,
                                            ElementName=customersgrid}"
                             TextWrapping="Wrap" />

                </Grid>
            </TabItem>

            <TabItem Header="Order" />

            <TabItem Header="OrderStatus" />

        </TabControl>
    </Grid>

</Window>

这是模型中的ObjectID属性,减去了我的转换代码:

    [BsonId]
    public ObjectId Id 
    {
        get
        {
            return id;
        }
        set
        {

            id = value;
        }
    }

1 个答案:

答案 0 :(得分:1)

TextBox的Text属性默认绑定为双向,这意味着需要与string进行转换。

由于您在TextBox上设置IsReadOnly="True",您似乎只需要单向绑定,因此您可以编写

<TextBox Text="{Binding SelectedItem.Id, ElementName=customersgrid, Mode=OneWay}" .../>

更好的是,当您不想编辑该值时,根本不使用TextBox。请改为使用TextBlock,默认情况下Text属性单向绑定。您不需要任何转换器,因为Binding会通过调用string方法隐式地将值转换为ToString()

<TextBlock Text="{Binding SelectedItem.Id, ElementName=customersgrid}" .../>