RadGridView不显示PropertyChanged

时间:2012-08-22 11:47:28

标签: silverlight viewmodel inotifypropertychanged radgridview

第一次质疑长时间潜伏的问题。

我正在使用一个实现RadGridView的视图的Silverlight应用程序。我有一个ViewModel,它将PersonSkills的ObservableCollection绑定到RadGridView。在模型中,PersonSkills是多对一的技能。描述是Skill的财产。他们在SkillId上加了一个外键(抱歉没有足够的代表发布图片)

我在RadGridView中的列绑定属于Skill.Description属性。一切正常,直到我在这里没有表示的数据形式中进行编辑。 PersonSkills集合触发,我可以看到更改的值,更改发布到数据库,但RadGridView显示一个空单元格而不是像它应该的Skill.Description。

我需要做些什么才能让RadGridView反映对作为PersonSkills集合的子项的Skill集合的属性所做的更改?

   <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 个答案:

答案 0 :(得分:0)

我应该提到我也在使用RadDatForm。这是罪魁祸首。我放弃了使用RadDataForm。它值得更麻烦。相反,我通过viewmodel中的绑定命令实现了自行开发的数据表单。更清洁恕我直言。希望它可以帮助其他人。

<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>

在我的视图模型中,我添加了以下内容

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