2009-11-26 68 views
0

我想在组合框中显示一些数据类型。该数据类型被包裹在下面的类:活动数据绑定在wf 4.0中

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

经由XAML在一个WorkflowElement连接:

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

我不在我的组合框gType中获得任何东西。我错了什么?我是WPF和Workflow 4.0的新手,所以我认为这对你来说不是一件难事。

谢谢咨询, EL

回答

0

如果您DatatypesDisplayed集合为空时,UI最初结合到它,那么后续的更改不会通知查看......你在初始化你的类的CollectionView构造函数?

而且 - 仔细检查你设置视图的DataContext是你的类的实例...

干杯,伊恩

编辑:

OK - 所以来看看这条线在真实定义您的组合框的XAML:

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

这意味着“东西”应该出现在您的组合框应该生活在一个收藏品n称为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; } 
} 

我也不太清楚为什么有ModelItem在你的ItemsSource结合的路径,你会发现,你不需要它 - 只要直接放在中的CollectionView在ViewModel ...

+0

谢谢你的awnser。我的CollectionView不是null initialy。当我在WF中定义我的设计器时,是否必须设置DataContext?我怎样才能设置一个DataContext? – elCapitano 2009-11-27 08:46:29