×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    11

    bind linq to textblock without listBox.ItemsSource

    i have a linq that i bind the results to my listbox. what i want to do is in my page i want a value of an element from that linq result to be bound on a control (textblock) that is outside my <ListBox.ItemTemplate><DataTemplate>

    here is my linq:
    Code:
    var data = from query in loadedData.Descendants("article")
                           where query.Attribute("Title").Value == articleNo
                           select new article()
                           {
                               Title = query.Attribute("Title").Value,
                               SubTitle = query.Attribute("SubTitle").Value,
                               Content = query.Attribute("Content").Value
                           };
    
                listBox.ItemsSource = data;
    and here is my xaml:
    Code:
    <ListBox x:Name="listBox">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                        <StackPanel Height="Auto" HorizontalAlignment="Left" Name="stackPanel1" VerticalAlignment="Top" Width="auto">
                                <TextBlock Height="Auto" Name="txSubTitle" Text="{Binding SubTitle}" FontSize="32" Foreground="#FFFFFF" Margin="12,0,0,0" TextWrapping="Wrap" />
                                <TextBlock FontSize="26" Height="Auto" Name="txContent" Text="{Binding Content}" Margin="12,0,0,0" Foreground="#FF7D97AE" TextWrapping="Wrap" />
                        </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
    i need to bind the SubTitle to a textblock outside of my listbox. is there anyway for me to bind a linq result to a control even if i have already put the linq's result to a listbox's itemsource? like <TextBlock Text="<Binding SubTitle"> only?

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2012
    Location
    Bilbao
    Posts
    53

    Re: bind linq to textblock without listBox.ItemsSource

    Yes, you can bind your Textblock Text property to the selectedItem of the listbox:

    Code:
    <TextBlock Text="{Binding ElementName=listboxName, Path=SelectedItem.Subtitle}"
                VerticalAlignment="Bottom"></TextBlock>
    This way, you use the element called ListboxName as your binding source and use the property SelectedItem to get to the listbox current selected item, and you can access to the properties of the object contained in the selecteditem, in this case an instance of the article class.

    Hope this help

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    19

    Re: bind linq to textblock without listBox.ItemsSource

    please see the code
    --------------------------------------add xml file name "phconstitution.xml"-------xml formet----------------
    <?xml version="1.0" encoding="utf-8" ?>
    <Accounts>
    <User>
    <Title>your name title</Title>
    <SubTitle>your name subtitle</SubTitle>
    <Content>details</Content>
    </User>
    </Accounts>

    -----------------------------------------------------------------------------------design page---------
    <Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
    <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,98">
    <ListBox x:Name="listBox" Margin="6,170,-6,178">
    <ListBox.ItemTemplate>
    <DataTemplate>
    <StackPanel Height="Auto" HorizontalAlignment="Left" Name="stackPanel1" VerticalAlignment="Top" Width="auto">
    <TextBlock Height="Auto" Name="txtTitle" Text="{Binding Title}" FontSize="32" Foreground="Red" Margin="12,0,0,0" TextWrapping="Wrap" />
    <TextBlock Height="Auto" Name="txSubTitle" Text="{Binding SubTitle}" FontSize="32" Foreground="Red" Margin="12,0,0,0" TextWrapping="Wrap" />
    <TextBlock FontSize="26" Height="Auto" Name="txContent" Text="{Binding Content}" Margin="12,0,0,0" Foreground="#FF7D97AE" TextWrapping="Wrap" />
    </StackPanel>
    </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox>
    <ListBox x:Name="listBox1" Margin="6,337,-6,6">
    <ListBox.ItemTemplate>
    <DataTemplate>
    <StackPanel Height="Auto" HorizontalAlignment="Left" Name="stackPanel1" VerticalAlignment="Top" Width="auto">
    <TextBlock Height="Auto" Name="txtTitle" Text="{Binding Title}" FontSize="32" Foreground="Red" Margin="12,0,0,0" TextWrapping="Wrap" />
    <TextBlock Height="Auto" Name="txSubTitle" Text="{Binding SubTitle}" FontSize="32" Foreground="Red" Margin="12,0,0,0" TextWrapping="Wrap" />
    <TextBlock FontSize="26" Height="Auto" Name="txContent" Text="{Binding Content}" Margin="12,0,0,0" Foreground="#FF7D97AE" TextWrapping="Wrap" />
    </StackPanel>
    </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox>
    </Grid>
    <Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="24,523,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" Grid.Row="1" />
    </Grid>
    -----------------------------------------------------------------------------------code page---------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;
    using System.IO;
    using System.Xml;
    using System.Text.RegularExpressions;
    using System.Text;
    using System.IO.IsolatedStorage;
    using System.Xml.Linq;

    namespace PhoneAppsplash
    {
    public partial class MainPage : PhoneApplicationPage
    {
    // Constructor
    public MainPage()
    {
    InitializeComponent();
    }
    public static IsolatedStorageSettings AppSetting = IsolatedStorageSettings.ApplicationSettings;
    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
    List<dataList> dataSource = null;
    dataSource = new List<dataList>();
    XDocument xmlDoc = null;
    try
    {
    xmlDoc = XDocument.Load("phconstitution.xml");
    string Title = xmlDoc.Element("Accounts").Element("User").Element("Title").Value.ToString();
    string SubTitle = xmlDoc.Element("Accounts").Element("User").Element("SubTitle").Value.ToString();
    string Content = xmlDoc.Element("Accounts").Element("User").Element("Content").Value.ToString();
    dataSource.Add(new dataList()
    {
    Title = Title,
    SubTitle = SubTitle,
    Content = Content,
    });
    listBox.ItemsSource = dataSource;
    }
    catch { }

    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
    XDocument xmlDoc = null;
    try
    {
    xmlDoc = XDocument.Load("phconstitution.xml");
    var data = from query in xmlDoc.Descendants("User")
    select new dataList
    {
    Title = (string)query.Element("Title"),
    SubTitle = (string)query.Element("SubTitle"),
    Content = (string)query.Element("Content")
    };
    listBox1.ItemsSource = data;

    AppSetting["Title"] = Title;
    AppSetting["SubTitle"] = SubTitle;
    AppSetting["Content"] = Content;
    AppSetting.Save();
    }
    catch { }
    }
    }

    public class dataList
    {
    public string Title { get; set; }
    public string SubTitle { get; set; }
    public string Content { get; set; }
    }
    }

    ----------------------------------------------------------------------------------------------

    please see this link
    http://windowsphonegeek.com/articles...sitory-Pattern
    hope resolve your problem.

    other wise "var data" find a list and create class,
    public class dataList
    {
    public string Title { get; set; }
    public string SubTitle { get; set; }
    public string Content { get; set; }
    }

    List<dataList> dataSource = null;
    dataSource = new List<dataList>();
    dataSource.Add(new dataList()
    {
    Title = Title,
    SubTitle = SubTitle,
    Content = Content,
    });

    listBox.ItemsSource = dataSource;

    than bind data in the TextBlock.
    Last edited by pravejnini; 2012-02-28 at 10:12.

Similar Threads

  1. pass the URI parameter/value to linq
    By thejeraldo in forum Windows Phone General
    Replies: 4
    Last Post: 2012-02-27, 12:13
  2. html formatting in xml retrieve to textblock
    By thejeraldo in forum Windows Phone General
    Replies: 1
    Last Post: 2012-02-21, 07:33
  3. Replies: 4
    Last Post: 2010-07-21, 13:43
  4. Can't bind 127.0.0.1 on N93?
    By maoxizhu in forum Symbian
    Replies: 3
    Last Post: 2009-05-06, 11:51
  5. Listbox.bind and EKeySelect problem
    By madsbjoern in forum Symbian
    Replies: 2
    Last Post: 2007-03-29, 20:30

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×