How can I bind tooltip of a control that resides in a datatemplate.
<UserControl.Resources>
<DataTemplate x:Key="PositionAdded">`enter code here`
<materialDesign:PackIcon x:Name="PositionInd" Kind="TimerSandEmpty" ToolTip="{Binding XXXX }" HorizontalAlignment="Center" Foreground="DarkOrange" Width="20" Height="20">
<materialDesign:PackIcon.DataContext>
<local:TestClass />
</materialDesign:PackIcon.DataContext>
</materialDesign:PackIcon>
</DataTemplate>
</UserControl.Resources>
I want to bind the tooltip of above Icon dynamically say from side column values. so the tooltip should display as "4 to 9" for that specific icon
Below is the dataGrid structure:
<DataGrid x:Name="DataGridInvRecord" SelectedItem="{Binding item,Mode=TwoWay}" AutoGenerateColumns="False" IsReadOnly="True" >
<DataGrid.Columns>
<DataGridTextColumn Header="#" Binding="{Binding SLNum}" />
<DataGridTextColumn Header="Matterial" Binding="{Binding FPN}" />
<DataGridTextColumn Header="Category" Binding="{Binding Category}" />
<DataGridTextColumn Header="Product" Binding="{Binding Product}" />
<DataGridTextColumn Header="Base" Binding="{Binding Base}"/>
<DataGridTextColumn Header="Size" Binding="{Binding Size}" />
<DataGridTextColumn Header="Available" Binding="{Binding Available}" />
<DataGridTextColumn Visibility="Visible" Header="MinRange" Binding="{Binding MinRange}" />
<DataGridTextColumn Visibility="Visible" Header="MaxRange" Binding="{Binding MaxRange}" />
<DataGridTemplateColumn Header="Position" CellTemplateSelector="{StaticResource PositionTemplateSelector}" />
</DataGrid.Columns>
</DataGrid>
Suggestion with whole code is most welcome. Thanks!!
CodePudding user response:
ToolTips aren't children of their associated controls in the visual tree and thus don't inherit the DataContext. To bind tooltips you need to bind via an intermediate proxy class, as explained in this post.
CodePudding user response:
The Datatemplate should have the Item as DataContext
<DataTemplate x:Key="PositionAdded">
<materialDesign:PackIcon Kind="TimerSandEmpty" HorizontalAlignment="Center" Foreground="DarkOrange" Width="20" Height="20">
<materialDesign:PackIcon.ToolTip>
<MultiBinding StringFormat="{0}-{1}">
<Binding Path="MinRange"/>
<Binding Path="MaxRange"/>
</MultiBinding>
</materialDesign:PackIcon.ToolTip>
</materialDesign:PackIcon>
</DataTemplate>