Windows手机根据复选框选择显示/隐藏文本框

时间:2014-04-13 12:07:25

标签: xaml windows-phone-8 binding windows-phone

我会根据复选框选择显示/隐藏文本框(如果选中,则显示文本框已归档)。 我试过这种方式,但它不起作用(文本框字段总是隐藏):

<CheckBox x:Name="chkpsw" IsChecked="{ Binding PasswordRequired, Mode=TwoWay}" />
<TextBox Text="{Binding Password,Mode=TwoWay}" Visibility="{Binding PasswordRequired, Converter={StaticResource VisibilityConverter}}"/>

PasswordRequired 是bool属性,密码是字符串属性, 的 VisibilityConverter

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    bool visibility = (bool)value;
    return visibility ? Visibility.Visible : Visibility.Collapsed;
}

我的模特:

private bool _passwordRequired;
...
public bool PasswordRequired
{
    get { return _passwordRequired; }
    set
    {
        _passwordRequired = value;
        OnPropertyChanged("PasswordRequired");
    }
}

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string name)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(name));
    }
}

0 个答案:

没有答案