输入ISPassword和IsReadonly

发布于 2025-02-06 13:10:43 字数 636 浏览 0 评论 0原文

我想创建一个详细信息,该详细信息显示了不可编辑的信息,其中某些值像密码条目一样被隐藏/掩盖了“ *****”。我希望用户能够切换一个允许他们看到值的按钮。我尝试使用具有与ISPassword具有绑定的输入控件,其中ISreadonly设置为true如下:

<HorizontalStackLayout>
    <Entry Text="{Binding SomeValue}" 
            IsPassword="{Binding ShowValue}" 
            IsReadOnly="True"/>
    <ImageButton Source="visibility.svg"
                    Padding="20,0,0,0"
                    Command="{Binding ToggleValueCommand}"/>
</HorizontalStackLayout>

但是,即使ISPassword为true,iSreadonly设置为true true,即使

这是正确的行为? 如果我无法将isReadonly =“ true”的条目使用,那么拥有标签具有此功能的最佳方法是什么。我应该使用ValueConverter还是行为?

I want to create a DetailsPage that shows non-editable information where some values are hidden/masked like a password entry with "*****". I would like the user to be able to toggle a button that allows them to see the value. I tried using an Entry control that has a binding to IsPassword with IsReadOnly set to true as follows:

<HorizontalStackLayout>
    <Entry Text="{Binding SomeValue}" 
            IsPassword="{Binding ShowValue}" 
            IsReadOnly="True"/>
    <ImageButton Source="visibility.svg"
                    Padding="20,0,0,0"
                    Command="{Binding ToggleValueCommand}"/>
</HorizontalStackLayout>

However, when IsReadOnly is set to true the entry shows the text value even when IsPassword is True

Is this the proper behavior?
If I cannot use an Entry with IsReadOnly="True", then what is the best way to have a Label have this functionality. Should I use a ValueConverter or a Behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世俗缘 2025-02-13 13:10:43

作为解决方法,您可以用输入标签替换,每次单击按钮时更改值。

示例代码

XAML
<HorizontalStackLayout>
    <Label Text="{Binding SomeValue}" />
    <ImageButton Source="visibility.svg" Padding="20,0,0,0" Command="{Binding ToggleValueCommand}"/>
</HorizontalStackLayout>
代码背后
public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private bool showValue;

    private string someValue;
    public string SomeValue
    {
        get
        {
            return someValue;
        }
        set
        {
            someValue = value;
            NotifyPropertyChanged();
        }
    }

    public ICommand ToggleValueCommand { get; set; }

    private string copy;
 
    public ViewModel()
    {
        showValue = true;
        SomeValue = "1234";
        copy = SomeValue;

        ToggleValueCommand = new Command<string>((s) => {

            showValue = !showValue;

            if (showValue)
            {
                SomeValue = copy;
            }
            else
            {
                SomeValue = "";
                for (int i = 0; i < copy.Length; i++)
                {
                    SomeValue += "*";
                }
            }
        });
    }
}

As a workaround , you can replace Entry with a Label , change the value while clicking the button each time .

Sample code

Xaml
<HorizontalStackLayout>
    <Label Text="{Binding SomeValue}" />
    <ImageButton Source="visibility.svg" Padding="20,0,0,0" Command="{Binding ToggleValueCommand}"/>
</HorizontalStackLayout>
code behind
public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private bool showValue;

    private string someValue;
    public string SomeValue
    {
        get
        {
            return someValue;
        }
        set
        {
            someValue = value;
            NotifyPropertyChanged();
        }
    }

    public ICommand ToggleValueCommand { get; set; }

    private string copy;
 
    public ViewModel()
    {
        showValue = true;
        SomeValue = "1234";
        copy = SomeValue;

        ToggleValueCommand = new Command<string>((s) => {

            showValue = !showValue;

            if (showValue)
            {
                SomeValue = copy;
            }
            else
            {
                SomeValue = "";
                for (int i = 0; i < copy.Length; i++)
                {
                    SomeValue += "*";
                }
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文