【问题标题】:get checked event data binding wpf获取检查事件数据绑定 wpf
【发布时间】:2013-09-18 17:00:56
【问题描述】:

我在 wpf 中创建了一个文本框、一个密码框和一个切换按钮。我需要,当我检查按钮时,密码是可见的,当按钮未选中时,字符显示为项目符号。我的问题是,当我运行应用程序时,该按钮未选中(这意味着隐藏密码),但密码仍然显示。就在我检查按钮之后,一切都按我想要的方式工作。如何从头开始绑定?不仅在我检查之后

XML 代码:

   <ToggleButton  Name="toggle1" Height="40" Padding="0" Width="56" Canvas.Left="131" Canvas.Top="0" BorderBrush="{x:Null}" IsChecked="{Binding ShowPassword}">

代码:

public bool IsPasswordVisible
    {
        get { return _IsPasswordVisible; }
        set
        {
            if (_IsPasswordVisible == value)
                return;
            _IsPasswordVisible = value;
            if(IsPasswordVisible)
            {
                passwordBox1.Visibility = System.Windows.Visibility.Collapsed;
                textbox1.Visibility = System.Windows.Visibility.Visible;
                textbox1.EditValue = passwordBoxEdit1.Password;
            }else{
                passwordBox1.Visibility = System.Windows.Visibility.Visible;
                textbox1.Visibility = System.Windows.Visibility.Collapsed;
                passwordBox1.Password = textEdit1.Text;
            }
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("ShowPassword"));
        }
    }       

    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = this;

    }

    public event PropertyChangedEventHandler PropertyChanged;

    #region OnPropertyChanged
    /// <summary>
    /// Triggers the PropertyChanged event.
    /// </summary>
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion

【问题讨论】:

    标签: c# wpf data-binding


    【解决方案1】:

    查看数据上下文后将 IsPasswordVisible 设置为 false

    this.DataContext = this;
    IsPasswordVisible = false;
    

    从 IsPasswordVisible 设置器中删除以下代码

    if (_IsPasswordVisible == value)
                    return;
    

    【讨论】:

      【解决方案2】:

      您可以通过绑定和转换器使用 xaml 来完成大部分工作。

      例子:

      <Window x:Class="WpfApplication1.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:local="clr-namespace:WpfApplication1"
          Title="MainWindow" Height="350" Width="525">
      <Window.Resources>
          <BoolToVisibilyConverter x:Key="boolVisibilityConverter"/>
          <BoolToVisibilyOppositeConverter x:Key="boolVisibilityOppsiteConverter"/>
      </Window.Resources>
      <Grid x:Name="myGrid" Margin="5" Width="500">
          <StackPanel>
              <TextBox Visibility="{Binding ShowPassword, Converter={StaticResource boolVisibilityOppsiteConverter}}"/>
              <PasswordBox Visibility="{Binding ShowPassword, Converter={StaticResource boolVisibilityConverter}}"/>
          </StackPanel>
      </Grid>
      

      Converters:

      class BoolToVisibilityConverter : IValueConverter
      {
          #region IValueConverter Members
      
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              return bool.Parse(value.ToString()) == true ? Visibility.Visible : Visibility.Collapsed; 
          }
      
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              throw new NotImplementedException();
          }
      
          #endregion
      }
      
      class BoolToVisibilityOppositeConverter : IValueConverter
      {
          #region IValueConverter Members
      
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              return bool.Parse(value.ToString()) == false ? Visibility.Visible : Visibility.Collapsed; 
          }
      
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              throw new NotImplementedException();
          }
      
          #endregion
      }
      

      那么,切换按钮应该只绑定到ShowPassword 属性。

      设置 ShowPassword 时,将文本从文本框复制到密码框,反之亦然。

      public bool ShowPassword
      {
          set
          {  
             if (_IsPasswordVisible == value)
                  return;
              _IsPasswordVisible = value;
      
             if (_IsPasswordVisible = true)
                textbox1.EditValue = passwordBoxEdit1.Password;
             else
                passwordBox1.Password = textEdit1.Text;
      
             if (PropertyChanged != null)
                 PropertyChanged(this, new PropertyChangedEventArgs("ShowPassword"));
          }
      

      这样,TextBoxPasswordBox 可见性绑定到 ShowPassword 属性。因为它的默认值为 false 并且他们从那里设置了初始可见性,所以这应该可以解决您的问题

      【讨论】:

      • 但只是切换按钮被绑定..我会尝试你的解决方案,我希望它有效。谢谢!
      • @alexAlex 你是对的,你的切换按钮绑定正确。无需更改。
      【解决方案3】:

      这只是因为在切换按钮中 IsChecked 的默认值为 null。

      public Nullable<bool> IsChecked { get; set; }
      

      【讨论】:

      • 但他将该值绑定到他的财产。你能详细说明你的答案吗? OP 应该如何解决这个问题?
      • 所以需要确保“ShowPassword”依赖属性必须为空。 link
      【解决方案4】:

      您可以尝试将密码框设置为默认折叠吗?其他一切都应该正常工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-06
        • 2011-01-13
        • 1970-01-01
        • 2016-05-09
        • 1970-01-01
        • 2015-05-30
        • 2016-06-30
        • 1970-01-01
        相关资源
        最近更新 更多