从 LINQ 查询(Web 服务)填充文本块

Populating Textblock From LINQ Query (Web Service)

我编写了一个 Web 服务,允许我从我的 SQL 数据库中提取信息并在我的通用 Windows 应用程序中显示该信息。目前我在列表框中显示此信息。我想在 3 个单独的文本块中显示此信息,但我不确定如何实现...这是我目前拥有的,工作正常,但将其放在列表框中:

网络服务

 [OperationContract]
 List<TBL_My_Info> FindInfo(string uid);

 public List<TBL_My_Info> FindInfo(string uid)
 {
    DataClasses1DataContext context = new DataClasses1DataContext();
    var res = from r in context.TBL_My_Info where r.User_Name == uid select r;
    return res.ToList();
 }

XAML

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListBox Height="500" HorizontalAlignment="Left" 
     Margin="8,47,0,0" 
     Name="listBoxInfo" VerticalAlignment="Top" Width="440">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{Binding Title}" FontSize="14" TextWrapping="Wrap"/>
                    <TextBlock Text="{Binding Description}" FontSize="14" TextWrapping="Wrap"/>
                    <TextBlock Text="{Binding Name}" FontSize="14" TextWrapping="Wrap"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

通用 Web 应用程序

private void btnView_Click(object sender, RoutedEventArgs e)
{
    string s = txtNameFind.Text;
    this.Content = new Page1(s);
}       

 public Page1(string s)
{
    this.InitializeComponent();
    LoadData(s);          
}

private async void LoadData(string s)
{
    var client = new ServiceReference1.Service1Client();
    var res = await client.FindMyInfoAsync(s);
    listBoxInfo.ItemsSource = res;
}

基本上我要问的是,我怎样才能将 3 条信息显示在 3 个单独的文本块中,而不是在列表框中...

谢谢

绑定示例:

//this is the backing store property
public static readonly DependencyProperty ListBoxInfoProperty =
       DependencyProperty.Register("ListBoxInfo", typeOf(ObservableCollection<Tbl_my_Info>), typeof(thisControlType));

//this is the CLR Wrapper
public ObservableCollection<Tbl_my_Info> ListBoxInfo {

    get{return (ObservableCollection<Tbl_my_Info>)GetValue(ListBoxInfoProperty);}
    set{SetValue(ListBoxInfoProperty,value);}

在 InitializeComponent() 调用后的 Window 或 UserControl 中,输入此...

DataContext = this;

您刚刚使 XAML 可以绑定到此代码。

现在 XAML...

<ListBox Height="500" HorizontalAlignment="Left" 
 Margin="8,47,0,0" 
 ItemsSource = "{Binding ListBoxInfo}"
 Name="listBoxInfo" VerticalAlignment="Top" Width="440">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <TextBlock Text="{Binding Title}" FontSize="14" TextWrapping="Wrap"/>
                <TextBlock Text="{Binding Description}" FontSize="14" TextWrapping="Wrap"/>
                <TextBlock Text="{Binding Name}" FontSize="14" TextWrapping="Wrap"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

试一试....