Вопрос: Как отобразить данные в IndexesView?
IndexesView отображается в MainWindow
Как отобразить IndexesView в ManagerIndexesView?
Т.е. как и где правильно прописать биндинг к IndexesView?
https://github.com/jhon65496/DataDisplayWpfApp1
При отладке получаю сообщение в VS:
System.Windows.Data Error: 40 : BindingExpression path error: 'IndexesViewModel2' property not found on 'object' ''ManagerIndexesViewModel' (HashCode=22385437)'.
BindingExpression:Path=IndexesViewModel2;
DataItem='ManagerIndexesViewModel' (HashCode=22385437);
target element is 'ContentControl' (Name='');
target property is 'Content' (type 'Object')
App.xaml.cs
public partial class App : Application
{
public App()
{
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// AppManager appManager = new AppManager();
MainWindowViewModel mainWindowViewModel = new MainWindowViewModel();
MainWindow mainWindow = new MainWindow();
mainWindow.DataContext = mainWindowViewModel;
mainWindow.Show();
}
}
App.xaml
<Application x:Class="DataDisplayWpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DataDisplayWpfApp1">
<Application.Resources>
</Application.Resources>
</Application>
=== === === === === === === === === === === === === === === ===
IndexCalculation
public class IndexCalculation
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
MainMenuItemMainWindow
internal class MainMenuItemMainWindow
{
public int Id { get; set; }
public string Name { get; set; }
public string Alias { get; set; }
}
=== === === === === === === === === === === === === === === ===
DataContextApp
public class DataContextApp
{
public DataContextApp()
{
GenerateDataСalculationIndex();
}
private ObservableCollection<IndexCalculation> calculationIndexes;
public ObservableCollection<IndexCalculation> СalculationIndexes
{
get { return calculationIndexes; }
set { calculationIndexes = value; }
}
public void GenerateDataСalculationIndex()
{
var calculationIndexes = new ObservableCollection<IndexCalculation>();
for (int i = 1; i < 11; i++)
{
var indexCalculation = new IndexCalculation()
{
Id = i,
Name = $"NameIndex-{i}",
Description = $"DescriptionIndex-{i}"
};
calculationIndexes.Add(indexCalculation);
}
СalculationIndexes = calculationIndexes;
}
}
=== === === === === === === === === === === === === === === ===
MainWindowViewModel
internal class MainWindowViewModel : BaseVM
{
public IndexesViewModel indexesViewModel;
public ManagerIndexesViewModel managerIndexesViewModel;
DataContextApp DataContextApp;
public MainWindowViewModel()
{
// this.appManager = appManager;
DataContextApp = new DataContextApp();
managerIndexesViewModel = new ManagerIndexesViewModel(DataContextApp);
indexesViewModel = new IndexesViewModel(DataContextApp);
LoadItemMainMenu();
}
#region Title
private string title = "App `DataDisplayWpfApp1`. Prop title";
public string Title
{
get { return title; }
set { title = value; }
}
#endregion
#region mainMenuItems
private ObservableCollection<MainMenuItemMainWindow> mainMenuItems;
public ObservableCollection<MainMenuItemMainWindow> MainMenuItems
{
get { return mainMenuItems; }
set
{
mainMenuItems = value;
RaisePropertyChanged(nameof(MainMenuItemMainWindow));
}
}
#endregion
#region SelectedItem
private MainMenuItemMainWindow selectedMainMenuItem;
public MainMenuItemMainWindow SelectedMainMenuItem
{
get { return selectedMainMenuItem; }
set
{
selectedMainMenuItem = value;
SwitchView(selectedMainMenuItem.Alias);
RaisePropertyChanged(nameof(CurrentView));
}
}
#endregion
private BaseVM currentView;
public BaseVM CurrentView
{
get { return currentView; }
set
{
currentView = value;
RaisePropertyChanged(nameof(CurrentView));
}
}
private string titleDetail = "TitleDetail";
public string TitleDetail
{
get { return titleDetail; }
set
{
titleDetail = value;
Debug.WriteLine($"titleDetail -- {titleDetail}");
RaisePropertyChanged(nameof(TitleDetail));
}
}
public void LoadItemMainMenu()
{
mainMenuItems = new ObservableCollection<MainMenuItemMainWindow>()
{
new MainMenuItemMainWindow(){ Name = "Управление коэффициентами", Alias ="ManagerIndexes" },
new MainMenuItemMainWindow(){ Name = "Коэффициенты", Alias ="Indexes" },
new MainMenuItemMainWindow(){ Name = "Поставщки", Alias ="Provider" }
};
}
public void SwitchView(string nameView)
{
switch (nameView)
{
case "ManagerIndexes":
CurrentView = managerIndexesViewModel;
break;
case "Indexes":
CurrentView = indexesViewModel;
break;
}
}
}
=== === === === === === === === === === === === === === === ===
ManagerIndexesViewModel
public class ManagerIndexesViewModel : BaseVM
{
IndexesViewModel IndexesViewModel2 { get; }
public DataContextApp DataContextApp;
public ManagerIndexesViewModel(DataContextApp DataContextApp)
{
this.DataContextApp = DataContextApp;
IndexesViewModel2 = new IndexesViewModel(this);
}
private IndexCalculation selectedIndexCalculation;
public IndexCalculation SelectedIndexCalculation
{
get { return selectedIndexCalculation; }
set
{
selectedIndexCalculation = value;
RaisePropertyChanged(nameof(SelectedIndexCalculation));
}
}
}
=== === === === === === === === === === === === === === === ===
IndexesViewModel
public class IndexesViewModel : BaseVM
{
ManagerIndexesViewModel managerIndexesViewModel;
DataContextApp DataContextApp;
public IndexesViewModel(ManagerIndexesViewModel managerIndexesViewModel)
{
this.managerIndexesViewModel = managerIndexesViewModel;
this.DataContextApp = this.managerIndexesViewModel.DataContextApp;
СalculationIndexs = DataContextApp.СalculationIndexes;
}
public IndexesViewModel(DataContextApp DataContextApp)
{
this.DataContextApp = DataContextApp;
СalculationIndexs = DataContextApp.СalculationIndexes;
}
private ObservableCollection<IndexCalculation> calculationIndexs;
public ObservableCollection<IndexCalculation> СalculationIndexs
{
get { return calculationIndexs; }
set
{
calculationIndexs = value;
RaisePropertyChanged(nameof(СalculationIndexs));
}
}
private IndexCalculation selectedIndexCalculation;
public IndexCalculation SelectedIndexCalculation
{
get { return selectedIndexCalculation; }
set
{
selectedIndexCalculation = value;
this.managerIndexesViewModel.SelectedIndexCalculation = selectedIndexCalculation;
RaisePropertyChanged(nameof(SelectedIndexCalculation));
}
}
public void LoadDataTest2()
{
СalculationIndexs = this.DataContextApp.СalculationIndexes;
}
}
=== === === === === === === === === === === === === === === ===
IndexesView
<UserControl x:Class="DataDisplayWpfApp1.Views.Views.IndexesView"
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:vvm ="clr-namespace:DataDisplayWpfApp1.ViewModels.Views"
xmlns:local="clr-namespace:DataDisplayWpfApp1.Views.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<DockPanel>
<UniformGrid DockPanel.Dock="Top" Rows="1" Margin="-3,3">
<TextBlock FontSize ="30">IndexesView</TextBlock>
<!--<Button Content="Load"
Command="{}"
CommandParameter="{}"
Foreground="Blue" Padding="0,5" Margin="3,0"/>
<Button Content="Create"
Command=""
CommandParameter=""
Foreground="Green" Padding="0,5" Margin="3,0"/>-->
</UniformGrid>
<DataGrid Grid.Row="1"
ItemsSource="{Binding СalculationIndexs}"
SelectedItem=""
SelectedIndex="2"
x:Name="MainDataGrid"
AutoGenerateColumns="False"
CanUserAddRows="True"
GridLinesVisibility="Vertical"
VerticalGridLinesBrush="DarkGray"
AlternatingRowBackground="LightGray"
>
<DataGrid.Columns>
<DataGridTextColumn Header="id" Binding="{Binding Id}" Width="Auto"/>
<DataGridTextColumn Header="Имя" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Description" Binding="{Binding Description}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
</DockPanel>
</UserControl>
=== === === === === === === === === === === === === === === ===
ManagerIndexesView
<UserControl x:Class="DataDisplayWpfApp1.Views.Views.ManagerIndexesView"
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:v ="clr-namespace:DataDisplayWpfApp1.Views.Views"
xmlns:vm="clr-namespace:DataDisplayWpfApp1.ViewModels"
xmlns:vvm="clr-namespace:DataDisplayWpfApp1.ViewModels.Views"
xmlns:local="clr-namespace:DataDisplayWpfApp1.Views.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<!--<UserControl.Resources>
<DataTemplate DataType="{x:Type vvm:IndexesViewModel}">
<v:IndexesView DataContext="{Binding}"/>
</DataTemplate>
</UserControl.Resources>-->
<Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="350" MinWidth="200"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<GroupBox Grid.Column="0" Header="Управление индексами">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<GroupBox Grid.Row="0" Header="Индексы">
<v:IndexesView DataContext="{Binding IndexesViewModel2}"/>
<!--<v:IndexesView />-->
<!--<ContentControl Content="{Binding IndexesViewModel2}"/>-->
</GroupBox>
<!-- GridSplitter -->
<GridSplitter Grid.Row="1" HorizontalAlignment="Stretch" Height="3"/>
<!-- ** ========== ** -->
<GroupBox Grid.Row="2" Header="ИндексыПоставщкик">
<!--<ContentControl Content="{Binding IndexesProvidersView}"/>-->
<!--<v:ProvidersAssignedIndexCalculationView DataContext="{Binding ProvidersAssignedIndexViewModel}"/>-->
</GroupBox>
</Grid>
</GroupBox>
<GroupBox Grid.Column="1" Header="Поставщики">
<!--<ContentControl Content="{Binding ProvidersView}"/>-->
<!--<v:ProvidersNotAssignedIndexCalculationView DataContext="{Binding ProvidersNotAssignedIndexViewModel}"/>-->
</GroupBox>
<GridSplitter Grid.Column="0" HorizontalAlignment="Right" Width="3"/>
</Grid>
</Grid>
</UserControl>
=== === === === === === === === === === === === === === === ===
MainWindow
<Window x:Class="DataDisplayWpfApp1.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:DataDisplayWpfApp1"
xmlns:vm ="clr-namespace:DataDisplayWpfApp1.ViewModels"
xmlns:vvm ="clr-namespace:DataDisplayWpfApp1.ViewModels.Views"
xmlns:v ="clr-namespace:DataDisplayWpfApp1.Views.Views"
d:DataContext="{d:DesignInstance Type=vm:MainWindowViewModel, IsDesignTimeCreatable=True}"
mc:Ignorable="d"
Title="{Binding Title}"
Height="800" Width="900">
<Window.Resources>
<DataTemplate DataType="{x:Type vvm:ManagerIndexesViewModel}">
<v:ManagerIndexesView />
</DataTemplate>
<DataTemplate DataType="{x:Type vvm:IndexesViewModel}">
<v:IndexesView />
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" MinWidth="200"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!--Группы-->
<GroupBox Grid.Column="0" Header="Меню">
<DockPanel>
<ListBox ItemsSource="{Binding MainMenuItems}"
SelectedItem="{Binding SelectedMainMenuItem}"
Name="MainMenu"
SelectedIndex="2">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment"
Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel>
<TextBlock Text="{Binding Name}"/>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
</GroupBox>
<GroupBox Header="Панель" Grid.Column="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<TextBlock Text="{Binding TitleDetail}" FontSize="26"/>
<Separator Background="DarkBlue"/>
</StackPanel>
<ContentControl Grid.Row="1" Content="{Binding CurrentView}" />
</Grid>
</GroupBox>
<GridSplitter Grid.Column="0" HorizontalAlignment="Right" Width="3"/>
</Grid>
</Window>

