Silverlight使用MVVM将集合绑定到DataForm中的Combobox

发布于 2024-08-13 04:00:54 字数 1992 浏览 5 评论 0原文

我有这个问题,我有使用 MVVM 编写的 Silverlight 应用程序。我需要创建绑定到 ViewModel 上的属性的 DataForm,并且我想添加 ComboBox 并用同一 ViewModel 中其他集合中的值填充它。

代码:

<dataFormToolkit:DataForm CurrentItem="{Binding NewUser, Mode=TwoWay}" AutoGenerateFields="False" Height="298">
            <dataFormToolkit:DataForm.EditTemplate>
                <DataTemplate>
                    <StackPanel>

                        <dataFormToolkit:DataField Label="Email">
                            <TextBox Text="{Binding Email, Mode=TwoWay}"/>
                        </dataFormToolkit:DataField>

                        <dataFormToolkit:DataField Label="Język">
                            <ComboBox ItemsSource="{Binding Path=Languages, Mode=TwoWay}"/>
                        </dataFormToolkit:DataField>

                    </StackPanel>
                </DataTemplate>
            </dataFormToolkit:DataForm.EditTemplate>
        </dataFormToolkit:DataForm>

所有这些都是由 NewAccountVM 处理的,它具有以下属性:

private User newUser;
    public User NewUser { 
        get 
        { 
            return newUser; 
        }
        set
        {
            if (value != newUser)
            {
                newUser = value;
                RaisePropertyChanged("NewUser");
            }
        }
    }

    private ObservableCollection<Language> languages;

    public ObservableCollection<Language> Languages
    {
        get { return languages; }
        set 
        {
            if (languages != value)
            {
                languages = value;
                RaisePropertyChanged("Languages");
            }
        }
    }

现在,除了将 ItemsSource 添加到 ComboBox 之外,所有这些都有效。我发现很多例子展示了如何在 CodeBehind 中填充 CB,但就像我说的,我想用 MVVM 风格来做到这一点:) 据我所知,ComboBox 从 DataForm 继承了 DataContext,而这个 ItemsSource="{Binding Path=Languages, Mode=TwoWay}" 将不起作用,但我不知道如何实现我的目标。

有人可以帮助我吗?

I have this problem, I've got Silverlight app written using MVVM. I need to create DataForm which is binded to property on ViewModel and I want to add ComboBox and fill it with values from other collection in the same ViewModel.

Code:

<dataFormToolkit:DataForm CurrentItem="{Binding NewUser, Mode=TwoWay}" AutoGenerateFields="False" Height="298">
            <dataFormToolkit:DataForm.EditTemplate>
                <DataTemplate>
                    <StackPanel>

                        <dataFormToolkit:DataField Label="Email">
                            <TextBox Text="{Binding Email, Mode=TwoWay}"/>
                        </dataFormToolkit:DataField>

                        <dataFormToolkit:DataField Label="Język">
                            <ComboBox ItemsSource="{Binding Path=Languages, Mode=TwoWay}"/>
                        </dataFormToolkit:DataField>

                    </StackPanel>
                </DataTemplate>
            </dataFormToolkit:DataForm.EditTemplate>
        </dataFormToolkit:DataForm>

All this is handled by NewAccountVM which has these properties:

private User newUser;
    public User NewUser { 
        get 
        { 
            return newUser; 
        }
        set
        {
            if (value != newUser)
            {
                newUser = value;
                RaisePropertyChanged("NewUser");
            }
        }
    }

    private ObservableCollection<Language> languages;

    public ObservableCollection<Language> Languages
    {
        get { return languages; }
        set 
        {
            if (languages != value)
            {
                languages = value;
                RaisePropertyChanged("Languages");
            }
        }
    }

Now, all this works besides adding ItemsSource to ComboBox. I've found many examples showing how fill CB in CodeBehind, but like I said I want to do this in MVVM-Style :)
I understand that, ComboBox inherited DataContext from DataForm, and this ItemsSource="{Binding Path=Languages, Mode=TwoWay}" will not work, but I have no idea how to achieve my goal.

