簡體   English   中英

XAML C#數據表綁定文本框

[英]Xaml c# datatable binding textbox

我正在嘗試將數據表的數據綁定到文本框。 它工作正常,但僅顯示數據表的最后一行。 是否可以顯示數據表的所有行?

XAML:

<Window x:Class="messenger.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
    <TextBox Height="203" HorizontalAlignment="Left" Name="conversationInput" VerticalAlignment="Top" Width="491" Margin="12,3,0,0" IsReadOnly="True">
        <TextBox.Text>
            <MultiBinding StringFormat="{0} : {1}">
                <Binding Path="date" />
                <Binding Path="message" />
            </MultiBinding>
        </TextBox.Text>
    </TextBox>
</Grid>

后面的代碼:

    public DataTable loadConversation()
    {
        DataTable conversation = new DataTable();
        string loadMessages = "select message, date from Messages where userID=1";
        ad = new MySqlDataAdapter(loadMessages, connection);
        ad.Fill(conversation);
        return conversation;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        conversationInput.DataContext = loadConversation().DefaultView;
    }

預先感謝,Fluxxi

TextBox的設計方式。 您應該改用ListBox (或類似的)控件。

是的,但是您需要使用某種中繼器控件,例如DataGridListView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM