5 Replies - 1200 Views - Last Post: 02 October 2012 - 08:27 AM Rate Topic: -----

#1 Tim.Simon  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 26
  • Joined: 12-June 12

WPF Not Binding Properly When Called From Another Project

Posted 26 September 2012 - 10:23 AM

I have a WPF application that manages several Senders and Receivers. The list of Senders and Receivers are bound to a collapsible container, which gives additional information when expanded, allowing the user to configure the device.

When starting the WPF project, the saved devices load correctly, and display properly in the collapsible containers.

My task was to turn this into a service. To allow UI access, I created a tray app, which can call the WPF application. These are in two separate projects in the same solution (if that makes a difference). Currently, I have not changed any code in the WPF application, just the startup project. The issue that I have is that the Sender list and Receiver list don't seem to be bound properly to the collapsible containers when loading from the tray. I just get an empty container. I've included the image below to help describe what I mean.

Attached Image

I know the Senders and Receivers are being properly loaded from observing the Load method, and they show up in a different part of the application (see above). I have revved up my reporting, and notice that I get several Information messages saying that the WPF "Cannot retrieve value using the binding...", but research has shown that this is normal as Information, and should only cause concern if there is a Warming. I've included a sample of them below just in case I am mistaken, and they can be helpful.

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=ReceiversList; DataItem=null; target element is 'RepeatControl' (Name=''); target property is 'List' (type 'IList')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=DataContext; DataItem=null; target element is 'ContentPresenter' (Name=''); target property is 'DataContext' (type 'Object')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=CollapsedContent; DataItem=null; target element is 'ContentPresenter' (Name='PART_CollapsedContentPresenter'); target property is 'Content' (type 'Object')


I seem to get these when setting the WPF as startup as well, which confirms that they are probably not related.

I've also included a brief sampling of the xaml below. I've tried to keep it relative to the issue, and have cut out quite a bit to keep the listing a reasonable length.

      <UserControl.Resources>
        <DataTemplate x:Key="SenderSummary">
            <Border BorderThickness="1" BorderBrush="LightGray" CornerRadius="5" Padding="10" Width="720" Margin="10,1,3,0" >
            <WrapPanel Orientation="Horizontal" MaxWidth="750" Margin="10,0,0,0">
                <TextBlock Name="appinfo"> ...

            <DataTemplate x:Key="SenderTemplate" DataType="{x:Type m:SenderReceiverManager}" >
                <c:CollapsibleSection Header="{Binding  Path=ApplicationName}" Width="750">
                <c:CollapsibleSection.CollapsedContent>
                    <ContentPresenter  Content="{Binding}" ContentTemplate="{StaticResource SenderSummary}" Margin="0,1,0,0" Width="750"/>
                </c:CollapsibleSection.CollapsedContent>           
                <Grid  MinWidth="710" Margin="20,0,0,0 " > ...

        <c:CollapsibleSection Header="Senders">
            <c:CollapsibleSection.CollapsedContent>
                 <c:RepeatControl Margin="30,0,0,0" ItemsSource="{Binding SendersList}"  
                                  ItemType="{x:Type m:Sender}"  List="{Binding SendersList}"  
                                  ItemTemplate="{StaticResource SenderSummary}"/>
            </c:CollapsibleSection.CollapsedContent>
            <Border BorderThickness="1" BorderBrush="Chocolate" Margin="30,0,0,0">
                <c:RepeatControl  ItemsSource="{Binding SendersList}"  ItemType="{x:Type m:Sender}" List="{Binding SendersList}"  ItemTemplate="{StaticResource SenderTemplate}"/> 
            </Border>
        </c:CollapsibleSection>



Any insights on what I might be doing wrong would be extremely appreciated. I have not worked with WPF before, and am finding it difficult to debug the xaml. Thank you all in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: WPF Not Binding Properly When Called From Another Project

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3650
  • View blog
  • Posts: 11,419
  • Joined: 05-May 12

