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

时间:2015-11-19 01:39:41

标签: c# wpf linq xaml textblock

我编写了一个Web服务,允许我从SQL DB中提取信息并在我的通用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>

通用网络应用

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个单独的文本块中,而不是放在列表框中...

由于

1 个答案:

答案 0 :(得分:0)

使用绑定的示例:

//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中,输入this ...

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>

给那个镜头......