Can somebody help me?

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

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

发布评论

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

评论(3

愛上了 2024-08-20 04:00:54

1)在资源部分声明视图模型。

<UserControl.Resources>
    <local:MyViewModel x:Key="myViewModel" />
</UserControl.Resources>

2) 将 ComboBox 绑定到视图模型上的集合属性。

<ComboBox ItemsSource="{Binding Path=Languages, 
                                Source={StaticResource myViewModel}, 
                                Mode=TwoWay}"/>

1) Declare the viewmodel to the view in the resources section.

<UserControl.Resources>
    <local:MyViewModel x:Key="myViewModel" />
</UserControl.Resources>

2) Bind the ComboBox to the collection property on the viewmodel.

<ComboBox ItemsSource="{Binding Path=Languages, 
                                Source={StaticResource myViewModel}, 
                                Mode=TwoWay}"/>
另类 2024-08-20 04:00:54

您可以将 XAML 中的数据上下文设置为静态资源,如下所示:

<UserControl.DataContext>
    <Binding Source="{StaticResource myViewModel}" />
</UserControl.DataContext>

you can set the Data Context in XAML to your static resource like so:

<UserControl.DataContext>
    <Binding Source="{StaticResource myViewModel}" />
</UserControl.DataContext>
暖心男生 2024-08-20 04:00:54

场景A:
1. 假设您希望使用所有成员角色填充组合,并允许客户端选择角色并分配给用户:
即对象A:Aspnet_Role
即ObjectB:用户

  1. 让我们说User.MembershipRoleId将绑定到Aspnet_Role.RoleId

  2. 数据表单绑定到 ObjectB

  3. 数据表单中的组合框填充列表
  4. 在 XAML 中编写以下内容:

    <代码>

这里的映射是,ObjectB.MembershipRoleId=ObjectA.RoleId

场景 B:
1. 如果您不想通过场景A中的方式显式定义,那么在这种情况下,在数据库中的表之间定义一个ForeignKey-PrimaryKey关系,例如
外键->用户.会员ID
主键-> Aspnet_Roles.RoleId
2. 在 ADO.NET (.edmx) 文件中,从数据库更新模型,您将观察到在 User 实体中存在与实体 Aspnet_Roles 建立的关联
3. 在XAML中编写如下代码将组合框绑定到数据表单的所需字段

<Combobox DisplayMemberPath="RoleName" SelectedItem="{Binding MembershipRoleId,Mode=TwoWay}" .... />

Scenario A:
1. Assume you wish to populate a combo with all the membership Roles, and allow the client to select the role and assign to the User :
i.e. ObjectA : Aspnet_Role
i.e. ObjectB : User

  1. Let us say User.MembershipRoleId is to be bound to Aspnet_Role.RoleId

  2. Dataform is bound to ObjectB

  3. Combobox in dataform is populated with List
  4. In XAML write the following:

    <Combobox DisplayMemberPath="RoleName"
    SelectedValue="{Binding MembershipRoleId,Mode=TwoWay}" SelectedValuePath="RoleId" />

here the mapping is, ObjectB.MembershipRoleId=ObjectA.RoleId

Scenario B:
1. If you do not want to explicitly define by the way in ScenarioA, then in that case, define a ForeignKey-PrimaryKey relationship between the tables in the database like
ForeignKey -> User.MembershipId
PrimaryKey -> Aspnet_Roles.RoleId
2. From the ADO.NET (.edmx) file, update the model from the database, you will observe that in the User entity there is an association made upon entity Aspnet_Roles
3. In XAML write the code as below to bind the combobox, to the desired field of the Dataform

<Combobox DisplayMemberPath="RoleName" SelectedItem="{Binding MembershipRoleId,Mode=TwoWay}" .... />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文