DataTemplate for floating ToolWindow is not being picked up

Docking/MDI for WPF Forum

Posted 7 months ago by Matthew Bristow
Version: 17.2.0655
Platform: .NET 4.6
Environment: Windows 10 (64-bit)
Avatar

Afternoon Actipro,

I just noticed this bug when I was looking to replicate another bug outside of our application.

If you add a new ToolWindow where it is set to float when you are using a DataTemplate to display the view based on a ViewModel type it won't pick it up. Although it will work if docked initially. However it does work by using a TemplateSelector like in your MVVM ToolWindow example.

I managed to replicate this using your MVVM ToolWindows example.

I modified the MainView.xaml to not use the TemplateSelector and use DataTemplates, and then in the MainViewModel I just created an instance of a new ToolWindow but set it to float.

    <UserControl.Resources>
	..

    <DataTemplate DataType="{x:Type sample:ToolItem1ViewModel}">
            <sample:ToolItem1View />
        </DataTemplate>

        <DataTemplate DataType="{x:Type sample:ToolItem2ViewModel}">
            <sample:ToolItem2View />
        </DataTemplate>

        <DataTemplate DataType="{x:Type sample:ToolItem3ViewModel}">
            <sample:ToolItem3View />
        </DataTemplate>

        <!--<sample:ToolItemTemplateSelector x:Key="ToolItemTemplateSelector">
            <sample:ToolItemTemplateSelector.ToolItem1Template>
                <DataTemplate>
                    <sample:ToolItem1View />
                </DataTemplate>
            </sample:ToolItemTemplateSelector.ToolItem1Template>
            <sample:ToolItemTemplateSelector.ToolItem2Template>
                <DataTemplate>
                    <sample:ToolItem2View />
                </DataTemplate>
            </sample:ToolItemTemplateSelector.ToolItem2Template>
            <sample:ToolItemTemplateSelector.ToolItem3Template>
                <DataTemplate>
                    <sample:ToolItem3View />
                </DataTemplate>
            </sample:ToolItemTemplateSelector.ToolItem3Template>
        </sample:ToolItemTemplateSelector>-->
    </UserControl.Resources>
		public MainViewModel() {
		    ...
		    toolItems.Add(new ToolItem1ViewModel() { IsFloating = true, SerializationId = "floating"});
		    ...
		}

Comments (3)

Posted 7 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Matthew,

This could possibly be a scope issue.  Keep in mind that when you define the DataTemplate in the main Window's Resources or at a level lower than that, only docking windows within that hierarchy can ever pick it up.  Floating docking windows are hosted in a separate WPF Window behind the scenes and thus won't inherit those resources.  If you define them in the Application.Resources instead, they should get applied to all Windows in your ap.


Actipro Software Support

Posted 7 months ago by Matthew Bristow
Avatar

Cheers for that. It does work if you put it in the Application.Resources will make a note of that my end.

Now on to the issue I have, bit of a weird one.

Essentially a TabControl within my ToolWindow changes its selection the second time of getting focus.

This can be replication by modiying your MVVM ToolWindows example.

I modified ToolItem1ViewModel to have a TabControlSelectedIndex property and the ToolItem1View.xaml to have a TabControl which is bound to my previously added property. When I select that ToolWindow for a second time I can see that the tab selection has gone from All to High (0 to 1).

	public class ToolItem1ViewModel : ToolItemViewModel {
        ...
        ...
        private int tabControlSelectedIndex;

        public int TabControlSelectedIndex
	{
	        get { return tabControlSelectedIndex; }

	        set
	        {
	            if (this.tabControlSelectedIndex == value)
	            {
	                return;
	            }

	            tabControlSelectedIndex = value;
	            this.NotifyPropertyChanged("TabControlSelectedIndex");
	        }
	}
    }
}

 

...
     <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <TabControl SelectedIndex="{Binding TabControlSelectedIndex}"
                    Height="23"
                    Grid.Column="0">
            <TabItem Header="All" />
            <TabItem Header="High" />
            <TabItem Header="Medium" />
            <TabItem Header="Low" />
            <TabItem Header="Masked" />
        </TabControl>
    </Grid>
</UserControl>
Posted 7 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Matthew,

Thanks for the sample.  After debugging, it appears our logic that tries to focus a focusable child control in the tool window is what triggers this.  We basically recurse down the visual tree and look for a focusable descendant visual of the tool window.  But for whatever reason, the WPF TabControl's TabPanel is listing the "High" tab first in this scenario.  So we find that before the "All" tab and thus end up focusing the "High" tab, thereby selecting it.

I'm not sure why the WPF TabControl is returning them in that order.  But one workaround would be to add focusable content within each tab.  Any element should work, as long as it is visible and has Focusable set to true.  Then it should focus that content instead of ever falling back to looking at the tabs.


Actipro Software Support

The latest build of this product (v2018.1 build 0675) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.