WPF数据绑定工具提示不会更新

时间:2013-08-16 19:01:19

标签: wpf data-binding tooltip

我需要显示一些ToolTip并使用CheckBox来显示并隐藏它。我有数据绑定但不会更新。 bool值似乎始终为true,这意味着即使取消选中ToolTipCheckBox也不会消失。

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");
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

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

此外,如果您想从ToolTip控制CheckBox启用状态(并且复选框位于相同的上下文中),您可以将IsEnabled绑定到CheckBox { {1}}(具有相对约束力)

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

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>

答案 1 :(得分:0)

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

<CheckBox x:Name="checkBoxTooltip" ></CheckBox>
<Button ToolTip="Cancel" ToolTipService.IsEnabled="{Binding ElementName=checkBoxTooltip, Path=IsChecked}" />
相关问题