Skip to main content

Microsoft Silverlight

Answered Question Databinding with ListBox show nothingRSS Feed

(0)

xsdf
xsdf

Member

Member

135 points

218 Posts

Databinding with ListBox show nothing

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

************************************
To be or not to be, it's not a question ,it's life.

bartczernicki
bartczer...

Contributor

Contributor

4036 points

717 Posts

Answered Question

Re: Databinding with ListBox show nothing

Very simple solution.  SImply DO NOT inherit your binding object from UserControl....

public class MyBaseUserControl

{

public string F_HolderUsername { get; set; }

public string F_HolderKey { get; set; }

public string F_DisplayName { get; set; }

}

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!

xsdf
xsdf

Member

Member

135 points

218 Posts

Answered Question

Re: Databinding with ListBox show nothing

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

************************************
To be or not to be, it's not a question ,it's life.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities