0

我需要显示一些ToolTips 并有一个CheckBox显示和隐藏它。我有数据绑定,但不会更新。bool 值似乎总是正确的,这意味着ToolTip即使CheckBox未选中 也不会消失。

WPF 代码

<Button Name="btnCancel" Content="Cancel" 
        ToolTipService.IsEnabled="{Binding GeneralDisplayTooltipsForPreferencesAndSearchOptions}"
        ToolTip="Discard Changes On All Tabs and Close Dialog"
        Height="25" Width="80" Margin="0,5,2,5"
        VerticalAlignment="Center" Click="btnCancel_Click"/>

C#

public class General : INotifyPropertyChanged, IPreferencesGeneral
{

    private bool generalDisplayTooltipsForPreferencesAndSearchOptions = false;

    [field: NonSerializedAttribute()]
    public event PropertyChangedEventHandler PropertyChanged = delegate
    {

    };

    public bool GeneralDisplayTooltipsForPreferencesAndSearchOptions
    { 
        get { return generalDisplayTooltipsForPreferencesAndSearchOptions; }
        set
        {
            if (!Equals(generalDisplayTooltipsForPreferencesAndSearchOptions, value))
            {
                generalDisplayTooltipsForPreferencesAndSearchOptions = value;
                OnPropertyChanged("GeneralDisplayTooltipsForPreferencesAndSearchOptions");
            }
        }
    }
}
4

2 回答 2

0

如果它不适用于所有应用程序,您应该让视图处理它。做这样的事情:

<CheckBox x:Name="checkBoxTooltip" ></CheckBox>
<Button ToolTip="Cancel" ToolTipService.IsEnabled="{Binding ElementName=checkBoxTooltip, Path=IsChecked}" />
于 2013-08-18T18:27:10.947 回答
0

检查你是否有正确DataContext的 . 如果您有任何绑定错误,另请参阅输出。

此外,如果您想控制ToolTip启用状态CheckBox(并且复选框在同一上下文中),您可以绑定IsEnabledCheckBox IsChecked(使用相对绑定)

此代码适用于我的机器,请参见示例:

<Window x:Class="WpfApplication11.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"
    Width="525"
    Height="350">
<Grid>
    <CheckBox VerticalAlignment="Top"
              IsChecked="{Binding GeneralDisplayTooltipsForPreferencesAndSearchOptions}"
              Content="Check me!" />
    <Button Name="btnCancel"
            Width="80"
            Height="25"
            Margin="0,5,2,5"
            VerticalAlignment="Center"
            Content="Cancel"
            ToolTip="Discard Changes On All Tabs and Close Dialog"
            ToolTipService.IsEnabled="{Binding GeneralDisplayTooltipsForPreferencesAndSearchOptions}" />
</Grid>

和视图模型:

namespace WpfApplication11
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new General();
    }


}

public class General : INotifyPropertyChanged
{

    private bool generalDisplayTooltipsForPreferencesAndSearchOptions = false;

    [field: NonSerializedAttribute()]
    public event PropertyChangedEventHandler PropertyChanged = delegate
    {

    };

    public bool GeneralDisplayTooltipsForPreferencesAndSearchOptions
    {
        get { return generalDisplayTooltipsForPreferencesAndSearchOptions; }
        set
        {
            if (!Equals(generalDisplayTooltipsForPreferencesAndSearchOptions, value))
            {
                generalDisplayTooltipsForPreferencesAndSearchOptions = value;
                OnPropertyChanged("GeneralDisplayTooltipsForPreferencesAndSearchOptions");
            }
        }
    }

    private void OnPropertyChanged(string p)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(p));
    }
}
}
于 2013-08-16T19:54:03.247 回答