Search code examples
wpfxamldatatrigger

How to setup Tooltip based on DataGrid binded object


There is a standard DataGrid where each row of it is an Invoice object.

There is some boolean attribute of Invoice object like AAA so it has impact to the report user is gonna to print.

I would like to change the ToolTip of the PRINT button based on this boolean value.

I assume that it could be done via XAML and code behind.

Method 1.

<Button Content="Print" ToolTip="Not shown" ToolTipOpening="Button_ToolTipOpening"/>

Method 2. And here I need a help...

 <DataGridTemplateColumn >
     <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
               <Button Width="19" Height="15"   Tag="{Binding Inv.Id}" 
                    Background="{DynamicResource ButtonLB}" Click="btnPrintInvoice_Click" Margin="5,0,0,0"    BorderBrush="#FF0565B8" Foreground="{DynamicResource CaptionBrush}" Name="btnPrintInvoice" >
    <Button.Style>
          <Style TargetType="{x:Type Button}">                                                             
     <Setter Property="UIElement.IsEnabled" Value="True" />
 <Style.Triggers>
           <DataTrigger  Binding="{Binding Path=IsAAA, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}, Mode=FindAncestor}}" Value="True">
           <Setter Property="StackPanel.ToolTip"
                    Value="{x:Static p:Resources.PrintInvoice}"
                           TargetName="self" />
          </DataTrigger>
        </Style.Triggers>
       </Style>
    </Button.Style>
  </Button>
 </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>
 </DataGridTemplateColumn>

So if IsAAA = true the tooltip should be "A1" and vise versa "A2".


Solution

  • You may need to write a converter for this, which returns a string value of A1 when input value is true and A2 if input is false.

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if((bool)value == true)
          {
              return "A1";
          }
            else
          {
              return "A2";
          }
        }
    

    Then bind the ToolTip content to the IsAAA property and specify your converter in the binding.