I'm using MVVM framework Caliburn Micro throughout my application with ViewModel first (or so I thought). However, when I had problems with a dialog using TryClose(true)
failing to close it's parent window and stumbled upon this question that perfectly outlined my problem, I'm also getting the "TryClose requires a parent IConductor or a view with a Close method or IsOpen property.":
Caliburn.Micro - ShowDialog() how to close the dialog?
However, I'm not exactly sure how to implement the solution. The answer states:
Remove the cal:Bind.Model and cal:View.Model bindings...
Turns out using these bindings is a View-First approach, which I wasn't aware i was doing. Here's a sample of my offending dialog:
<UserControl ... Height="206" Width="415">
<Grid Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="AUTO" />
</Grid.RowDefinitions>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button x:Name="Okay" Content="Okay" Width="100" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Button x:Name="Cancel" Content="Cancel" Width="100" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
<ContentControl cal:View.Model="{Binding TimeSpanViewModel}"/>
</Grid>
</UserControl>
It's just a wrapper with an okay and cancel button for an already existing ViewModel (who's view is resolved by caliburn, hence me thinking I'm doing ViewModel first). If I remove this cal:View.Model
binding I do indeed regain the ability to close my dialog box, but I loose all the actual content. I'm using the ContentControl to display things all over my application (in ItemsControls, dialog boxes, pop-ups, etc).
My question is, how should I be displaying a ViewModel in a ViewModel first Caliburn?
Edit: I'm displaying the DialogViewModel (which inherits screen) using the WindowManager like so:
[Export(typeof(IWindowManager))]
public class AppWindowManager : MetroWindowManager, IDialogManager
{
AppViewModel Content { get; set; }
public AppWindowManager()
{
}
public override MetroWindow CreateCustomWindow(object view, bool windowIsView)
{
if (windowIsView)
{
return view as MainWindowContainer;
}
MainWindowContainer window = new MainWindowContainer();
//{
window.Content = view;
//};
return window;
}
public override bool? ShowDialog(object rootModel, object context = null, IDictionary<string, object> settings = null)
{
Window window = CreateWindow(rootModel, true, context, settings);
return window.ShowDialog();
}
public object ShowCustomDialog(object rootModel, string title, bool showWindowsOptions = true)
{
dynamic settings = new ExpandoObject();
settings.Title = title;
settings.ShowCloseButton = showWindowsOptions;
settings.ShowMaxRestoreButton = showWindowsOptions;
settings.ShowMinButton = showWindowsOptions;
settings.SizeToContent = SizeToContent.WidthAndHeight;
return ShowDialog(rootModel, null, settings);
}
public ILoadingDialogViewModel CreateLoadingDialogManager()
{
return new LoadingDialogViewModel(this);
}
}
conductor
to display your dialog? What issue do you have withTryClose
? Do you see the same message of the other question? – Il Vic