Hi Guys,
Previously, I have this written and the view runs just like I imagined, taking up the whole space of the window:
<Window.DataContext>
<vm:ViewModel />
</Window.DataContext>
<view:View/>
Now I changed it to this and the view autosizes itself to an empty DataGrid in the view and only occupies enough space to have only the header of the DataGrid
<Window.DataContext>
<vm:MainWindowViewModel/>
</Window.DataContext>
<Window.Resources>
<DataType DataType="{x:type vm:ViewModel}">
<view:View/>
</DataType>
</Window.Resources>
<ItemsControl ItemsSource="{Binding ViewModels}" />
From what I can see, the ItemsControl is still at the same size of all the available size of the window. What is making the ItemsControl approach different from directly summoning the View?
EDIT:
This is how the View goes
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1.5*"/>
<ColumnDefinition Width="1.5*"/>
<ColumnDefinition Width="7*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="22*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Command="{Binding NewWindowCommand}">
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="50" Text="+" Margin="0,0,0,0"/>
<TextBlock Text="New Window" Margin="15,22,0,0"/>
</StackPanel>
</Button>
<ListBox Grid.Row="1" ItemsSource="{Binding List}" SelectedValue="{Binding SelectedItem}" SelectedIndex="-1"/>
<CheckBox Grid.Row="2" Content="Filter list items"/>
</Grid>
<StackPanel Grid.Column="1">
</StackPanel>
<Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="60" />
<RowDefinition Height="22*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0"/>
<DataGrid Grid.Row="1">
<DataGrid.Columns>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Column 1" Width="150"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Column 2" Width="200"/>
</DataGrid.Columns>
</DataGrid>
<DockPanel Grid.Row="2" removed="#FF5A8EFF" LastChildFill="False">
<Button Content="Save" Width="150" DockPanel.Dock="Right"/>
</DockPanel>
</Grid>
</Grid>
And here is the main window view model:
class MainWindowVM :ViewModelBase
{
ObservableCollection<ViewModelBase> viewmodels;
public MainWindowVM()
{
ViewModel VM = new ViewModel ();
ViewModels.Add(VM);
}
public ObservableCollection<ViewModelBase> ViewModels
{
get
{
if (viewmodels == null)
{
viewmodels = new ObservableCollection<ViewModelBase>();
}
return viewmodels;
}
}
}
and the view model base class
protected ViewModelBase()
{
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string PropName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if(handler != null)
{
PropertyChangedEventArgs e = new PropertyChangedEventArgs(PropName);
handler(this, e);
}
}
The view uses the ViewModel class. Basically:
there view is called View
the view models are called MainWindowViewModel and ViewModel the view models derive from ViewModelBase
ViewModel is the View's view model
View is put into ItemsControl in the MainWindow