【问题标题】:RadGridView not displaying PropertyChangedRadGridView 不显示 PropertyChanged
【发布时间】:2012-08-22 11:47:28
【问题描述】:

第一次问题问了很长时间的潜伏者。

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

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

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

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

        }
    }

【问题讨论】:

    标签: silverlight viewmodel inotifypropertychanged radgridview


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 2012-03-19
      • 2010-12-10
      • 2012-11-13
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多