0
votes

First time question asked long time lurker.

I'm working on a Silverlight app with a view that implements a RadGridView. I have a ViewModel that binds a ObservableCollection of PersonSkills to that RadGridView. In the Model, PersonSkills is many to one Skill. Description is a property of Skill. They are joined by a foreign key on SkillId (Sorry not enough rep to post an image)

My column bindings in the RadGridView are to the Skill.Description property. Everything works fine until I make an edit in a dataform not represented here. The PersonSkills collection fires and I can see the changed value, and the change posts to the database but the RadGridView displays an empty cell instead of the Skill.Description like it should.

What do I need to do to get the RadGridView to reflect the changes that are made to a Property of the Skill collection which is a child of the PersonSkills collection?

   <telerik:RadGridView
        x:Name="skillsGrid"
        ItemsSource="{Binding PersonSkills, Mode=TwoWay}"
        SelectedItem="{Binding CurrentPersonSkill, Mode=TwoWay}"
        ColumnWidth="*">
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn
                Header="SKILL"
                DataMemberBinding="{Binding Skill.Description}"
                IsGroupable="False"
                Width="2*" />
        </telerik:RadGridView.Columns>
   </telerik:RadGridView>


private ObservableCollection<PersonSkill> personSkills;
    public ObservableCollection<PersonSkill> PersonSkills
    {
        get
        {
            return this.personSkills;
        }
        set
        {
            this.personSkills = value;
            this.OnUiThread(() =>
            {
                this.RaisePropertyChanged("PersonSkills","CurrentPersonSkill");
            });

        }
    }


private PersonSkill currentPersonSkill;
    public PersonSkill CurrentPersonSkill
    {
        get
        {
            return this.currentPersonSkill;
        }
        set
        {
            if (this.currentPersonSkill != value)
            {
                this.currentPersonSkill = value;
                this.RaisePropertyChanged("PersonSkills","CurrentPersonSkill");
            }

        }
    }
1

1 Answers

0
votes

I should have mentioned I was using a RadDatForm as well. It was the culprit. I gave up on using the RadDataForm. It was more trouble that it was worth. Instead I implemented a homegrown dataform through bound Commands in the viewmodel. Much cleaner IMHO. Hopefully it helps someone else out.

<Grid
        x:Name="readOnlyForm"
        Visibility="{Binding RequestFormInEdit, Converter={StaticResource InvertedBooleantToVisibilityConverter}}">
        <StackPanel>
        <TextBlock
            Text="{Binding PersonSkill.Skill.Description}"/>
        </StackPanel>
        <StackPanel>
            <telerik:RadButton
                Command="{Binding AddCommand}"
                Tag="ADD">
            </telerik:RadButton>
            <telerik:RadButton
                Command="{Binding EditCommand}"
                Tag="EDIT">
            </telerik:RadButton>
        </StackPanel>
    </Grid>
    <Grid
        x:Name="editForm"
        Visibility="{Binding FormInEdit, Converter={StaticResource BooleantToVisibilityConverter}}">
        <Grid>
            <StackPanel>
                <Grid>
                    <TextBlock
                        Text="SKILL"/>
                    <TextBox                                                                                                                Text="{Binding PersonSkill.Skill.Description, Mode=TwoWay}"/>
                </Grid>
            </StackPanel>
            <StackPanel>
                <telerik:RadButton
                    Tag="SAVE"
                    Command="{Binding SubmitCommand}">
                </telerik:RadButton>
                <telerik:RadButton
                    Tag="CANCEL"
                    Command="{Binding CancelCommand}">
                </telerik:RadButton>
            </StackPanel>
        </Grid>
    </Grid>
</Grid>

And in my view model I've added the following

private bool FormInEdit;
    public bool FormInEdit
    {
        get
        {
            return FormInEdit;
        }
        private set
        {
            if (FormInEdit != value)
            {
                FormInEdit = value;
                RaisePropertyChanged("FormInEdit");
            }
        }
    }

    private DelegateCommand addCommand;
    public DelegateCommand AddCommand
    {
        get
        {
            if (addCommand == null)
            {
                addCommand = new DelegateCommand(
                   OnAddCommand);
            }
            return addCommand;
        }
    }
    private void OnAddCommand()
    {

        // create a new personskill code goes here


        // and begin edit

        FormBeginEdit();

    }
    private DelegateCommand editCommand;
    public DelegateCommand EditCommand
    {
        get
        {
            if (editCommand == null)
            {
                editCommand = new DelegateCommand(
                    OnEditCommand);
            }
            return editCommand;
        }
    }
    private void OnEditCommand()
    {
       if (CurrentPersonSKill != null)
       {
            if (FormInEdit)
            {

            }
            else
            {
                FormBeginEdit();
            }
        }

    }

    private DelegateCommand cancelCommand;
    public DelegateCommand CancelCommand
    {
        get
        {
            if (cancelCommand == null)
            {
                cancelCommand = new DelegateCommand(
                    OnCancelCommand,
                                                                                            () => (CurrentPersonSkill != null) && FormInEdit);
            }
            return cancelCommand;
        }
    }
    private void OnCancelCommand()
    {
        if (CurrentPersonSkill != null)
        {
            FormCancelEdit();
        }
        FormEndEdit();
    }

    private DelegateCommand submitCommand;
    public DelegateCommand SubmitCommand
    {
        get
        {
            if (submitCommand == null)
            {
                submitCommand = new DelegateCommand(
                    OnSubmitCommand,
                                                                                            () => (CurrentPersonSkill != null) && FormInEdit);
            }
            return submitCommand;
        }
    }
    private void OnSubmitCommand()
    {
            if (CurrentPersonSkill != null)
            {
                //submit the PersonSkill here
                FormEndEdit();
            }
    }



    private void FormBeginEdit()
    {
        if CurrentPersonSkill != null)
        {
            FormInEdit = true;
        }
    }
    private void FormEndEdit()
    {
        FormInEdit = false;
    }
    private void FormCancelEdit()
    {
        if CurrentPersonSkill != null)
        {
            FormInEdit = false;
        }
    }

    private void OnViewModelPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        switch (e.PropertyName)
        {
            case "FormInEdit":
                SubmitCommand.RaiseCanExecuteChanged();
                CancelCommand.RaiseCanExecuteChanged();
                break;
            case "CurrentPersonSkill":
                SubmitCommand.RaiseCanExecuteChanged();
                CancelCommand.RaiseCanExecuteChanged();
                break;
        }
    }