Re: WPF Not Binding Properly When Called From Another Project

Posted 26 September 2012 - 11:27 AM

Is any of your code dependent on what the current running directory is? By changing the startup project, the current running directory changes over to that project's output directory. If you have any dependent files, or configuration files, they will more likely not be found.

As an alternate test: If you just make a List<Foo> of your data where Foo is your class, and add at least one instance of Foo into the List, and then assign the list to the DataSource, does the UI populate? If so then it's probably not a binding issue, but rather a data loading issue. I know that you previously stated that the data seems to load correctly for one of your tabs. But is that same instance of data shared with other tabs, or are the other tabs responsible for loading their own data?

If you were following good MVC or MVVM practices, the first M is the Model. You should only ever have one instance of that Model and it is passed around to all the Views or View-Models that need it.

This post has been edited by Skydiver: 26 September 2012 - 11:28 AM

Was This Post Helpful? 1
  • +
  • -

#3 Tim.Simon  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 26
  • Joined: 12-June 12

Re: WPF Not Binding Properly When Called From Another Project

Posted 26 September 2012 - 11:46 AM

Thank you for your response, Skydiver. I do not have any code dependent on the current directory. The settings are being loaded from program data, and examining the objects as they are loaded shows that the Senders and Receivers are complete. I have also removed the settings file, and tried running it. In this case, it automatically creates an empty Sender and Receiver, which also do not show up.

I did not create the initial application, and since I have to extract the model from the view, I can say that MVC wsa not followed very well. One thing that I did notice while pouring through the code is that the manager is loaded twice, which means that the settings are loaded twice. However, when I commented it out, I found that it was not actually ever used. I have left it commented out, and the settings now only load once. Unfortunately, this did not fix the issue.

Thank you for taking the time to respond.

This post has been edited by Tim.Simon: 26 September 2012 - 11:53 AM

Was This Post Helpful? 0
  • +
  • -

#4 Tim.Simon  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 26
  • Joined: 12-June 12

Re: WPF Not Binding Properly When Called From Another Project

Posted 26 September 2012 - 12:58 PM

Update:

Just to test out a theory, I added a label that had no bindings at all to my summary panel.

                    <TextBlock Name="testTextBlock">
                        <ContentControl Content="This is just a test...  " Foreground="Red"></ContentControl>
                    </TextBlock>



This showed up when WPF was set as startup, but still didn't appear when the tray was set as startup. i don't think that this necessarily rules out a binding problem for the Sender and Receiver list, but it does tell me that the SenderSummary template is never being displayed for some reason.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3650
  • View blog
  • Posts: 11,419
  • Joined: 05-May 12

Re: WPF Not Binding Properly When Called From Another Project

Posted 27 September 2012 - 09:07 AM

Good job trying that out! That would have been the next thing I would have tried.

You might as well as well start with the simplest possible template first with no property binding. Just something that says "Here!". If that shows up, and shows up the correct number of instances, at least it's forward progress. If not, then that indicates that the binding issue is with the container that uses your template, rather than the template itself.
Was This Post Helpful? 1
  • +
  • -

#6 Tim.Simon  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 26
  • Joined: 12-June 12

Re: WPF Not Binding Properly When Called From Another Project

Posted 02 October 2012 - 08:27 AM

I finally figured this one out. Instead of instantiating the UI, the entire WPF application must be called to run. This will cause the App.xaml to load the dictionary, and other WPF forms can then access it. This is done with the following code:

        private void settingsEvent_Click(object sender, EventArgs e)
        {
            app.InitializeComponent();
            gui = new Sender_Receiver.mainWindow();
            app.Run(gui);
        }
        private static App app = new App(); 


This was discovered through experimentation, so i am sure it is not the best practice, but it works, and right now, that was what I needed.

Thank you, Skydiver, for your assistance. It was greatly appreciated.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1