【问题标题】:WPF databinding ToolTips won't get updatedWPF 数据绑定工具提示不会更新
【发布时间】:2013-08-16 19:01:19
【问题描述】:

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

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

【问题讨论】:

  • 为什么将事件设置为委托?不应该只是宣布就足够了吗?另外,请包含绑定到GeneralDisplayTooltipsForPreferencesAndSearchOptionsCheckBox 的代码。
  • @Andy:有些人这样做是为了在引发事件之前不必检查null

标签: wpf data-binding tooltip


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    另外,如果您想从CheckBox 控制ToolTip 启用状态(并且复选框在同一上下文中),您可以将IsEnabled 绑定到CheckBox 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));
        }
    }
    }
    

    【讨论】:

    • 复选框也有数据绑定。它仍然无法正常工作。现在不知何故,工具提示消失了,但即使我选中复选框,它也不会重新出现。
    猜你喜欢
    • 2010-10-22
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    相关资源
    最近更新 更多