获取检查事件数据绑定wpf

时间:2013-09-18 08:13:30

标签: c# wpf data-binding

我在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

4 个答案:

答案 0 :(得分: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 visibilty绑定到ShowPassword属性。因为它的默认值是false,并且它们从那里设置了初始可见性,这应该可以解决你的问题

答案 1 :(得分:2)

在seeting datacontext

之后将IsPasswordVisible设置为false
this.DataContext = this;
IsPasswordVisible = false;

从IsPasswordVisible setter中删除以下代码

if (_IsPasswordVisible == value)
                return;

答案 2 :(得分:1)

这只是因为IsChecked的切换按钮默认为空。

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

答案 3 :(得分:1)

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