简体   繁体   English

wf 4.0中的活动数据绑定

[英]Activity Databinding in wf 4.0

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: 通过WorkflowElement中的xaml连接:

<... 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 . 我的组合框gType没有任何内容。 What did i wrong? 我怎么了 I am new to WPF and Workflow 4.0 so i think this isnt a hard one for you. 我是WPF和Workflow 4.0的新手,所以我认为这对您来说并不困难。

Thanks in advice, el 谢谢你的建议,埃尔

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? 如果UI最初绑定到您的DatatypesDisplayed集合时为null,则随后的更改将不会通知给View ...您是否在类的构造函数中初始化CollectionView

Also - double check that you're setting the View's DataContext to be an instance of your class... 另外-再次检查您是否将View的DataContext设置为类的实例...

Cheers, Ian 干杯,伊恩

EDIT: 编辑:

OK - so have a look at this line in the xaml that's defining your combobox: 好的-因此,请在定义您的组合框的xaml中查看以下行:

<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. 这意味着应该出现在您的组合框中的“东西”应该位于一个名为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. 只要该对象公开名为“ Name”的属性,它就可以是任何类型的对象的集合,因为我们将其用于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... 此外,此集合应该是称为ModelItem的属性,而ModelItem应该是您视图的DataContext的属性...

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... 我不太确定为什么在ItemsSource绑定的路径中有ModelItem,而您可能会发现不需要它-只需将CollectionView直接放在ViewModel中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM