Have a look in your output window for binding exceptions. You can also turn
on Common Language Runtime Exceptions (check Thrown to stop on all
exceptions) Debug -> Exceptions (or Ctrl-D,E). That sometimes helps you with
incorrect XAML.

One comment, I can't see how your object you are binding to is getting into
your DataContext from your code example. Set a breakpoint somewhere and
check your UserControl's datacontext has something in it to bind to.

cheers,
Stephen

On Fri, Mar 4, 2011 at 1:57 PM, Peter Maddin <[email protected]>wrote:

> Ok I am trying to use bindings for my listbox using an
> ObservableCollection.
>
> However my listbox remains empty.
>
>
>
> My ObservableCollection is a very simple
>
>
>
> public class KeyDetailList : ObservableCollection<KeyDetail>
>
>     {
>
>         // Initial test data
>
>         public KeyDetailList()
>
>         {
>
>             this.Add(new KeyDetail("XXXXXX", "YYYYYYYYYYYYYYYYYY",
> "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"));
>
>             this.Add(new KeyDetail("AAAAAA", "BBBBBBBBBBBBBBBBBB",
> "CCCCCCCCCCCCCCCCCCCCCCCCCCC"));
>
>         }
>
>     }
>
>     public class KeyDetail
>
>     {
>
>         public string ClientCode { get; set; }
>
>         public string Generated { get; set; }
>
>         public string ClientDescription { get; set; }
>
>         public KeyDetail(string code, string generated, string
>  description)
>
>         {
>
>             this.ClientCode = code;
>
>             this.Generated = generated;
>
>             this.ClientDescription = description;
>
>         }
>
>     }
>
>
>
> In My XAML for my UserControl I give it a name of KeyFileControl
>
>
>
> . . . Detail omitted . . .
>
>              mc:Ignorable="d"
>
>              d:DesignHeight="617" d:DesignWidth="548"
>
>              Loaded="UserControl_Loaded"
>
>              *Name**="keyFileControl">*
>
> * *
>
> I have these resources
>
>
>
>  <UserControl.Resources>
>
>         <Style x:Key="KeyTextBlockStyle" TargetType="TextBlock">
>
>             <Setter Property="VerticalAlignment" Value="Center"/>
>
>             <Setter Property="MinHeight" Value="25"/>
>
>             <Setter Property="FontSize" Value="12.0"/>
>
>             <Setter Property="FontWeight" Value="Bold"/>
>
>             <Setter Property="Foreground" Value="WhiteSmoke" />
>
>             <Setter Property="TextAlignment" Value="Left"/>
>
>             <Setter Property="Margin" Value="5,10,5,0" />
>
>         </Style>
>
>
>
>         <DataTemplate x:Key="KeyListItemDetail">
>
>             <Grid>
>
>                 <Grid.RowDefinitions>
>
>                     <RowDefinition MinHeight="28" />
>
>                 </Grid.RowDefinitions>
>
>                 <Grid.ColumnDefinitions>
>
>                     <ColumnDefinition Width="*" MinWidth="75" />
>
>                     <ColumnDefinition Width="2*" MinWidth="120"/>
>
>                     <ColumnDefinition Width="3*" />
>
>                 </Grid.ColumnDefinitions>
>
>                 <TextBlock Grid.Column="0" Style="{StaticResource 
> KeyTextBlockStyle}" Text="{Binding Path=ClientCode}" />
>
>                 <TextBlock Grid.Column="1" Style="{StaticResource 
> KeyTextBlockStyle}" Text="{Binding Path=Generated}" />
>
>                 <TextBlock Grid.Column="2" Style="{StaticResource 
> KeyTextBlockStyle}" Text="{Binding Path=ClientDescription}" />
>
>             </Grid>
>
>         </DataTemplate>
>
>     </UserControl.Resources>
>
>
>
> My ListBox is defined as
>
>
>
>             <ListBox  Margin="6" Name="listBoxKeys" Background="Transparent" 
> BorderThickness="0"
>
>                       Padding="10" ItemTemplate="{StaticResource 
> KeyListItemDetail}" ItemsSource="{Binding ElementName=KeyFileControl, 
> Path=KeyList}">
>
>             </ListBox>
>
>
>
>
>
> In my code behind I have this to generate some dummy test data.
>
> In debug the ObservableCollection is being instantiated and populated with 
> data.
>
>
>
> *My ListBox remains blank*.
>
>
>
> One can debug code. I am not sure if one can debug XAML as it runs.
>
> I must be doing something really dumb but am not sure what.
>
>
>
> I am using these UserControls from another window as such
>
>
>
>         <StackPanel Grid.Row="1"  Grid.Column="2" VerticalAlignment="Stretch">
>
>             <local:DownLoadUserControl x:Name="downLoadControl" 
> Visibility="Visible" Height="588" Width="446" />
>
>             <local:KeyFileUserControl x:Name="keyFileControl" 
> Visibility="Collapsed" Height="588" Width="446" />
>
>         </StackPanel>
>
>
>
> Would the use of x:Name conflict with the UserControl having a Name attribute.
>
>
>
> Can someone please put me out of my misery.
>
>
>
> Regards Peter.
>
>
>
>
>
> *From:* [email protected] [mailto:[email protected]]
> *On Behalf Of *Peter Maddin
> *Sent:* Thursday, 3 March 2011 4:28 PM
> *To:* 'ozWPF'
> *Subject:* RE: Adding ListBoxItems to a listbox programmatically
>
>
>
> Thanks again
>
>
>
> After doing it programmatically I might use your solution with a collection
> that is bindable and put my template back in.
>
> i.e. http://msdn.microsoft.com/en-us/library/ms748365.aspx.
>
> Its a lot easier to work with a collection than work with UI elements.
>
>
>
> Suppose I should  have done it that way in the first place but I like to
> know how things work under the hood, so to speak.
>
>
>
> WPF is much better than WinForms but the learning curve is much steeper.
>
> I have been able to accomplish things with WPF that I could dream about
> with WinForms.
>
> Still have  a lot to learn.
>
>
>
> Regards Peter.
>
>
>
>
>
> _______________________________________________
> ozwpf mailing list
> [email protected]
> http://prdlxvm0001.codify.net/mailman/listinfo/ozwpf
>
>
_______________________________________________
ozwpf mailing list
[email protected]
http://prdlxvm0001.codify.net/mailman/listinfo/ozwpf

Reply via email to