How to display tooltip in XAML?

I am writing an application using WPF MVVM. I have a view model with the IsFolderSelected property as follows:

public class SelectFolderViewModel : ViewModelBase
{        
    public bool IsFolderSelected
    {
        get
        {
            return _isFolderSelected;
        }

        set
        {
            if (_isFolderSelected == value)
            {
                return;
            }

            _isFolderSelected = value;
            RaisePropertyChanged(IsFolderSelectedPropertyName);
        }
    }
 }

And I have a TextBox element in XAML:

        <TextBox 
             Text="{Binding Path=FolderPath}"
             ToolTip="Please select folder"/>

How can I get a tooltip from a TextBox when the IsFolderSlected == false property?

+3
source share
1 answer

To save your MVVM model, I think it will be difficult to achieve with a tooltip. You can use the popup and bind the IsOpen property.

    <TextBox Grid.Row="1" x:Name="folder"
         Text="{Binding Path=FolderPath}"
         ToolTip=""/>
    </StackPanel>

    <Popup PlacementTarget="{Binding ElementName=folder}" IsOpen="{Binding IsFolderSelected, Mode=TwoWay}">
        <Border Margin="1">
        <TextBlock Background="White" Foreground="Black" Text="Please select folder"></TextBlock>
        </Border>
    </Popup>
+5
source

Source: https://habr.com/ru/post/1771639/


All Articles