【问题标题】:How to display tooltip in XAML?如何在 XAML 中显示工具提示?
【发布时间】:2022-01-12 14:25:12
【问题描述】:

我正在使用 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 显示工具提示?

【问题讨论】:

    标签: wpf xaml tooltip


    【解决方案1】:

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

    <TextBox Grid.Row="1" x:Name="folder"
         Text="{Binding Path=FolderPath}"
         ToolTip=""/>
    </TextBox>
    
    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      相关资源
      最近更新 更多