如何在XAML中显示工具提示?

时间:2010-10-27 06:07:52

标签: wpf xaml tooltip

我正在使用WPF MVVM编写应用程序。我有一个视图模型,其属性IsFolderSelected如下:

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

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

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

我在XAML中有一个TextBox元素:

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

当属性IsFolderSlected == false时,如何从TextBox强制显示工具提示?

1 个答案:

答案 0 :(得分:6)

为了与MVVM模型保持一致,我认为使用工具提示很难实现。您可以使用弹出窗口并绑定IsOpen属性。

    <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>