ButchersBoy / Dragablz

Dragable and tearable tab control for WPF
http://dragablz.net
MIT License
2.2k stars 323 forks source link

ToolTip Not Displaying Binding Field Correctly 'System.Windows.DataTemplate' #216

Open aherrick opened 6 years ago

aherrick commented 6 years ago

I have a basic Style setup like the following. I want to bind to Header property.

When I run the App, this is what I see for the ToolTip: System.Windows.DataTemplate

image

<Style TargetType="{x:Type dragablz:TabablzControl}" BasedOn="{StaticResource {x:Type dragablz:TabablzControl}}">
                <Setter Property="ItemContainerStyle" Value="{StaticResource TrapezoidDragableTabItemStyle}" />
                <Setter Property="AdjacentHeaderItemOffset" Value="-12" />
                <!--<Setter Property="NewItemFactory" Value="{x:Static local:MainWindowViewModel.NewItemFactory}" />-->
                <Setter Property="ShowDefaultAddButton" Value="True" />
                <Setter Property="ShowDefaultCloseButton" Value="True" />
                <Setter Property="ToolTip">
                    <Setter.Value>
                        <DataTemplate DataType="{x:Type local:TabContent}">
                            <TextBlock Text="{Binding Header}" />
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="HeaderItemTemplate">
                    <Setter.Value>
                        <DataTemplate DataType="{x:Type local:TabContent}">
                            <TextBlock Text="{Binding Header}" Width="200" ToolTip="{Binding Header}" />
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate DataType="{x:Type local:TabContent}">
                            <ContentPresenter Margin="4" Content="{Binding Content}" />
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
jeremyVignelles commented 6 years ago

Tooltip expects a string value, not a datatemplate. That's how WPF works. you will need anything else if you want do display more than text

aherrick commented 6 years ago

@jeremyVignelles I do just want to display text... however how can I just include a string value considering I'm using Data Binding?

I've tried something like this but it doesn't work.

<Setter Property="ToolTip" Value="{Binding Header}"/>

jeremyVignelles commented 6 years ago

It should work that way. It depends on what is your DataContext. You should have a message in the output window of Visual studio.

See also : https://www.wpf-tutorial.com/data-binding/debugging/

aherrick commented 6 years ago

It doesn't work that way as it has no context of the DataType:

See how HeaderItemTemplateand ContentTemplate are being set. When I just reference

<Setter Property="ToolTip" Value="{Binding Header}"/> It doesn't have:

<DataTemplate DataType="{x:Type local:TabContent}">

jeremyVignelles commented 6 years ago

Oh, I see where your problem is: You are trying to set a Tooltip on the TabablzControl itself, instead of each tab.

Try to remove this code:

               <Setter Property="ToolTip">
                   <Setter.Value>
                       <DataTemplate DataType="{x:Type local:TabContent}">
                           <TextBlock Text="{Binding Header}" />
                       </DataTemplate>
                   </Setter.Value>
               </Setter>

because <TextBlock Text="{Binding Header}" Width="200" ToolTip="{Binding Header}" /> should be enough. You will need to put your mouse on the text however.

You can also add a container around your text block that will hold the tooltip.

aherrick commented 6 years ago

Thanks for the thoughts but no, having ToolTipon the TextBlockdoesn't display anything.

I'm only able to get the ToolTipto display anything is using the ToolTipProperty.

Any thoughts how I can still use the property?

jeremyVignelles commented 6 years ago

Yeah, but then it would apply to the whole tabs collection, so that's not what you want.

You will want to do your work inside HeaderItemTemplate. try to wrap your text block inside a Grid or a Border with a background color and put your tooltip on that

aherrick commented 6 years ago

I'm trying something like you suggested but it's not working:

<Setter Property="HeaderItemTemplate">
                    <Setter.Value>
                        <DataTemplate DataType="{x:Type local:TabContent}">

                            <Grid Grid.Row="1" ToolTipService.ShowDuration="5000">
                                <TextBlock Text="{Binding Header}" Width="200" />

                                <Grid.ToolTip>
                                    <ToolTip>
                                        <TextBlock Text="{Binding Path=Header}" TextTrimming="CharacterEllipsis" TextWrapping="Wrap" />
                                    </ToolTip>
                                </Grid.ToolTip>
                            </Grid>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
aherrick commented 6 years ago

@jeremyVignelles Any thoughts here? This is the sample project I am basing my testing off. How would you get ToolTip to work here:

https://github.com/ButchersBoy/DragablzSamplez/blob/master/MahAppsWindowApp/App.xaml