Rah*_*hul 2 c# wpf binding datagrid caliburn.micro
我在 WPF 应用程序中使用 Caliburn Micro 框架,我需要将集合绑定到 DatGrid 的 ItemsSource。请考虑以下代码:
班级
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public ObservableCollection<Subject> Subjects;
}
public class Subject
{
public string Title{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
查看模型
public class PersonViewModel : Screen
{
private Person _person;
public Person Person
{
get { return _person; }
set
{
_person = value;
NotifyOfPropertyChange(() => Person);
NotifyOfPropertyChange(() => CanSave);
}
}
....
}
Run Code Online (Sandbox Code Playgroud)
看法
<UserControl x:Class="CalCompose.ViewModels.PersonView" ...ommited... >
<Grid Margin="0">
<TextBox x:Name="Person_Id" HorizontalAlignment="Left" Height="23" Margin="10,52,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="Person_Name" HorizontalAlignment="Left" Height="23" Margin="10,90,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<DataGrid ItemsSource="{Binding Person_Subjects}" Margin="10,177,0,0"></DataGrid>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
问题 1: 当我运行应用程序时,TextBoxes 获得正确的值,但数据网格尚未填充。在这里,我使用约定“ClassName_PropertyName”的深度属性绑定技术。
问题 2 当我更改“名称”属性的值时,永远不会调用 NotifyOfPropertyChange(() => Person)。我想在 Name 字段中的文本更改时调用 guard 方法。
谁能建议我一个简单的解决方案来克服这个问题?提前致谢。
PropertyChangedBase在Person类上实现,然后Name我们可以写
private string name;
public string Name
{
get { return name; }
set
{
if (name == value)
return;
name = value;
NotifyOfPropertyChange(() => Name);
}
}
Run Code Online (Sandbox Code Playgroud)
对于绑定到DataGrid,不要使用“深度绑定”,只需使用
<DataGrid ItemsSource="{Binding Person.Subjects}" ...
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助。
| 归档时间: |
|
| 查看次数: |
5157 次 |
| 最近记录: |