【问题标题】:Bind selected value on combobox to view model将组合框上的选定值绑定到视图模型
【发布时间】:2010-05-12 13:48:22
【问题描述】:

我的 silverlight 应用程序可以将数据从视图模型中提取到数据网格中。 vm 通过 Mef 公开。我还有一个包含组合框的详细信息网格。 vm 还包含用于填充组合框值的数据。第一次加载时,一切正常,组合框上的选定项目是正确的,我可以选择替代值。但是,如果我对我的主数据网格进行排序(允许 sort=true),那么我会发现组合框上选定值的绑定消失了。组合框仍填充有数据,但未选择任何内容。

以前有人遇到过这个问题吗?我不确定如何解决这个问题。

谢谢

【问题讨论】:

    标签: silverlight data-binding mef mvvm-light


    【解决方案1】:

    Shaggy,前几天我才注意到这一点,试图设置异步 ComboBox 加载。出于某种原因,ComboBox 似乎只是放弃了绑定(但您已经知道了)。无论如何,我把这篇文章放在一起解决了其中一些问题。如果有帮助,请告诉我。

    http://blogs.msdn.com/kylemc/archive/2010/06/18/combobox-sample-for-ria-services.aspx

    凯尔

    【讨论】:

      【解决方案2】:

      您如何为组合框收集数据?它是字符串列表还是特定对象列表?可能发生的情况是排序正在其组合框中或每一行数据中创建另一组对象,并且所选项目不再与引用匹配。可以发个代码示例吗?

      【讨论】:

        【解决方案3】:

        组合框代码如下

         <TextBlock>Status</TextBlock>
                <ComboBox x:Name="CB_Status"   ItemsSource="{Binding Status}" SelectedValuePath="StatusId" SelectedValue="{Binding CurrentCall.StatusId, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" ItemTemplate="{StaticResource StatusTemplate}" />
                <TextBlock>Priority</TextBlock>
                <ComboBox x:Name="CB_Priority"  ItemsSource="{Binding Priorities}" SelectedValuePath="PriorityId" SelectedValue="{Binding CurrentCall.PriorityId, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" ItemTemplate="{StaticResource PriorityTemplate}"/>
                <TextBlock>Issue Type</TextBlock>
                <ComboBox x:Name="CB_IssueType" ItemsSource="{Binding IssueType}" SelectedValuePath="IssueTypeId" SelectedValue="{Binding CurrentCall.IssueTypeId, Mode=TwoWay,NotifyOnValidationError=True, ValidatesOnExceptions=True}" ItemTemplate="{StaticResource IssueTemplate}" />
        

        从虚拟机中提取数据,并在开始时使用异步调用调用数据,变量填充如下:

           private IEnumerable<Priority> _priorities;
        
            public IEnumerable<Priority> Priorities
            {
                get { return _priorities; }
                set
                {
                    if (value != _priorities)
                    {
                        _priorities = value;
                        this.RaisePropertyChanged("Priorities");
                    }
                }
            }
         private IEnumerable<Status> _status;
        
            public IEnumerable<Status> Status
            {
                get { return _status; }
                set
                {
                    if (value != _status)
                    {
                        _status = value;
                        this.RaisePropertyChanged("Status");
                    }
                }
            }
        
        
            private IEnumerable<IssueType> _issueType;
        
            public IEnumerable<IssueType> IssueType
            {
                get { return _issueType; }
                set
                {
                    if (value != _issueType)
                    {
                        _issueType = value;
                        this.RaisePropertyChanged("IssueType");
                    }
                }
            }
        

        所以组合框是各种实体的 IEnumerable 集合。事情是在排序时,父表,组合框失去了它们选择的值,但组合框的数据保持不变。通过提琴手,我可以看到没有任何后续调用来获取组合框的数据。

        【讨论】:

          【解决方案4】:

          我之前对 SelectedValue 的一个想法和问题是,当组合框、数据网格等...经历状态变化时,例如;失去焦点、重绘和其他一些它们会将 SelectedValue 更改为 null。当您选择一个值时,可能会设置 VM 上的 SelectedValue(绑定属性)。但是,当 Grid 排序时,它还会告诉 VM 将 SelectedValue 设置为“null”。因此,在排序之后,组合框被设置为默认值。

          您可以尝试的一件事是在 SelectedValue 属性“set”之一处设置断点,并查看在 Debug.Assert 期间设置值的频率(如果值为 null)。

          【讨论】:

            【解决方案5】:

            不确定您在此处的设置,但如果您的数据网格是调用列表并且 CurrentCall 是选定项,您可以不使用元素绑定吗?例如

            <ComboBox x:Name="CB_Status"   
                                  ItemsSource="{Binding Status}" 
                                 SelectedItem="{ Path=SelectedItem.Status, Mode=TwoWay, ElementName=YOUR_DATAGRID}" 
                                 ItemTemplate="{StaticResource StatusTemplate}" /> 
            

            我假设网格的数据上下文绑定到你的 VM 上的 IEnumerable&lt;Call&gt;(或其他东西),所以我会说排序会产生一个新的集合(就像你说的 .Sort 或 order 等)。

            这是一个工作示例的快速剪辑(在这种情况下使用列表框而不是数据网格)

            <ComboBox 
               DisplayMemberPath="DisplayName"
               SelectedItem="{Binding Path=SelectedItem.Individual.IndividualNameTitle, 
                               Mode=TwoWay, ElementName=AccountList}"
               ItemsSource="{Binding Path=IndividualNameTitles}">
            </ComboBox>
            

            希望对你有帮助。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-10-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多