【问题标题】:Activity Databinding in wf 4.0wf 4.0 中的活动数据绑定
【发布时间】:2009-11-26 15:53:09
【问题描述】:

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

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 的新手,所以我认为这对你来说并不难。

感谢您的建议, 埃尔

【问题讨论】:

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


    【解决方案1】:

    如果您的 DatatypesDisplayed 集合在 UI 最初绑定到它时为 null,那么后续更改将不会通知给视图...您是否在类的构造函数中初始化 CollectionView

    另外 - 仔细检查您是否将 View 的 DataContext 设置为您的类的实例...

    干杯,伊恩

    编辑:

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

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

    这意味着应该出现在组合框中的“东西”应该存在于一个名为 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 中即可。 ..

    【讨论】:

    • 感谢您的遮阳篷。我的 CollectionView 最初不是空的。当我在 WF 中定义我的设计器时,是否必须设置 DataContext?如何设置 DataContext?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多