将选定的值绑定到组合框上以查看模型

发布于 2024-09-01 18:51:54 字数 245 浏览 2 评论 0原文

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

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

谢谢

I have my silverlight app which pulls data into a datagrid from a view model. The vm is exposed via Mef. I also have a details grid which has comboboxes. The vm also contains the data to populate the combobox values. Upon first load, everything works fine and the selected items on te comboboxes are correct and I can select alternative values. However, if I sort my main data grid (allow sort=true) then I find the binding for selected value on the comboboxes dissapear. The combobox is still populated with data but nothing is selected.

Has anyone come across this issue before? I am unsure how to solve this one.

Thanks

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

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

发布评论

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

评论(5

帅气尐潴 2024-09-08 18:51:54

Shaggy,我前几天刚刚注意到这一点,试图设置异步 ComboBox 加载。由于某种原因,组合框似乎只是删除了绑定(但您已经知道了)。无论如何,我整理了这篇文章来解决其中一些问题。如果有帮助请告诉我。

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

凯尔

Shaggy, I just noticed this the other day trying to setup async ComboBox loading. For some reason the ComboBox just appears to drop the binding (but you already knew that). Anyway, I put this post together that addresses some of these issues. Let me know if it helps.

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

Kyle

撩发小公举 2024-09-08 18:51:54

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

How are you gathering the Data for the combobox? Is it a list of strings or a list of specific objects? What could be occuring is that the sorting is creating another set of objects within its combobox, or each row of data, and the selected item is no longer matching the reference. Could you post a code example?

纸短情长 2024-09-08 18:51:54

组合框的代码如下。

 <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 集合。问题是在排序时,父表、组合框会丢失其选定的值,但组合框的数据保持不变。通过 fiddler,我可以看到没有任何后续调用来获取组合框的数据。

Code for the comboboxes is as follows

 <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}" />

The data is pulled from a VM, and the data is called using async calls at the start, and the variables are populated as follow:

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

so comboboxes are IEnumerable collections of various entities. The thing is upon sorting, the parent table, the combo boxes lose their selected value, but the data for the combobox remains intact. Via fiddler I can see that there arent any subsequent calls to fetch the data for the comboboxes.

少女七分熟 2024-09-08 18:51:54

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

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

One thought and issue I have had with SelectedValue before, is that when a combox, datagrid, etc... go through state changes like; Loss of focus, Redrawing, and a few other they will change the SelectedValue to null. It is possible, that when you choose a value the SelectedValue (bound properties) on the VM are set. However, when the Grid sorts it also tells the VM to set the SelectedValue to 'null'. So, after the sort the comboboxes are set to default values.

A thing you can try, is set a breakpoint at one of the SelectedValue properties 'set' and see how often the value is set, during Debug.Assert if the value is null.

美胚控场 2024-09-08 18:51:54

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

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

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

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

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

希望它有所帮助。

Not sure of your setup here, but if your datagrid is a list of calls and CurrentCall is the selected item can you not use Element Binding? E.g.

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

I assume the grid’s datacontext is bound to IEnumerable<Call> (or something) on your VM so I’d say a sort would result in a new collection (like if you said .Sort or order etc).

Here is a quick cut from a working example (using a listbox not datagrid in this case)

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

Hope it helps.

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