wpf 绑定的问题

现在是这样,我用mvvm框架写的程序,出现了一个问题,ViewMode层是类嵌套类的关系,假设

 1     class DataPresentCategoryViewModel:NotificationObject
 2     {
 3 
 4         public DataPresentCategoryViewModel()
 5         {
 6             LoadCategoryViewModel load = new LoadCategoryViewModel();
 7             load.LoadDataCategory = new List<LoadDataCategoryViewModel>();
 8         }
 9         private string _iskName;
10 
11         public string ISKName
12         {
13             get { return _iskName; }
14             set
15             {
16                 _iskName = value;
17                 this.RaisePropertyChanged("ISKName");
18             }
19         }
20 
21         private int _selectedScientificLoadName;
22 
23         public int SelectedScientificLoadName
24         {
25             get { return _selectedScientificLoadName; }
26             set
27             {
28                 _selectedScientificLoadName = value;
29                 this.RaisePropertyChanged("SelectedScientificLoadName");
30             }
31         }
32 
33         private int _selectedEngineeringLoadName;
34 
35         public int SelectedEngineeringLoadName
36         {
37             get { return _selectedEngineeringLoadName; }
38             set
39             {
40                 _selectedEngineeringLoadName = value;
41                 this.RaisePropertyChanged("SelectedEngineeringLoadName");
42             }
43         }
44 
45         private LoadCategoryViewModel _presentLoadCategory;
46 
47         public LoadCategoryViewModel PresentLoadCategory
48         {
49             get { return _presentLoadCategory; }
50             set
51             {
52                 _presentLoadCategory = value;
53                 this.RaisePropertyChanged("PresentLoadCategory");
54             }
55         }
56     }
57 }

行中 47 是被此类封装的另一个类。

现在按照msdn上绑定的介绍,我在MainWindow中直接对windows.dataContent进行引用赋值

1         public MainWindow()
2         {
3             InitializeComponent();
4             this.DataContext = new DataPresentCategoryViewModel();
5         }

 现在在xaml界面我需要对DataPresentCategoryViewModel类中的LoadCategoryViewModel里面的内容进行调用,

{..........Visibility="{Binding IsVisable, Mode=TwoWay}

红色部分应该怎么写,问题出在这里,我不知道对象下面的属性,怎么调用,还是这种绑定的用法不对,我只是想把界面通过xml持久化,并且更容易的改变配置。

翻翻过去那场雪
浏览 230回答 2
2回答

江户川乱折腾

这里有个问题, 你构造函数里创建的LoadCategoryViewModel与你的属性LoadCategoryViewModel不是一个对象,你应该找不到在构造函数里赋的值。 除非在构造函数里这样写: this.PresentLoadCategory=new LoadCategoryViewModel();this.PresentLoadCategory.LoadDataCategory = new List<LoadDataCategoryViewModel>(); 绑定就比较简单了,因为你用的时数据上下文,所以: {..........Visibility="{Binding PresentLoadCategory.LoadDataCategory[Index], Mode=TwoWay}

慕森卡

正如楼上所说没有赋值,PresentLoadCategory.IsVisable
打开App,查看更多内容
随时随地看视频慕课网APP