In C# using MVVM ,Is it possible to change the contents of textbox to any one of the special characters using...
Explanation:
I have checkbox called hide password and two textboxes called password and conform password where I am binding my previously saved password . Now If I select the checkbox,the contents of password textbox and conform password textbox(i.e., the previously changed password which is binded to both the textboxes) should change to * .
For Example: already saved password should change to **********
Is it possible in c# (wpf) using MVVM?
c# wpf mvvm
|
show 1 more comment
Explanation:
I have checkbox called hide password and two textboxes called password and conform password where I am binding my previously saved password . Now If I select the checkbox,the contents of password textbox and conform password textbox(i.e., the previously changed password which is binded to both the textboxes) should change to * .
For Example: already saved password should change to **********
Is it possible in c# (wpf) using MVVM?
c# wpf mvvm
w3schools.com/howto/howto_js_toggle_password.asp
– David784
Jan 19 at 3:37
But I need c# code...@David784
– GJPD
Jan 19 at 3:40
@GJPD that's not how this site works.
– Mark Feldman
Jan 19 at 3:58
I have edited my question up to my knowledge ....Someone help me..
– GJPD
Jan 19 at 4:15
1
Why not usePasswordBox
that's what it's for. You can set and clear the mask and it stores the password in a secure string. This may help
– JSteward
Jan 19 at 4:18
|
show 1 more comment
Explanation:
I have checkbox called hide password and two textboxes called password and conform password where I am binding my previously saved password . Now If I select the checkbox,the contents of password textbox and conform password textbox(i.e., the previously changed password which is binded to both the textboxes) should change to * .
For Example: already saved password should change to **********
Is it possible in c# (wpf) using MVVM?
c# wpf mvvm
Explanation:
I have checkbox called hide password and two textboxes called password and conform password where I am binding my previously saved password . Now If I select the checkbox,the contents of password textbox and conform password textbox(i.e., the previously changed password which is binded to both the textboxes) should change to * .
For Example: already saved password should change to **********
Is it possible in c# (wpf) using MVVM?
c# wpf mvvm
c# wpf mvvm
edited Jan 19 at 4:14
GJPD
asked Jan 19 at 3:25
GJPDGJPD
196
196
w3schools.com/howto/howto_js_toggle_password.asp
– David784
Jan 19 at 3:37
But I need c# code...@David784
– GJPD
Jan 19 at 3:40
@GJPD that's not how this site works.
– Mark Feldman
Jan 19 at 3:58
I have edited my question up to my knowledge ....Someone help me..
– GJPD
Jan 19 at 4:15
1
Why not usePasswordBox
that's what it's for. You can set and clear the mask and it stores the password in a secure string. This may help
– JSteward
Jan 19 at 4:18
|
show 1 more comment
w3schools.com/howto/howto_js_toggle_password.asp
– David784
Jan 19 at 3:37
But I need c# code...@David784
– GJPD
Jan 19 at 3:40
@GJPD that's not how this site works.
– Mark Feldman
Jan 19 at 3:58
I have edited my question up to my knowledge ....Someone help me..
– GJPD
Jan 19 at 4:15
1
Why not usePasswordBox
that's what it's for. You can set and clear the mask and it stores the password in a secure string. This may help
– JSteward
Jan 19 at 4:18
w3schools.com/howto/howto_js_toggle_password.asp
– David784
Jan 19 at 3:37
w3schools.com/howto/howto_js_toggle_password.asp
– David784
Jan 19 at 3:37
But I need c# code...@David784
– GJPD
Jan 19 at 3:40
But I need c# code...@David784
– GJPD
Jan 19 at 3:40
@GJPD that's not how this site works.
– Mark Feldman
Jan 19 at 3:58
@GJPD that's not how this site works.
– Mark Feldman
Jan 19 at 3:58
I have edited my question up to my knowledge ....Someone help me..
– GJPD
Jan 19 at 4:15
I have edited my question up to my knowledge ....Someone help me..
– GJPD
Jan 19 at 4:15
1
1
Why not use
PasswordBox
that's what it's for. You can set and clear the mask and it stores the password in a secure string. This may help– JSteward
Jan 19 at 4:18
Why not use
PasswordBox
that's what it's for. You can set and clear the mask and it stores the password in a secure string. This may help– JSteward
Jan 19 at 4:18
|
show 1 more comment
2 Answers
2
active
oldest
votes
The following solution uses a TextBox
to show the password in plain text and a PasswordBox
to mask the password. The PasswordBox
is on top of the TextBox
, so at first you'd see the password masked. When the "show password" checkbox is checked, the PasswordBox
is hidden, thereby showing the TextBox
beneath (and the password in plain text). Here's the XAML:
<Window.Resources>
<BooleanToVisibilityConverter x:Key="VisibilityConverter" />
</Window.Resources>
<StackPanel>
<Grid>
<TextBox
Text="{Binding Password, UpdateSourceTrigger=PropertyChanged}"
/>
<PasswordBox
x:Name="PasswordBox"
PasswordChanged="OnPasswordChanged"
Visibility="{Binding HidePassword, Converter={StaticResource VisibilityConverter}}"
/>
</Grid>
<CheckBox
Content="Show password"
IsChecked="{Binding ShowPassword}"
/>
</StackPanel>
It doesn't use MVVM for everything (notice the OnPasswordChanged
event handler). This is because the PasswordBox
can't use binding, so the password must be set in the code-behind. But before showing that, here's the view model:
public class ViewModel : ViewModelBase
{
private string _password;
public string Password
{
get => _password;
set => Set(ref _password, value);
}
private bool _showPassword;
public bool ShowPassword
{
get => _showPassword;
set
{
Set(ref _showPassword, value);
RaisePropertyChanged(nameof(HidePassword));
}
}
public bool HidePassword => !ShowPassword;
}
The Set
methods come from the ViewModelBase
parent class, which is part of the MVVM Light Toolkit. The Set
method simply sets the property's backing field and raises the PropertyChanged
event for that property.
Finally, here's the code behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitializeViewModel();
}
public ViewModel ViewModel => DataContext as ViewModel;
private void InitializeViewModel()
{
DataContext = new ViewModel();
ViewModel.PropertyChanged += (sender, args) =>
{
// Update the password box only when it's not visible;
// otherwise, the cursor goes to the beginning on each keystroke
if (!PasswordBox.IsVisible)
{
if (args.PropertyName == nameof(ViewModel.Password))
PasswordBox.Password = ViewModel.Password;
}
};
}
private void OnPasswordChanged(object sender, RoutedEventArgs e)
{
ViewModel.Password = PasswordBox.Password;
}
}
After setting the DataContext
to a new ViewModel
, we listen to changes to the Password
property so that we update it in the PasswordBox
. Notice that we only do this when the PasswordBox
is not visible (otherwise, the cursor is set to the beginning on each keystroke and we end up with the password reversed!)
The event handler simply updates the Password
in the view model whenever it's changed in the PasswordBox
.
The code for the "confirm password" TextBox
and PasswordBox
would be very similar.
you are requested to see the following post as well to understand my requirements completely stackoverflow.com/questions/54264195/… @redcurry
– GJPD
Jan 19 at 5:43
@GJPD Questions should be as self contained as possible. If your question needs to be expanded to fully explain your problem, please edit your question.
– redcurry
Jan 19 at 5:47
okay...is it possible to do what you said without adding anything in code behind? @redcurry
– GJPD
Jan 19 at 5:54
@GJPD Not if you're going to use thePasswordBox
. It doesn't support data-binding for security reasons, so we're forced to use code behind.
– redcurry
Jan 19 at 5:57
k...Thank you for your Info?
– GJPD
Jan 19 at 6:14
add a comment |
you could solve this by means of a Converter that checks whether you should hide or show the actual Text.
By the use of a MultiValueConverter we can pass in both the original text and a bool which specifies whether we should hide or show it. The return value then is either the actual text or the hidden text:
public class PasswordToHiddenCharactersConverter : IMultiValueConverter
{
public object Convert(object values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length != 2)
{
return string.Empty;
}
var passwordText = (string)values[0];
var hidePassword = (bool)values[1];
if (hidePassword)
{
return string.Empty.PadRight(passwordText.Length, '*');
}
return passwordText;
}
public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
{
return new { value };
}
}
This then can be used in the XAML to bind the text of the TextBox and it will display correctly depending on the value of the CheckBox:
<Window.Resources>
<local:PasswordToHiddenCharactersConverter x:Key="PasswordToHiddenCharactersConverter" />
</Window.Resources>
<StackPanel>
<CheckBox x:Name="HidePasswordBox" Content="Hide Password" />
<TextBox >
<TextBox.Text>
<MultiBinding Converter="{StaticResource PasswordToHiddenCharactersConverter}" UpdateSourceTrigger="PropertyChanged">
<Binding Path="Password" />
<Binding ElementName="HidePasswordBox" Path="IsChecked" />
</MultiBinding>
</TextBox.Text>
</TextBox>
</StackPanel>
"Password" is the property name in our ViewModel, for the CheckBox we can directly bind it from within the xaml wihtout having to go via the ViewModel
If I type in a password, then hide it, and then type more of the password, the second part is shown while the first part is hidden.
– redcurry
Jan 21 at 15:52
@redcurry You're right, this is due to the missing "UpdateSourceTrigger" in the Multibinding. I updated the code snippet with this and that should solve that problem
– huserben
Jan 21 at 16:40
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54263820%2fin-c-sharp-using-mvvm-is-it-possible-to-change-the-contents-of-textbox-to-any-o%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The following solution uses a TextBox
to show the password in plain text and a PasswordBox
to mask the password. The PasswordBox
is on top of the TextBox
, so at first you'd see the password masked. When the "show password" checkbox is checked, the PasswordBox
is hidden, thereby showing the TextBox
beneath (and the password in plain text). Here's the XAML:
<Window.Resources>
<BooleanToVisibilityConverter x:Key="VisibilityConverter" />
</Window.Resources>
<StackPanel>
<Grid>
<TextBox
Text="{Binding Password, UpdateSourceTrigger=PropertyChanged}"
/>
<PasswordBox
x:Name="PasswordBox"
PasswordChanged="OnPasswordChanged"
Visibility="{Binding HidePassword, Converter={StaticResource VisibilityConverter}}"
/>
</Grid>
<CheckBox
Content="Show password"
IsChecked="{Binding ShowPassword}"
/>
</StackPanel>
It doesn't use MVVM for everything (notice the OnPasswordChanged
event handler). This is because the PasswordBox
can't use binding, so the password must be set in the code-behind. But before showing that, here's the view model:
public class ViewModel : ViewModelBase
{
private string _password;
public string Password
{
get => _password;
set => Set(ref _password, value);
}
private bool _showPassword;
public bool ShowPassword
{
get => _showPassword;
set
{
Set(ref _showPassword, value);
RaisePropertyChanged(nameof(HidePassword));
}
}
public bool HidePassword => !ShowPassword;
}
The Set
methods come from the ViewModelBase
parent class, which is part of the MVVM Light Toolkit. The Set
method simply sets the property's backing field and raises the PropertyChanged
event for that property.
Finally, here's the code behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitializeViewModel();
}
public ViewModel ViewModel => DataContext as ViewModel;
private void InitializeViewModel()
{
DataContext = new ViewModel();
ViewModel.PropertyChanged += (sender, args) =>
{
// Update the password box only when it's not visible;
// otherwise, the cursor goes to the beginning on each keystroke
if (!PasswordBox.IsVisible)
{
if (args.PropertyName == nameof(ViewModel.Password))
PasswordBox.Password = ViewModel.Password;
}
};
}
private void OnPasswordChanged(object sender, RoutedEventArgs e)
{
ViewModel.Password = PasswordBox.Password;
}
}
After setting the DataContext
to a new ViewModel
, we listen to changes to the Password
property so that we update it in the PasswordBox
. Notice that we only do this when the PasswordBox
is not visible (otherwise, the cursor is set to the beginning on each keystroke and we end up with the password reversed!)
The event handler simply updates the Password
in the view model whenever it's changed in the PasswordBox
.
The code for the "confirm password" TextBox
and PasswordBox
would be very similar.
you are requested to see the following post as well to understand my requirements completely stackoverflow.com/questions/54264195/… @redcurry
– GJPD
Jan 19 at 5:43
@GJPD Questions should be as self contained as possible. If your question needs to be expanded to fully explain your problem, please edit your question.
– redcurry
Jan 19 at 5:47
okay...is it possible to do what you said without adding anything in code behind? @redcurry
– GJPD
Jan 19 at 5:54
@GJPD Not if you're going to use thePasswordBox
. It doesn't support data-binding for security reasons, so we're forced to use code behind.
– redcurry
Jan 19 at 5:57
k...Thank you for your Info?
– GJPD
Jan 19 at 6:14
add a comment |
The following solution uses a TextBox
to show the password in plain text and a PasswordBox
to mask the password. The PasswordBox
is on top of the TextBox
, so at first you'd see the password masked. When the "show password" checkbox is checked, the PasswordBox
is hidden, thereby showing the TextBox
beneath (and the password in plain text). Here's the XAML:
<Window.Resources>
<BooleanToVisibilityConverter x:Key="VisibilityConverter" />
</Window.Resources>
<StackPanel>
<Grid>
<TextBox
Text="{Binding Password, UpdateSourceTrigger=PropertyChanged}"
/>
<PasswordBox
x:Name="PasswordBox"
PasswordChanged="OnPasswordChanged"
Visibility="{Binding HidePassword, Converter={StaticResource VisibilityConverter}}"
/>
</Grid>
<CheckBox
Content="Show password"
IsChecked="{Binding ShowPassword}"
/>
</StackPanel>
It doesn't use MVVM for everything (notice the OnPasswordChanged
event handler). This is because the PasswordBox
can't use binding, so the password must be set in the code-behind. But before showing that, here's the view model:
public class ViewModel : ViewModelBase
{
private string _password;
public string Password
{
get => _password;
set => Set(ref _password, value);
}
private bool _showPassword;
public bool ShowPassword
{
get => _showPassword;
set
{
Set(ref _showPassword, value);
RaisePropertyChanged(nameof(HidePassword));
}
}
public bool HidePassword => !ShowPassword;
}
The Set
methods come from the ViewModelBase
parent class, which is part of the MVVM Light Toolkit. The Set
method simply sets the property's backing field and raises the PropertyChanged
event for that property.
Finally, here's the code behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitializeViewModel();
}
public ViewModel ViewModel => DataContext as ViewModel;
private void InitializeViewModel()
{
DataContext = new ViewModel();
ViewModel.PropertyChanged += (sender, args) =>
{
// Update the password box only when it's not visible;
// otherwise, the cursor goes to the beginning on each keystroke
if (!PasswordBox.IsVisible)
{
if (args.PropertyName == nameof(ViewModel.Password))
PasswordBox.Password = ViewModel.Password;
}
};
}
private void OnPasswordChanged(object sender, RoutedEventArgs e)
{
ViewModel.Password = PasswordBox.Password;
}
}
After setting the DataContext
to a new ViewModel
, we listen to changes to the Password
property so that we update it in the PasswordBox
. Notice that we only do this when the PasswordBox
is not visible (otherwise, the cursor is set to the beginning on each keystroke and we end up with the password reversed!)
The event handler simply updates the Password
in the view model whenever it's changed in the PasswordBox
.
The code for the "confirm password" TextBox
and PasswordBox
would be very similar.
you are requested to see the following post as well to understand my requirements completely stackoverflow.com/questions/54264195/… @redcurry
– GJPD
Jan 19 at 5:43
@GJPD Questions should be as self contained as possible. If your question needs to be expanded to fully explain your problem, please edit your question.
– redcurry
Jan 19 at 5:47
okay...is it possible to do what you said without adding anything in code behind? @redcurry
– GJPD
Jan 19 at 5:54
@GJPD Not if you're going to use thePasswordBox
. It doesn't support data-binding for security reasons, so we're forced to use code behind.
– redcurry
Jan 19 at 5:57
k...Thank you for your Info?
– GJPD
Jan 19 at 6:14
add a comment |
The following solution uses a TextBox
to show the password in plain text and a PasswordBox
to mask the password. The PasswordBox
is on top of the TextBox
, so at first you'd see the password masked. When the "show password" checkbox is checked, the PasswordBox
is hidden, thereby showing the TextBox
beneath (and the password in plain text). Here's the XAML:
<Window.Resources>
<BooleanToVisibilityConverter x:Key="VisibilityConverter" />
</Window.Resources>
<StackPanel>
<Grid>
<TextBox
Text="{Binding Password, UpdateSourceTrigger=PropertyChanged}"
/>
<PasswordBox
x:Name="PasswordBox"
PasswordChanged="OnPasswordChanged"
Visibility="{Binding HidePassword, Converter={StaticResource VisibilityConverter}}"
/>
</Grid>
<CheckBox
Content="Show password"
IsChecked="{Binding ShowPassword}"
/>
</StackPanel>
It doesn't use MVVM for everything (notice the OnPasswordChanged
event handler). This is because the PasswordBox
can't use binding, so the password must be set in the code-behind. But before showing that, here's the view model:
public class ViewModel : ViewModelBase
{
private string _password;
public string Password
{
get => _password;
set => Set(ref _password, value);
}
private bool _showPassword;
public bool ShowPassword
{
get => _showPassword;
set
{
Set(ref _showPassword, value);
RaisePropertyChanged(nameof(HidePassword));
}
}
public bool HidePassword => !ShowPassword;
}
The Set
methods come from the ViewModelBase
parent class, which is part of the MVVM Light Toolkit. The Set
method simply sets the property's backing field and raises the PropertyChanged
event for that property.
Finally, here's the code behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitializeViewModel();
}
public ViewModel ViewModel => DataContext as ViewModel;
private void InitializeViewModel()
{
DataContext = new ViewModel();
ViewModel.PropertyChanged += (sender, args) =>
{
// Update the password box only when it's not visible;
// otherwise, the cursor goes to the beginning on each keystroke
if (!PasswordBox.IsVisible)
{
if (args.PropertyName == nameof(ViewModel.Password))
PasswordBox.Password = ViewModel.Password;
}
};
}
private void OnPasswordChanged(object sender, RoutedEventArgs e)
{
ViewModel.Password = PasswordBox.Password;
}
}
After setting the DataContext
to a new ViewModel
, we listen to changes to the Password
property so that we update it in the PasswordBox
. Notice that we only do this when the PasswordBox
is not visible (otherwise, the cursor is set to the beginning on each keystroke and we end up with the password reversed!)
The event handler simply updates the Password
in the view model whenever it's changed in the PasswordBox
.
The code for the "confirm password" TextBox
and PasswordBox
would be very similar.
The following solution uses a TextBox
to show the password in plain text and a PasswordBox
to mask the password. The PasswordBox
is on top of the TextBox
, so at first you'd see the password masked. When the "show password" checkbox is checked, the PasswordBox
is hidden, thereby showing the TextBox
beneath (and the password in plain text). Here's the XAML:
<Window.Resources>
<BooleanToVisibilityConverter x:Key="VisibilityConverter" />
</Window.Resources>
<StackPanel>
<Grid>
<TextBox
Text="{Binding Password, UpdateSourceTrigger=PropertyChanged}"
/>
<PasswordBox
x:Name="PasswordBox"
PasswordChanged="OnPasswordChanged"
Visibility="{Binding HidePassword, Converter={StaticResource VisibilityConverter}}"
/>
</Grid>
<CheckBox
Content="Show password"
IsChecked="{Binding ShowPassword}"
/>
</StackPanel>
It doesn't use MVVM for everything (notice the OnPasswordChanged
event handler). This is because the PasswordBox
can't use binding, so the password must be set in the code-behind. But before showing that, here's the view model:
public class ViewModel : ViewModelBase
{
private string _password;
public string Password
{
get => _password;
set => Set(ref _password, value);
}
private bool _showPassword;
public bool ShowPassword
{
get => _showPassword;
set
{
Set(ref _showPassword, value);
RaisePropertyChanged(nameof(HidePassword));
}
}
public bool HidePassword => !ShowPassword;
}
The Set
methods come from the ViewModelBase
parent class, which is part of the MVVM Light Toolkit. The Set
method simply sets the property's backing field and raises the PropertyChanged
event for that property.
Finally, here's the code behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitializeViewModel();
}
public ViewModel ViewModel => DataContext as ViewModel;
private void InitializeViewModel()
{
DataContext = new ViewModel();
ViewModel.PropertyChanged += (sender, args) =>
{
// Update the password box only when it's not visible;
// otherwise, the cursor goes to the beginning on each keystroke
if (!PasswordBox.IsVisible)
{
if (args.PropertyName == nameof(ViewModel.Password))
PasswordBox.Password = ViewModel.Password;
}
};
}
private void OnPasswordChanged(object sender, RoutedEventArgs e)
{
ViewModel.Password = PasswordBox.Password;
}
}
After setting the DataContext
to a new ViewModel
, we listen to changes to the Password
property so that we update it in the PasswordBox
. Notice that we only do this when the PasswordBox
is not visible (otherwise, the cursor is set to the beginning on each keystroke and we end up with the password reversed!)
The event handler simply updates the Password
in the view model whenever it's changed in the PasswordBox
.
The code for the "confirm password" TextBox
and PasswordBox
would be very similar.
answered Jan 19 at 5:36
redcurryredcurry
1,001822
1,001822
you are requested to see the following post as well to understand my requirements completely stackoverflow.com/questions/54264195/… @redcurry
– GJPD
Jan 19 at 5:43
@GJPD Questions should be as self contained as possible. If your question needs to be expanded to fully explain your problem, please edit your question.
– redcurry
Jan 19 at 5:47
okay...is it possible to do what you said without adding anything in code behind? @redcurry
– GJPD
Jan 19 at 5:54
@GJPD Not if you're going to use thePasswordBox
. It doesn't support data-binding for security reasons, so we're forced to use code behind.
– redcurry
Jan 19 at 5:57
k...Thank you for your Info?
– GJPD
Jan 19 at 6:14
add a comment |
you are requested to see the following post as well to understand my requirements completely stackoverflow.com/questions/54264195/… @redcurry
– GJPD
Jan 19 at 5:43
@GJPD Questions should be as self contained as possible. If your question needs to be expanded to fully explain your problem, please edit your question.
– redcurry
Jan 19 at 5:47
okay...is it possible to do what you said without adding anything in code behind? @redcurry
– GJPD
Jan 19 at 5:54
@GJPD Not if you're going to use thePasswordBox
. It doesn't support data-binding for security reasons, so we're forced to use code behind.
– redcurry
Jan 19 at 5:57
k...Thank you for your Info?
– GJPD
Jan 19 at 6:14
you are requested to see the following post as well to understand my requirements completely stackoverflow.com/questions/54264195/… @redcurry
– GJPD
Jan 19 at 5:43
you are requested to see the following post as well to understand my requirements completely stackoverflow.com/questions/54264195/… @redcurry
– GJPD
Jan 19 at 5:43
@GJPD Questions should be as self contained as possible. If your question needs to be expanded to fully explain your problem, please edit your question.
– redcurry
Jan 19 at 5:47
@GJPD Questions should be as self contained as possible. If your question needs to be expanded to fully explain your problem, please edit your question.
– redcurry
Jan 19 at 5:47
okay...is it possible to do what you said without adding anything in code behind? @redcurry
– GJPD
Jan 19 at 5:54
okay...is it possible to do what you said without adding anything in code behind? @redcurry
– GJPD
Jan 19 at 5:54
@GJPD Not if you're going to use the
PasswordBox
. It doesn't support data-binding for security reasons, so we're forced to use code behind.– redcurry
Jan 19 at 5:57
@GJPD Not if you're going to use the
PasswordBox
. It doesn't support data-binding for security reasons, so we're forced to use code behind.– redcurry
Jan 19 at 5:57
k...Thank you for your Info?
– GJPD
Jan 19 at 6:14
k...Thank you for your Info?
– GJPD
Jan 19 at 6:14
add a comment |
you could solve this by means of a Converter that checks whether you should hide or show the actual Text.
By the use of a MultiValueConverter we can pass in both the original text and a bool which specifies whether we should hide or show it. The return value then is either the actual text or the hidden text:
public class PasswordToHiddenCharactersConverter : IMultiValueConverter
{
public object Convert(object values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length != 2)
{
return string.Empty;
}
var passwordText = (string)values[0];
var hidePassword = (bool)values[1];
if (hidePassword)
{
return string.Empty.PadRight(passwordText.Length, '*');
}
return passwordText;
}
public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
{
return new { value };
}
}
This then can be used in the XAML to bind the text of the TextBox and it will display correctly depending on the value of the CheckBox:
<Window.Resources>
<local:PasswordToHiddenCharactersConverter x:Key="PasswordToHiddenCharactersConverter" />
</Window.Resources>
<StackPanel>
<CheckBox x:Name="HidePasswordBox" Content="Hide Password" />
<TextBox >
<TextBox.Text>
<MultiBinding Converter="{StaticResource PasswordToHiddenCharactersConverter}" UpdateSourceTrigger="PropertyChanged">
<Binding Path="Password" />
<Binding ElementName="HidePasswordBox" Path="IsChecked" />
</MultiBinding>
</TextBox.Text>
</TextBox>
</StackPanel>
"Password" is the property name in our ViewModel, for the CheckBox we can directly bind it from within the xaml wihtout having to go via the ViewModel
If I type in a password, then hide it, and then type more of the password, the second part is shown while the first part is hidden.
– redcurry
Jan 21 at 15:52
@redcurry You're right, this is due to the missing "UpdateSourceTrigger" in the Multibinding. I updated the code snippet with this and that should solve that problem
– huserben
Jan 21 at 16:40
add a comment |
you could solve this by means of a Converter that checks whether you should hide or show the actual Text.
By the use of a MultiValueConverter we can pass in both the original text and a bool which specifies whether we should hide or show it. The return value then is either the actual text or the hidden text:
public class PasswordToHiddenCharactersConverter : IMultiValueConverter
{
public object Convert(object values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length != 2)
{
return string.Empty;
}
var passwordText = (string)values[0];
var hidePassword = (bool)values[1];
if (hidePassword)
{
return string.Empty.PadRight(passwordText.Length, '*');
}
return passwordText;
}
public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
{
return new { value };
}
}
This then can be used in the XAML to bind the text of the TextBox and it will display correctly depending on the value of the CheckBox:
<Window.Resources>
<local:PasswordToHiddenCharactersConverter x:Key="PasswordToHiddenCharactersConverter" />
</Window.Resources>
<StackPanel>
<CheckBox x:Name="HidePasswordBox" Content="Hide Password" />
<TextBox >
<TextBox.Text>
<MultiBinding Converter="{StaticResource PasswordToHiddenCharactersConverter}" UpdateSourceTrigger="PropertyChanged">
<Binding Path="Password" />
<Binding ElementName="HidePasswordBox" Path="IsChecked" />
</MultiBinding>
</TextBox.Text>
</TextBox>
</StackPanel>
"Password" is the property name in our ViewModel, for the CheckBox we can directly bind it from within the xaml wihtout having to go via the ViewModel
If I type in a password, then hide it, and then type more of the password, the second part is shown while the first part is hidden.
– redcurry
Jan 21 at 15:52
@redcurry You're right, this is due to the missing "UpdateSourceTrigger" in the Multibinding. I updated the code snippet with this and that should solve that problem
– huserben
Jan 21 at 16:40
add a comment |
you could solve this by means of a Converter that checks whether you should hide or show the actual Text.
By the use of a MultiValueConverter we can pass in both the original text and a bool which specifies whether we should hide or show it. The return value then is either the actual text or the hidden text:
public class PasswordToHiddenCharactersConverter : IMultiValueConverter
{
public object Convert(object values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length != 2)
{
return string.Empty;
}
var passwordText = (string)values[0];
var hidePassword = (bool)values[1];
if (hidePassword)
{
return string.Empty.PadRight(passwordText.Length, '*');
}
return passwordText;
}
public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
{
return new { value };
}
}
This then can be used in the XAML to bind the text of the TextBox and it will display correctly depending on the value of the CheckBox:
<Window.Resources>
<local:PasswordToHiddenCharactersConverter x:Key="PasswordToHiddenCharactersConverter" />
</Window.Resources>
<StackPanel>
<CheckBox x:Name="HidePasswordBox" Content="Hide Password" />
<TextBox >
<TextBox.Text>
<MultiBinding Converter="{StaticResource PasswordToHiddenCharactersConverter}" UpdateSourceTrigger="PropertyChanged">
<Binding Path="Password" />
<Binding ElementName="HidePasswordBox" Path="IsChecked" />
</MultiBinding>
</TextBox.Text>
</TextBox>
</StackPanel>
"Password" is the property name in our ViewModel, for the CheckBox we can directly bind it from within the xaml wihtout having to go via the ViewModel
you could solve this by means of a Converter that checks whether you should hide or show the actual Text.
By the use of a MultiValueConverter we can pass in both the original text and a bool which specifies whether we should hide or show it. The return value then is either the actual text or the hidden text:
public class PasswordToHiddenCharactersConverter : IMultiValueConverter
{
public object Convert(object values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length != 2)
{
return string.Empty;
}
var passwordText = (string)values[0];
var hidePassword = (bool)values[1];
if (hidePassword)
{
return string.Empty.PadRight(passwordText.Length, '*');
}
return passwordText;
}
public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
{
return new { value };
}
}
This then can be used in the XAML to bind the text of the TextBox and it will display correctly depending on the value of the CheckBox:
<Window.Resources>
<local:PasswordToHiddenCharactersConverter x:Key="PasswordToHiddenCharactersConverter" />
</Window.Resources>
<StackPanel>
<CheckBox x:Name="HidePasswordBox" Content="Hide Password" />
<TextBox >
<TextBox.Text>
<MultiBinding Converter="{StaticResource PasswordToHiddenCharactersConverter}" UpdateSourceTrigger="PropertyChanged">
<Binding Path="Password" />
<Binding ElementName="HidePasswordBox" Path="IsChecked" />
</MultiBinding>
</TextBox.Text>
</TextBox>
</StackPanel>
"Password" is the property name in our ViewModel, for the CheckBox we can directly bind it from within the xaml wihtout having to go via the ViewModel
edited Jan 21 at 16:39
answered Jan 19 at 8:20
huserbenhuserben
5321513
5321513
If I type in a password, then hide it, and then type more of the password, the second part is shown while the first part is hidden.
– redcurry
Jan 21 at 15:52
@redcurry You're right, this is due to the missing "UpdateSourceTrigger" in the Multibinding. I updated the code snippet with this and that should solve that problem
– huserben
Jan 21 at 16:40
add a comment |
If I type in a password, then hide it, and then type more of the password, the second part is shown while the first part is hidden.
– redcurry
Jan 21 at 15:52
@redcurry You're right, this is due to the missing "UpdateSourceTrigger" in the Multibinding. I updated the code snippet with this and that should solve that problem
– huserben
Jan 21 at 16:40
If I type in a password, then hide it, and then type more of the password, the second part is shown while the first part is hidden.
– redcurry
Jan 21 at 15:52
If I type in a password, then hide it, and then type more of the password, the second part is shown while the first part is hidden.
– redcurry
Jan 21 at 15:52
@redcurry You're right, this is due to the missing "UpdateSourceTrigger" in the Multibinding. I updated the code snippet with this and that should solve that problem
– huserben
Jan 21 at 16:40
@redcurry You're right, this is due to the missing "UpdateSourceTrigger" in the Multibinding. I updated the code snippet with this and that should solve that problem
– huserben
Jan 21 at 16:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54263820%2fin-c-sharp-using-mvvm-is-it-possible-to-change-the-contents-of-textbox-to-any-o%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
w3schools.com/howto/howto_js_toggle_password.asp
– David784
Jan 19 at 3:37
But I need c# code...@David784
– GJPD
Jan 19 at 3:40
@GJPD that's not how this site works.
– Mark Feldman
Jan 19 at 3:58
I have edited my question up to my knowledge ....Someone help me..
– GJPD
Jan 19 at 4:15
1
Why not use
PasswordBox
that's what it's for. You can set and clear the mask and it stores the password in a secure string. This may help– JSteward
Jan 19 at 4:18