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

谢谢你的建议,埃尔

4

1 回答 1

0

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

另外-仔细检查您是否将视图设置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 的路径中有 ModelItem,您可能会发现不需要它 - 只需将 CollectionView 直接放在 ViewModel 中...

于 2009-11-26T21:26:49.077 回答