如何在设置ItemsControl ItemsSource时引用View Model属性

时间:2018-03-15 12:21:52

标签: c# wpf xaml mvvm

我有一个ItemsControl,像这样:

<ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1" Grid.ColumnSpan="4">
                    <ItemsControl Grid.Row="1" Grid.ColumnSpan="3" ItemsSource="{Binding StudentObservableCollection}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Grid Grid.Row="1"  Grid.ColumnSpan="3" Margin="5, 0, 15, 0" Height="120">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="80"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="70"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <CheckBox VerticalAlignment="Center" HorizontalAlignment="Center"/>
                                    <Label Grid.Row="1" Grid.Column="1" 
                                           HorizontalAlignment="Center"
                                           Content="{Binding SubjectString, UpdateSourceTrigger=PropertyChanged}"
                                           FontFamily="Helvetica" FontWeight="Thin" FontSize="16"/>
                                    <Grid Grid.Column="1">
                                        <Border BorderThickness="1" BorderBrush="{Binding StudentMemberColor}">
                                            <Grid>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="40"/>
                                                    <ColumnDefinition Width="80"/>
                                                    <ColumnDefinition Width="*"/>
                                                </Grid.ColumnDefinitions>
                                                <dxe:TextEdit Grid.Column="1" Margin="5, 5, 5, 5" Width="50"  Height="50" HorizontalContentAlignment="Center"
                                                              FontFamily="Helvetica" FontWeight="Thin" FontSize="16"
                                                              Text="{Binding StudentID, UpdateSourceTrigger=PropertyChanged}"/>
                                            </Grid>
                                        </Border>
                                    </Grid>
                                </Grid>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </ScrollViewer>

问题是,标签,这里

<Label Grid.Row="1" Grid.Column="1" 
                                       HorizontalAlignment="Center"
                                       Content="{Binding SubjectString, UpdateSourceTrigger=PropertyChanged}"

我想将它绑定到View Model中的属性。但是,ObservableCollection持有Student对象,而SubjectString不是Student的属性。它是View Model的一个属性。问题是,即使在实现属性更改时在视图模型中正确设置了值,该值也不会在View中显示属性。我是否需要将数据上下文设置为此特定标签?如果是这样,怎么样?我的XAML中的所有其他控件都准确地引用了我的视图模型,除此之外,我假设它是因为我正在创建ItemsControl并将ItemsSource分配给类型为Student的ObservableCollections。

0 个答案:

没有答案