0

привет есть код

ViewModel

public class StudentViewModel
{
    JournalContext context  = new JournalContext();
    List<Student> Students = new List<Student>();
    public StudentViewModel()
    {
        Students.AddRange(context.Students.Include(g => g.Parent));
    }
 }

View

<TabControl Name="Journal" BorderThickness="0">
            <TabItem Name="Students" DataContext="{StaticResource Student}"
                     BorderThickness="0" 
                     Style="{StaticResource NotBorder}" 
                     Background="Transparent"
                     Height="50"
                     Width="200">
                <TabItem.Header>
                    <Grid>
                        <TextBlock Text="Студенты" 
                                   FontSize="30"
                                   TextAlignment="Center" 
                                   VerticalAlignment="Center">

                        </TextBlock>
                    </Grid>
                </TabItem.Header>
                <TabItem.Content>
                    <Grid>
                        <ListBox ItemsSource="{Binding Students}" BorderThickness="0">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Margin="5">
                                        <TextBlock FontSize="18" Text="{Binding Path=FirstName}" />
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </Grid>
                </TabItem.Content>
            </TabItem>
        </TabControl>

Привязываю DataContext через ресурс

DataContext="{StaticResource Student}"

Ресурс определен в App.xaml

App.xaml

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Style/TabPanel.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <ViewModel:StudentViewModel x:Key="Student"/>
        </ResourceDictionary>

    </Application.Resources>

Так проблема в том что не работает.Подскажите как правильно

1

1 ответ 1

1

Привязка работает только с публичными свойствами, а у вас List<Student> Students приватное поле.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.