2009-11-26 67 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