views:

15

answers:

1

I have a listbox and as ItemsSource I give it an IList .

In this object exists another object with name {User} and I am trying to Bind the property {Username} onto a textBlock.

I tried something like this but with no luck

<TextBlock x:Name="usernamtTBL" Text="{Binding 'User.Username'}"/>

This is the full XAML code of listbox

<ListBox Height="275" x:Name="NewsFeedLB" Canvas.Left="8" Canvas.Top="8" Width="427" Background="White">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Canvas Height="57" Width="265" d:DesignWidth="265" d:DesignHeight="155">
                <Border BorderBrush="Black" BorderThickness="1" Height="35" Canvas.Left="8" Canvas.Top="8" Width="48">
                    <Image x:Name="thumbIMG" Margin="7"/>
                </Border>
                <TextBlock x:Name="usernamtTBL" Text="{Binding 'User.Username'}" Height="12" Canvas.Left="71" TextWrapping="Wrap" Canvas.Top="8" Width="180"/>
                <TextBlock x:Name="statusTBL" Text="{Binding 'Text'}" Height="12" Canvas.Left="71" TextWrapping="Wrap" Canvas.Top="24" Width="180"/>
            </Canvas>                       
        </DataTemplate>         
    </ListBox.ItemTemplate> 
</ListBox> 

and this is from codebehind

private void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e) {
    var newsFeedWcfClient = new NewsFeedWCFClient();
    newsFeedWcfClient.GetNewsFeedItemsCompleted += new EventHandler<GetNewsFeedItemsCompletedEventArgs>(newsFeedWcfClient_GetNewsFeedItemsCompleted);
    newsFeedWcfClient.GetNewsFeedItemsAsync();
}

void newsFeedWcfClient_GetNewsFeedItemsCompleted(object sender, GetNewsFeedItemsCompletedEventArgs e) {
    var source = (IList<NewsFeed>)e.Result;
    NewsFeedLB.ItemsSource = source;
}

Could someone help me solve this issue?

Thanks

A: 

Does your NewsFeed object have a public 'get' on it so that the value can be bound to? I reproduced your code as shown below (except I simply set the source to an ObservableCollection and didn't call a service) and it worked.

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();

        ObservableCollection<NewsFeed> source = new ObservableCollection<NewsFeed>() { new NewsFeed() { Text = "This is a test", User = new User(){Username="Test Name"} } };
        NewsFeedLB.ItemsSource = source;
    }
}

public class NewsFeed
{
    public string Text { get; set; }
    public User User { get; set; }
}

public class User
{
    public string Username { get; set; }
}
JSprang
You are setting only the {Text}, I don't have any problem with this property. The problem is with {User.Username}
StrouMfios
Updated to use the Username too, which also works. I did not change your XAML at all. Make sure that Username is a public property on your object.
JSprang
Ok I'll check it out. Tahnks
StrouMfios
Finally this was the error. Thanks again
StrouMfios