[英]Activity Databinding in 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;
}
}
通過WorkflowElement中的xaml連接:
<... WorkflowViewElement ...
<ComboBox Name="gType" ItemsSource="{Binding Path=ModelItem.DatatypesDisplayed }" DisplayMemberPath="Name" Margin="3" MinWidth="150" Height="20" />
我的組合框gType
沒有任何內容。 我怎么了 我是WPF和Workflow 4.0的新手,所以我認為這對您來說並不困難。
謝謝你的建議,埃爾
如果UI最初綁定到您的DatatypesDisplayed集合時為null,則隨后的更改將不會通知給View ...您是否在類的構造函數中初始化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綁定的路徑中有ModelItem,而您可能會發現不需要它-只需將CollectionView直接放在ViewModel中即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.