I need to create two UserControls, one is inside the other. Each has a DataGrid that displays information from different database tables.
Problem: I was able to configure everything for an external UserControl, but the internal one either automatically intercepts the parent's DataContext (and displays incorrect data in the DataGrid), or (when I try to set the DataContext directly) does not display anything at all, although, judging by the code, getting information from DB passes normally.
External UserControl:
<UserControl x:Class="AppM.Views.ListViews.UserListView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:viewModels="clr-namespace:AppM.ViewModels" xmlns:service="clr-namespace:AppM.Base" xmlns:commandBehaviors="clr-namespace:AppM.CommandBehaviors" xmlns:local="clr-namespace:AppM.Views.FormViews" xmlns:listViews="clr-namespace:AppM.Views.ListViews" service:DialogService.IsRegisteredView="True" mc:Ignorable="d" d:DataContext="{d:DesignInstance viewModels:UsersViewModel}" d:DesignHeight="300" d:DesignWidth="300"> <UserControl.Resources> <ResourceDictionary> <listViews:RoleValueConverter x:Key="roleValueConverter" /> </ResourceDictionary> </UserControl.Resources> <Grid> <StackPanel Orientation="Vertical"> <StackPanel Orientation="Horizontal"> <Button HorizontalAlignment="Left" Margin="5" MinWidth="120">Обновить</Button> </StackPanel> <Expander Header="Пользователи" Margin="5"> <StackPanel Orientation="Vertical"> <StackPanel Orientation="Horizontal"> <Button>Создать</Button> <Button>Редактировать</Button> <Button>Удалить</Button> </StackPanel> <DataGrid ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding Path=Label}" Header="Логин" Width="0.75*"/> <DataGridTextColumn Binding="{Binding Path=Name}" Header="ФИО" Width="2*"/> <DataGridTextColumn Binding="{Binding Path=DateBirth, StringFormat='{}{0:dd.MM.yyyy}'}" Header="Дата рождения" Width="0.75*"/> <DataGridTextColumn Binding="{Binding Path=USERROLE, Converter={StaticResource roleValueConverter}}" Header="Роль" Width="*"/> </DataGrid.Columns> </DataGrid> </StackPanel> </Expander> <!-- Внутренний UserControl здесь --> <Expander Header="Роли" Margin="5"> <local:UserRoleView HorizontalAlignment="Stretch" DataContext="{Binding viewModels:UserRoleViewModel}"/> <!-- судя по всему, не работает--> </Expander> </StackPanel> </Grid> Internal UserControl:
<UserControl x:Class="AppM.Views.FormViews.UserRoleView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:viewModels="clr-namespace:AppM.ViewModels" xmlns:service="clr-namespace:AppM.Base" xmlns:commandBehaviors="clr-namespace:AppM.CommandBehaviors" xmlns:local="clr-namespace:AppM.Views.FormViews" xmlns:listViews="clr-namespace:AppM.Views.ListViews" service:DialogService.IsRegisteredView="True" mc:Ignorable="d" d:DataContext="{d:DesignInstance viewModels:UserRoleViewModel}" > <!-- Если добавить DataContext="{Binding Source={x:Static viewModels:UserRoleViewModel}}" - не может найти UserRoleViewModel --> <!-- Если добавить DataContext="{Binding viewModels:UserRoleViewModel}" - просто ничего не показывает в DataGrid --> <UserControl.Resources> <ResourceDictionary> </ResourceDictionary> </UserControl.Resources> <Grid> <StackPanel Orientation="Vertical"> <StackPanel Orientation="Horizontal"> <Button>Создать</Button> <Button>Редактировать</Button> <Button>Удалить</Button> </StackPanel> <DataGrid ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding Path=Label}" Header="Наименование" Width="2*"/> <DataGridTextColumn Binding="{Binding Path=AccessLevel}" Header="Доступ" Width="3*"/> </DataGrid.Columns> </DataGrid> </StackPanel> </Grid> Please help.
Question after: maybe it would be more logical to move the DataGrid to separate UserControls, which will both be in the third UserControl?