locked
WF4 Binding - This newbie to WF4 needs some serious help! RRS feed

  • Question

  • I can't bind a combobox to an observablevariable. This newbie to WF4 needs help. I've been able to bind every other way but this.

    Code:

    using System;
    using System.Activities;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Text;
    using System.Windows.Data;
    using System.Windows.Markup;

    namespace CommercialWorkflowActivity
    {
        [Designer(typeof(ComboBoxDesigner))]
        public sealed class ComboBoxCode : NativeActivity
        {
            public InArgument<string> Text { get; set; }

            protected override void Execute(NativeActivityContext context)
            {
                Text = "This is the Text";
                //string text = text = context.GetValue(this.Text);

            }

            //**** Start: Add variables on startup ****//
            public ComboBoxCode()
            {
                InstantiateVariables();
            }

            public void InstantiateVariables()
            {
                for (Int32 i = 1; i <= 5; i++)
                {
                    Variable<string> lTestVar = new Variable<string>();
                    lTestVar.Name = "TestVar" + i;

                    this.Variables.Add(lTestVar);
                }
            }

            ObservableCollection<Variable> variables;
            public ObservableCollection<Variable> Variables
            {
                get
                {
                    if (this.variables == null)
                    {
                        variables = new ObservableCollection<Variable>();
                    }
                    return variables;
                }
            }
            //**** End: Add variables on startup ****//
        }
    }

    *****************************************************************************

    Designer

    <sap:ActivityDesigner x:Class="CommercialWorkflowActivity.ComboBoxDesigner"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:c="clr-namespace:CommercialWorkflowActivity"
        xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"
        xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="100" d:DesignWidth="200">
        <sap:ActivityDesigner.Resources>
            <ResourceDictionary>
                <c:ComboBoxItemConverter x:Key="comboBoxItemConverter"/>
            </ResourceDictionary>
        </sap:ActivityDesigner.Resources>
        <Grid>
            <ComboBox Height="23" HorizontalAlignment="Left" Margin="36,12,0,0" VerticalAlignment="Top" Width="120"
                      ItemsSource="{Binding Path=Variables}" />
        </Grid>
    </sap:ActivityDesigner>

    Wednesday, October 26, 2011 5:12 PM

Answers

All replies

  • Make your ItemsSource = "{Binding Path=ModelItem.Variables}"
    Wednesday, October 26, 2011 11:02 PM
  • Thank you for the reply. When I set it to; ItemsSource = "{Binding Path=ModelItem.Variables}" I receive an ArgumentNullException as shown below.


    Robert
    Thursday, October 27, 2011 12:45 PM
  • Sorry, I don't think the screen clip displayed so here it is.


    Robert
    Thursday, October 27, 2011 12:46 PM
  • That's progress, at least now your binding ;-)

     

    As a WAG(Wild Assed Guess) I would suggest that you have null items in your collection somehow, or that the collection itself is null.  There is really no way for me to do more than guess given the amount of information that I have.

    Thursday, October 27, 2011 1:09 PM
  • Thank you for your response. Now at least I can move forward.
    Robert
    Thursday, October 27, 2011 1:12 PM
  • Hi Robert,
    ComboBox will do a call to ICollection.IndexOf(null) during initialization which indirectly causes the assert. The more immediate problem causing the assert is that ModelItemCollection throws ArgumentNullException for IndexOf(null). This is a known bug in .Net 4. I have worked around it in the past, discussed at the end of this post:

    http://blogs.msdn.com/b/tilovell/archive/2010/05/12/fixing-an-activity-designer-for-invokeaction-t.aspx

    Tim

    • Marked as answer by GoldenChair Friday, October 28, 2011 8:33 PM
    Thursday, October 27, 2011 10:36 PM
  • Tim,

    Thank you for your help. Your example helped us out and we got the combobox to bind.

    Robert


    Robert
    Friday, October 28, 2011 8:35 PM