Search code examples
wpfdata-bindingprismdelegatecommand

How to bind tooltip to a button for different conditions


How to bind tooltip dynamically for different conditions
we have 2 Projects in the solution v are using PRISM framework GeneralBL contains the business logic and StudentManagementUI contains the Usercontrols ,views and ViewModels

Have StudentStatusUserControl.xaml.cs contains a Telerik RadButton

                 <telerik:RadButton Name="button1" Content="Stauses" Height="24" HorizontalAlignment="Left" VerticalAlignment="Top" Width="112" FontSize="12" Margin="2,2,2,2"
                prism:Click.Command="{Binding ButtonstatusCommand}">

this is enabled for a specific condition & when it is disabled we have to show the mouse hover or tooltip info depending on the condition

In the StudentStatusViewModel.cs

    private bool CanExecuteButtonStatusCommand(object o)
    {
        return SharedLogicBL.CanExecuteButtonStatusCommand(controller,dataService,  _selectedItem);
    }

SharedLogicBL.cs in GeneralBL project

      public static bool CanExecuteUnplannedInspection(BaseController controller, DataService dataService, SDataItem selectedItem)
    {
       if(controller.currentuser.Isallowed())
          {
            if(selectedItem!=null)
               {
                 Orders = dataservice.GetOrders(selectedItem);
                  return !Orders.Any();
                }
            }
            else
               return false;
       }

In the above method check to see if the user has the rights, if not Tooltip on the button "User doesn't have the rights" Let first condition is true , in the Orders.Any() returns false then we should display "the selected student has no orders"

Also have a dependency property in the StudentStatusUserControl.xaml.cs for this StudentStatusUserControlBL in the GeneralBL project


Solution

  • Create a public property in your viewmodel that you can databind the telerik button tooltip text to.

    public string Button1TooltipText
    {
        get { 
             if (!controller.currentuser.Isallowed())
               { return "User doesn't have the rights" }
             else
               { 
                 if (!SharedLogicBL.CanExecuteButtonStatusCommand(controller, dataService, _selectedItem))
                     return "the selected student has no orders";
                 else
                     return "Execute the unplanned inspection";
               }
    
             }
    }
    

    Since this property depends on the currently selected item, you'll need to call NotifyPropertyChanged("Button1TooltipText") when _selectedItem changes.