views:

279

answers:

1

Hi there,

i want to display some datatypes in a combobox. the datatypes are wrapped in the following class:

public class TDataTypeBinder: INotifyPropertyChanged
{
    private string name;
    public string Name
    {
        get
        {
            return name ;
        }
        set
        {
            name = value;
            OnPropertyChanged("Name");
        }
    }

    private DataType datatype;
    public DataType Datatype
    {
        get
        {
            return datatype;
        }
        set
        {
            datatype = value;
            OnPropertyChanged("Datatype");
        }
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="TDataTypeBinder"/> class.
    /// </summary>
    /// <param name="valueToSelect">The value to select.</param>
    public TDataTypeBinder(string valueToSelect)
    {
        Name = valueToSelect;
    }


    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propName)
    {
        PropertyChangedEventHandler eh = this.PropertyChanged;
        if (null != eh)
        {
            eh(this, new PropertyChangedEventArgs(propName));
        }
    }

}

currently i have a property for the binding:

public CollectionView DatatypesDisplayed
    {
        get
        {

            List<TDataTypeBinder> list = new List<TDataTypeBinder>();
            list.Add(new TDataTypeBinder("String"));
            list.Add(new TDataTypeBinder("Float"));
            list.Add(new TDataTypeBinder("Integer"));

            myDatatypes = new CollectionView(list);
            return myDatatypes;
        }
    }

which is connected via xaml in the WorkflowElement:

<... WorkflowViewElement ...
<ComboBox Name="gType" ItemsSource="{Binding Path=ModelItem.DatatypesDisplayed }" DisplayMemberPath="Name" Margin="3" MinWidth="150" Height="20" />

I dont get anything in my combobox gType. What did i wrong? I am new to WPF and Workflow 4.0 so i think this isnt a hard one for you.

Thanks in advice, el

A: 

If your DatatypesDisplayed collection is null when the UI initially binds to it, then subsequent changes will not be notified to the View... are you initialising the CollectionView in your class' constructor?

Also - double check that you're setting the View's DataContext to be an instance of your class...

Cheers, Ian

EDIT:

OK - so have a look at this line in the xaml that's defining your combobox:

<ComboBox Name="gType" ItemsSource="{Binding Path=ModelItem.DatatypesDisplayed }" DisplayMemberPath="Name" Margin="3" MinWidth="150" Height="20" />

this means that the 'stuff' that should appear in your combo box should live in a collection called DataTypesDisplayed. This can be a collection of any kind of objects, as long as that object exposes a property called 'Name', because we are using this for the DisplayMemberPath. Furthermore, this collection should be a property of something called ModelItem, and ModelItem should be a property of whatever it is that is the DataContext for your view...

put it all together, and I would expect to see something like this:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        // Set the View's DataContext to be an instance of the class that contains your CollectionView...
        this.DataContext = new MainWindowViewModel();
    }
}


public class MainWindowViewModel
{
    public MainWindowViewModel()
    {
    }

    public object ModelItem { get; set; }
}

public class ModelItem
{
    public CollectionView DataTypesDisplayed { get; set; }
}

I'm not too sure why you have the ModelItem in the Path of your ItemsSource Binding, and you may find that you don't need it - just place the CollectionView directly in the ViewModel...

IanR
Thank you for your awnser. My CollectionView is not null initialy. Do i have to set a DataContext when i have defined my designer in WF? How can i set a DataContext?
elCapitano