如何从List绑定文本框?

时间:2016-07-16 22:43:32

标签: c# entity-framework

我只需要绑定文本框。我创建了一个下一个和上一个按钮,但是文本框上没有任何内容。我错了什么?如何从列表中获取项目?

从数据库中读取:

   void read()
    {
        using (var nwe = new NORTHWNDEntities())
        {

            var objctx = (nwe as IObjectContextAdapter).ObjectContext;
            ObjectQuery<Employee> query = new ObjectQuery<Employee>(
                "SELECT VALUE n from NORTHWNDEntities.Employees as n", objctx);
            List<Employee> results = query.ToList();


            cursor = (CollectionView)CollectionViewSource.GetDefaultView(results);

        }
    }

    private void countposition()
    {
        labelRead.Content = "Read " + (cursor.CurrentPosition + 1) + " to " + cursor.Count; 
    }
private void buttonNext_Click(object sender, RoutedEventArgs e)
    {
        if(cursor.CurrentPosition < cursor.Count -1)
        {
            cursor.MoveCurrentToNext();
            countposition();
        }
        else
        {
            MessageBox.Show("No more data");
        }
    }

    private void buttonPrevious_Click(object sender, RoutedEventArgs e)
    {

         if (cursor.CurrentPosition > 0)
         {
            cursor.MoveCurrentToPrevious();
            countposition();

         }
         else
         {
             MessageBox.Show("No more data");
         }

    }

用于绑定数据的UI代码:

<TextBox x:Name="textBoxID" HorizontalAlignment="Left" Height="23" Margin="163,79,0,0" TextWrapping="Wrap" Text="{Binding Path=EmployeeID}" IsReadOnly="True" VerticalAlignment="Top" Width="87"/>
    <Label x:Name="labelFirstNAme" Content="First Name" HorizontalAlignment="Left" Margin="52,157,0,0" VerticalAlignment="Top"/>
    <TextBox x:Name="textBoxFirstName" HorizontalAlignment="Left" Height="23" Margin="163,157,0,0" TextWrapping="Wrap" Text="{Binding Path=FirstName}" VerticalAlignment="Top" Width="145"/>
    <Label x:Name="labelLastName" Content="Last Name" HorizontalAlignment="Left" Margin="52,224,0,0" VerticalAlignment="Top" Width="67"/>
    <TextBox x:Name="textBoxLastName" HorizontalAlignment="Left" Height="23" Margin="163,224,0,0" TextWrapping="Wrap" Text="{Binding Path=LastName}" VerticalAlignment="Top" Width="145"/>
    <Label x:Name="labelBirthDate" Content="Birth Date" HorizontalAlignment="Left" Margin="52,307,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.289,0.462" Width="67"/>
    <TextBox x:Name="textBoxBirthDate" HorizontalAlignment="Left" Height="23" Margin="163,310,0,0" TextWrapping="Wrap" Text="{Binding Path=BirthDate}" VerticalAlignment="Top" Width="145"/>

1 个答案:

答案 0 :(得分:0)

抱歉,我喝了几杯啤酒,但TextBox只接受一个值,通常是id。文件=&#34; oneStringInstanceOnly&#34;。 你想绑定List&lt;&gt; (具有多个值/对象/字符串/类)到TextBox,它通常接受一个(通常是字符串)值,这就是为什么不能将包含多个值的List绑定到接受一个值的TexBox的原因。 TextBox不是接受多个值/数据表的DataGridView或ListBox。你甚至可以给TexBox分配列表中的第一个值myTextBox =(myList!= null&amp;&amp; myList.Count!= 0)?myList [0] .ToString():&#34; NoMoreValues&#34 ;;