tags:

views:

598

answers:

3
<StackPanel x:Name="stkWaitingPatients" Width="300" Margin="0,0,0,-3"
   DataContext="{Binding Mode=OneWay, Source={StaticResource local:oPatients}}">

I'm getting StaticResource reference 'local:oPatients' was not found.

Here is the codebehind:

    public partial class MainWindow : Window
{
    ListBox _activeListBox;
    clsPatients oPatients;

 public MainWindow()
 {
        oPatients = new clsPatients(true);

...
+2  A: 

To be able to address the object as a StaticResource, it needs to be in a resource dictionary. However, since you're creating the object in MainWindow's constructor, you can set the DataContext in the code-behind like so.

oPatients = new clsPatients(true);
stkWaitingPatients.DataContext = oPatients;

And then change the Binding to this:

{Binding Mode=OneWay}

This is an ok practice if you're not going to be changing the DataContext again, otherwise you'd want a more flexible solution.

Edit: You mentioned ObjectDataProvider in your comment. Here's how you'd do that. First, add an xmlns:sys to the Window for the System namespace (I'm assuming you already have one for xmlns:local):

xmlns:sys="clr-namespace:System;assembly=mscorlib"

Then you can add an ObjectDataProvider to your resource dictionary like this:

<Window.Resources>
    <ObjectDataProvider
        x:Key="bindingPatients"
        ObjectType="{x:Type local:clsPatients}">
        <ObjectDataProvider.ConstructorParameters>
            <sys:Boolean>True</sys:Boolean>
        </ObjectDataProvider.ConstructorParameters>
    </ObjectDataProvider>
</Window.Resources>

And refer to it in a Binding with the StaticResource markup like this, using the same string we specified in the x:Key attached property we gave it in the dictionary:

{Binding Source={StaticResouce bindingPatients}, Mode=OneWay}

Edit 2: Ok, you posted more code in your answer, and now I know why it's throwing an exception during the constructor. You're attempting to do this...

lstWaitingPatients.DataContext = oPatients;

... but lstWaitingPatients doesn't actually exist until after this.InitializeComponent() finishes. InitializeComponent() loads the XAML and does a bunch of other things. Unless you really need to do something before all of that, put custom startup code after the call to InitalizeComponent() or in an event handler for Window's Loaded event.

Joel B Fant
I'm simply trying to move the datacontext to the codebehind so that I can trap for errors (like a bad connection string) and then move to a user friendly error page...couldn't figure out how to do that using and ObjectDataSource in XAML...Trying this now...
LSTayon
Nope, I'm getting a generic "Cannot create instance of Window"...can I send you more code? This is driving me nuts!
LSTayon
That means you have an exception occuring during `Window`'s constructor, probably due to a bug in the XAML. Sending you more code won't help, seeing your code will. Did you put `xmlns:sys` inside the `Window` tag? Was there already a `<Window.Resources>` and now there are 2? There's a lot of things it could be.
Joel B Fant
And which one did you try? Assigning `DataContext` in code-behind?
Joel B Fant
Putting more code in the question section
LSTayon
Awww, I got it! Duh, how about I put the DataContext assignment AFTER the InitializeComponent?! Thanks!!!
LSTayon
A: 

Now, I can't get this to work...I get a general Windows startup error.

Here is the codebehind with the Initializer and the class being instantiated:

    public partial class MainWindow : Window
{
    ListBox _activeListBox;

 public MainWindow()
 {
        clsPatients oPatients = new clsPatients(true);
        lstWaitingPatients.DataContext = oPatients;

  this.InitializeComponent();

Here's the top of my XAML:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Orista_Charting"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" 
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
x:Class="Orista_Charting.MainWindow"
x:Name="windowMain"
Title="Orista Chart"
Width="1024" Height="768" Topmost="True" WindowStartupLocation="CenterScreen" Activated="MainWindow_Activated" >


<Window.Resources>        

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/ButtonStyles.xaml"/>
            <ResourceDictionary Source="Resources/OtherResources.xaml"/>
            <ResourceDictionary Source="Resources/TextBlockStyles.xaml"/>
            <ResourceDictionary Source="Resources/Converters.xaml"/>
        </ResourceDictionary.MergedDictionaries>

Here's the pertinent XAML, as you see, I went ahead and moved the DataContext down to the ListBox from the StackPanel. This doesn't run, but it does render in Design View (however, with no data present in the ListBox):

<!-- Waiting Patients List -->
   <Border BorderThickness="1,1,1,1" BorderBrush="#FF000000" Padding="10,10,10,10" 
            CornerRadius="10,10,10,10" Background="#FFFFFFFF" Margin="15.245,187.043,0,41.957" HorizontalAlignment="Left" >
        <StackPanel x:Name="stkWaitingPatients" Width="300" Margin="0,0,0,-3"> 

            <StackPanel Orientation="Horizontal">    
          <TextBlock Text="Waiting Patients:" VerticalAlignment="Center" FontSize="21.333" Margin="0,0,0,20"/>
          <TextBlock HorizontalAlignment="Right" Margin="0,0,38.245,0" Width="139" Height="16" 
    Text="Minutes Waiting" TextWrapping="Wrap" Foreground="#FF9C2525" FontWeight="Bold" VerticalAlignment="Bottom"
    TextAlignment="Right"/> 
                <!-- Too be implemented, this is the wait animation -->
                <!--<Image x:Name="PollGif" Visibility="{Binding Loading}"
                       HorizontalAlignment="Left" Margin="100,0,0,0" Width="42.5" Height="42.5" 
                       Source="Images/loading-gif-animation.gif" Stretch="Fill"/>-->       
         </StackPanel>                  

            <ListBox x:Name="lstWaitingPatients"       
                     DataContext="{Binding Mode=OneWay}" ItemsSource="{Binding Mode=OneWay}"
                     IsSynchronizedWithCurrentItem="true"
                     ItemTemplate="{StaticResource WaitingPatientsItemTemplate}"
                     FontSize="21.333" Height="423.291" ScrollViewer.VerticalScrollBarVisibility="Visible"
                     GotFocus="lstWaitingPatients_GotFocus"
                     />

        </StackPanel>
    </Border>

Ok, but if I just take comment out the assigment line in the codebehind, it does run (albeit with no data in the listbox):

    public partial class MainWindow : Window
{
    ListBox _activeListBox;

 public MainWindow()
 {
        clsPatients oPatients = new clsPatients(true);
        //lstWaitingPatients.DataContext = oPatients;

THANKS!

LSTayon
As sixlettervariables notes in his answer, you don't need some of the bindings you're doing on the `ListBox`.
Joel B Fant
+1  A: 

The following sets the ItemsSource in Code Behind and correctly handles the DataBinding:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        clsPatients oPatients = new clsPatients(true);

        //assuming oPatients implements IEnumerable
        this.lstWaitingPatients.ItemsSource = oPatients;

And the XAML:

<ListBox x:Name="lstWaitingPatients"       
         IsSynchronizedWithCurrentItem="true"
         ItemTemplate="{StaticResource WaitingPatientsItemTemplate}"
         FontSize="21.333" Height="423.291"
         ScrollViewer.VerticalScrollBarVisibility="Visible"
         GotFocus="lstWaitingPatients_GotFocus"
         />
sixlettervariables
But, I'd go with the ObjectDataProvider from @Joel's answer.
sixlettervariables