Как отобразить ListBox в режиме разработчика в MenuControl.xaml ?
В MainWindow.xaml отображается.
По материалам: Обратиться из UserControl к MainWindow MVVM Проект: https://github.com/jhon65496/QDisplayListBoxInUC
MainWindow.xaml
<Window x:Class="MasterDetail0202Detail.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MasterDetail0202Detail"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=local:MainWindowViewModel , IsDesignTimeCreatable=True}"
Title="MainWindow" Height="450" Width="800">
<Grid Margin="0,1,0,-1">
<GroupBox Header="Master" Grid.Column="0" Grid.Row="1">
<local:MenuControl DockPanel.Dock="Bottom" ItemsSource="{Binding MenuItems}" SelectedItem="{Binding SelectedMenuItem, Mode=TwoWay}"/>
</GroupBox>
</Grid>
</Window>
MenuControl.xaml
<UserControl x:Class="MasterDetail0202Detail.MenuControl"
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:local="clr-namespace:MasterDetail0202Detail"
Background="White"
mc:Ignorable="d"
x:Name="Control"
d:DataContext="{d:DesignInstance Type=local:MainWindowViewModel , IsDesignTimeCreatable=True}"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<GroupBox Header="Меню" >
<ListBox ItemsSource="{Binding ElementName=Control, Path=ItemsSource}"
SelectedIndex="1"
SelectedItem="{Binding ElementName=Control, Path=SelectedItem}">
<ListBox.ItemTemplate>
<DataTemplate >
<DockPanel>
<TextBlock Text="{Binding Path=Name}" Margin="3"/>
<TextBlock Text="{Binding Path=State}" Margin="3"/>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</GroupBox>
</Grid>
</UserControl>
**MenuControl **
// <summary>
/// Логика взаимодействия для UserControl1.xaml
/// </summary>
public partial class MenuControl : UserControl
{
public MenuControl()
{
InitializeComponent();
}
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
"ItemsSource",
typeof(IEnumerable),
typeof(MenuControl),
new PropertyMetadata(default(IEnumerable)));
public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register(
"SelectedItem",
typeof(object),
typeof(MenuControl),
new PropertyMetadata(default(object)));
public IEnumerable ItemsSource
{
get => (IEnumerable)GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}
public object SelectedItem
{
get => GetValue(SelectedItemProperty);
set => SetValue(SelectedItemProperty, value);
}
}
App.xaml.cs
public partial class App : Application
{
public App()
{
MainWindowViewModel mainViewModel = new MainWindowViewModel();
var mainWin = new MainWindow();
mainWin.DataContext = mainViewModel;
mainWin.WindowStartupLocation = WindowStartupLocation.CenterScreen;
mainWin.Show();
}
}
MainWindowViewModel
internal class MainWindowViewModel : BaseVM
{
public ObservableCollection<MenuItem> MenuItems { get; set; }
public TestDataContext dataContext;
public MainWindowViewModel()
{
dataContext = new TestDataContext();
LoadMenuItems();
}
private BaseVM currentContent;
public BaseVM CurrentContent
{
get => currentContent;
set
{
currentContent = value;
RaisePropertyChanged();
}
}
private MenuItem selectedMenuItem;
public MenuItem SelectedMenuItem
{
get { return selectedMenuItem; }
set
{
selectedMenuItem = value;
RaisePropertyChanged();
Debug.WriteLine("MainWindowViewModel--Prop--SelectedMenuItem");
}
}
/// <summary>
/// Загрузка списка меню
/// </summary>
private async void LoadMenuItems()
{
//Загрузка списка меню
var menuItems = await dataContext.GetMenuItemsAsync();
this.MenuItems = new ObservableCollection<MenuItem>(menuItems);
}
}
BaseVM
class BaseVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
internal class MenuItem
{
public int Id { get; set; }
public int Sort { get; set; }
public string Name { get; set; }
public string NameVm { get; set; }
public string State { get; set; }
public BaseVM ViewModel { get; set; }
}
class TestDataContext
{
// private readonly List<MenuItem> _people;
public ObservableCollection<MenuItem> MenuItems { get; set; }
//ctor
public TestDataContext()
{
MenuItems = new ObservableCollection<MenuItem>()
{
new MenuItem {Name = "Страница 1", NameVm = "FirstPageViewModel", State = "Статус1"},
new MenuItem {Name = "Страница 2", NameVm = "SecondPageViewModel", State = "Статус1"}
};
}
public Task<IEnumerable<MenuItem>> GetMenuItemsAsync()
{
return Task.FromResult(MenuItems.AsEnumerable());
}
}
Update-1
https://github.com/jhon65496/QDisplayListBoxInUC
MainWindow.xaml
<Window x:Class="MasterDetail0202Detail.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MasterDetail0202Detail"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=local:MainWindow, IsDesignTimeCreatable=True}"
Title="MainWindow" Height="450" Width="800">
<Grid Margin="0,1,0,-1">
<GroupBox Header="Master" Grid.Column="0" Grid.Row="1">
<local:MenuControl ItemsSource="{Binding MenuItems}" SelectedItem="{Binding SelectedMenuItem, Mode=TwoWay}"/>
</GroupBox>
</Grid>
</Window>
MenuControl.xaml
<UserControl x:Class="MasterDetail0202Detail.MenuControl"
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:local="clr-namespace:MasterDetail0202Detail"
Background="White"
mc:Ignorable="d"
x:Name="Control"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<GroupBox Header="Меню" >
<ListBox ItemsSource="{Binding ElementName=Control, Path=ItemsSource}"
SelectedIndex="1"
SelectedItem="{Binding ElementName=Control, Path=SelectedItem}">
<ListBox.ItemTemplate>
<DataTemplate >
<DockPanel>
<TextBlock Text="{Binding Path=Name}" Margin="3"/>
<TextBlock Text="{Binding Path=State}" Margin="3"/>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</GroupBox>
</Grid>
</UserControl>
