Bind selected value to combobox to view model

I have a silverlight application that outputs data in a datagrid from a view model. Vm is exposed through Mef. I also have a parts grid that has comboboxes. Vm also contains data to populate combobox values. On first boot, everything works fine, and the selected items in comboboxes are correct, and I can choose alternative values. However, if I sort my main data grid (allow sort = true), then I find the binding for the selected value in comboboxes dissapear. The mapping is still populated with data but nothing is selected.

Has anyone encountered this problem before? I am not sure how to solve this problem.

thank

+3
source share
5 answers

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

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

Kyle

+1
source

How do you collect data for combobox? Is this a list of strings or a list of specific objects? What can happen is that sorting creates a different set of objects inside its combobox or each row of data, and the selected item no longer matches the link. Could you give some example code?

0

comboboxes

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

comboboxes IEnumerable . , , , combobox . fiddler , comboboxes.

0

, SelectedValue , , combox, datagrid .. , ; , SelectedValue null. , , SelectedValue ( ) . , Grid , VM SelectedValue "null". , comboboxes .

, , SelectedValue , Debug.Assert, null.

0

, datagrid - , CurrentCall - , ? .

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

, datacontext grid IEnumerable<Call> ( -) , Id , (, . ..).

( datagrid)

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

, .

0

Source: https://habr.com/ru/post/1745114/


All Articles