Search code examples
c#wpflinqxamltextblock

Populating Textblock From LINQ Query (Web Service)


I have written a Web Service that allows me to pull information from my SQL DB and display that information in my Universal Windows App. Currently I am displaying this information in a listbox. I would like to display this information in 3 separate textblocks and I am unsure of how to achieve that... This is currently what I have that works fine, but is placing it in a listbox:

Web Service

 [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>

Universal Web Application

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;
}

Basically what I am asking, is how can I get it to the 3 pieces of information to display in 3 separate textblocks, rather than in a listbox...

Thanks


Solution

  • Example of using Binding:

    //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);}
    

    In the Window or UserControl after InitializeComponent() call, enter this...

    DataContext = this;
    

    You've just made it possible for XAML to bind to this code.

    Now in 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>
    

    Give that a shot....