Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Databinding with ListBox show nothing
2 replies. Latest Post by xsdf on October 22, 2008.
(0)
xsdf
Member
135 points
218 Posts
10-21-2008 11:26 PM |
I have a problem when data binding with listbox .
The xaml is :
<Grid x:Name="LayoutRoot" Background="White"> <StackPanel> <StackPanel Margin="0,10,0,0"> <TextBlock Text="MyComponent" FontSize="12" x:Name="txtYourComp" HorizontalAlignment="Center"></TextBlock> </StackPanel> <ListBox MaxWidth="150" MaxHeight="250" ItemsSource="{Binding ''}" x:Name="MyDynaComp"> <ListBox.ItemTemplate> <DataTemplate> <TextBox Text="{Binding Path=F_DisplayName}" ></TextBox> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </StackPanel> </Grid>
The code file is :
public partial class Page : UserControl { public class MyBaseUserControl : UserControl { public string F_HolderUsername { get; set; } public string F_HolderKey { get; set; } public string F_DisplayName { get; set; } } Dictionary<int, MyBaseUserControl> myAllDynaCompCollection = new Dictionary<int, MyBaseUserControl>(); public Page() { InitializeComponent(); myAllDynaCompCollection.Add(0, new MyBaseUserControl() { F_DisplayName="MMMMM", F_HolderKey="MyKey", F_HolderUsername = "MyGod", } );
MyDynaComp.DataContext = myAllDynaCompCollection.Values; } }
These are the whole codes .
Why the listbox will show nothing even the cool object really has values ??????????
bartczer...
Contributor
4036 points
717 Posts
10-22-2008 1:38 AM |
Very simple solution. SImply DO NOT inherit your binding object from UserControl....
{
}
Objects that inherit from DependencyObject apparently break binding. You can check it out here...
http://silverlight.net/forums/t/13593.aspx
The UserControl inherits from Control which inherits from FrameworkElement -> UIElement -> DependencyObject
This worked in Beta 1 (not sure about Beta 2). Silverlight 2 RTW does not allow this, so if you are looking at an example from a beta this is why you might now be working.
Please mark answer if this helped!
10-22-2008 3:05 AM |
Thanks bartczernicki .
Because I have to use MyBaseUserControl , so I change the code like below.:
It will works fine with data binding .
public class MyDynaObject { public MyBaseUserControl F_HostedComp { get; set; } public string F_HolderUsername { get { return F_HostedComp.F_HolderUsername; } } public string F_HolderKey { get { return F_HostedComp.F_HolderKey; } } public string F_DisplayName { get { return F_HostedComp.F_DisplayName; } } }