Caliburn-Micro / Caliburn.Micro

A small, yet powerful framework, designed for building applications across all XAML platforms. Its strong support for MV* patterns will enable you to build your solution quickly, without the need to sacrifice code quality or testability.
http://caliburnmicro.com/
MIT License
2.8k stars 778 forks source link

Conductor<IScreen>.Collection.OneActive binding error while deactivating TabControl #792

Closed to87al closed 2 years ago

to87al commented 2 years ago

Hello, I'm working on a net6 wpf app. I have been getting e binding error while closing a window containing a tabcontrol with itemsource Conductor<IScreen>.Collection.OneActive. This is the code:

//xaml
<Window
    x:Class="Invent.WPF.Views.SetupView"
    ...>
    <Grid>
        <TabControl x:Name="Items"/>
    </Grid>
</Window>

//viewmodel
public class SetupViewModel : Conductor<IScreen>.Collection.OneActive
{
    public SetupViewModel()
    {
        DisplayName = "Setup";
        Items.Add(IoC.Get<FirstViewModel>());
        Items.Add(IoC.Get<SecondViewModel>());
        Items.Add(IoC.Get<ThirdViewModel>());
        Items.Add(IoC.Get<FourthViewModel>());
    }
}

If I launch the window and then close it without switching tab I get this binding error: System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.TabControl', AncestorLevel='1''. BindingExpression:Path=TabStripPlacement; DataItem=null; target element is 'TabItem' (Name=''); target property is 'NoTarget' (type 'Object') If I switch to an other tab and then close the window I don't get any binding error. Is there any thing I can do to fix this? Thank you!

vb2ae commented 2 years ago

how are you closing window without switching tab?

to87al commented 2 years ago

@vb2ae thanks for your reply. I have a main app window (MainViewModel) and I launch this child window (_windowManager.ShowDialogAsync(IoC.Get<SetupViewModel>())) containing a TabControl with several TabItems as explained above. If I open the window and then close it without doing anything I get the above binding error. Meanwhile, if I open the window and switch to an other tab different from the default active one (FirstViewModel) I don't get any binding error. Returning to your question I just close it normally (window X button). If you haven't noticed a similar behavior before, I can attach a sample project. Thanks!

to87al commented 2 years ago

Hello, @vb2ae sorry for disturbing you. I just made a demo repository for reproducing this error.

Requisites: Windows 11 Visual Studio 2022 wpf .net 6 x64 Caliburn Micro 4.0.197

Steps to reproduce the error: 1-press button 'Show new window with TabControl' 2-simply close the new window.

Thanks for your time!

vb2ae commented 2 years ago

@to87al thanks I will look at this

vb2ae commented 2 years ago

Try changing your tabsview xaml to this. Here is a style that fixes the issue

<Window x:Class="DemoBug.Views.TabsView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Title="TabsWindow" Height="450" Width="800">

Reference https://stackoverflow.com/questions/14419248/cannot-find-source-for-binding
to87al commented 2 years ago

Hello @vb2ae thanks a lot for your reply! At first I thought the problem was coming from CM, given that it customizes some control templates in background but according to your referenced link it's the control's implementation form WPF. Again, Thanks for your time and have a great time!