wf 4.0中的活动数据绑定

时间:2009-11-26 15:53:09

标签: c# wpf binding workflow-foundation-4 workflow-activity

我想在组合框中显示一些数据类型。数据类型包含在以下类中:

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));
        }
    }

}

目前我有一个绑定属性:

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;
        }
    }

通过WorkflowElement中的xaml连接:

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

我的组合框gType中没有任何东西。我错了什么?我是WPF和Workflow 4.0的新手,所以我觉得这对你来说并不难。

谢谢你的建议, EL

1 个答案:

答案 0 :(得分:0)

如果您的DatatypesDisplayed集合在UI最初绑定时为null,则后续更改将不会通知View ...您是否正在初始化类“构造函数中的CollectionView

另外 - 仔细检查您是否将视图的DataContext设置为您班级的实例...

干杯,伊恩

编辑:

好的 - 所以看看xaml中定义你的组合框的这一行:

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

这意味着应该出现在组合框中的'stuff'应该存在于名为DataTypesDisplayed的集合中。这可以是任何类型对象的集合,只要该对象公开名为“Name”的属性,因为我们将它用于DisplayMemberPath。此外,此集合应该是名为ModelItem的属性,而ModelItem应该是您的视图的DataContext的属性...

把它们放在一起,我希望看到这样的东西:

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; }
}

我不太确定为什么你在ItemsSource Binding的Path中有ModelItem,你可能会发现你不需要它 - 只需将CollectionView直接放在ViewModel中。 ..

相关问题