wf 4.0 中的活动数据绑定

发布于 2024-08-12 04:33:25 字数 1994 浏览 11 评论 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 的新手,所以我认为这对您来说并不难。

感谢您的建议, 埃尔

i want to display some datatypes in a combobox. the datatypes are wrapped in the following class:

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

}

currently i have a property for the binding:

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

which is connected via xaml in the WorkflowElement:

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

I dont get anything in my combobox gType. What did i wrong? I am new to WPF and Workflow 4.0 so i think this isnt a hard one for you.

Thanks in advice,
el

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩套路吗 2024-08-19 04:33:25

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

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

干杯,Ian

编辑:

好的 - 所以看看定义组合框的 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 中...

If your DatatypesDisplayed collection is null when the UI initially binds to it, then subsequent changes will not be notified to the View... are you initialising the CollectionView in your class' constructor?

Also - double check that you're setting the View's DataContext to be an instance of your class...

Cheers, Ian

EDIT:

OK - so have a look at this line in the xaml that's defining your combobox:

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

this means that the 'stuff' that should appear in your combo box should live in a collection called DataTypesDisplayed. This can be a collection of any kind of objects, as long as that object exposes a property called 'Name', because we are using this for the DisplayMemberPath. Furthermore, this collection should be a property of something called ModelItem, and ModelItem should be a property of whatever it is that is the DataContext for your view...

put it all together, and I would expect to see something like this:

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

I'm not too sure why you have the ModelItem in the Path of your ItemsSource Binding, and you may find that you don't need it - just place the CollectionView directly in the ViewModel...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文