第一个问题问了很长时间的潜伏者。
我正在开发一个带有实现RadGridView的视图的Silverlight应用程序。我有一个ViewModel,它将PersonSkills的ObservableCollection绑定到那个RadGridView。在模型中,PersonSkills是多对一的技能。描述是技能的一种属性。他们在SkillId上有一个外键加入(抱歉,没有足够的代表来发布图片)
我在RadGridView中的列绑定是到Skill.Description属性的。一切正常,直到我在这里没有显示的数据表单中进行了编辑。将触发PersonSkills集合,我可以看到更改后的值,更改将发送到数据库,但RadGridView会显示一个空单元格,而不是应该显示的Skill.Description。
我需要做什么才能让RadGridView反映对PersonSkills集合的子级技能集合的属性所做的更改?
<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");
}
}
}发布于 2012-09-15 21:34:23
我应该提到我也在使用RadDatForm。这就是罪魁祸首。我放弃了使用RadDataForm。更多的麻烦是值得的。相反,我通过视图模型中的绑定命令实现了一个自行开发的数据表单。更干净的IMHO。希望它能帮助其他人。
<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;
}
}https://stackoverflow.com/questions/12072409
复制相似问题