TextBox to PasswordBox in WPF - wpf

I have to convert TextBox XAML to PasswordBox XAML. The main problem is <TextBox.Text>.
Any clue how it can be done?
Thank you!
<TextBox Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Name="tbxPassword" VerticalAlignment="Top" Width="277">
<TextBox.Text>
<Binding Path="Password" Source="{StaticResource ods}" UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<c:ConfigValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>

If its really about password, you should use PasswordBox, Binding it to a dependency property would cause security vulnerabilities.
<PasswordBox
Name="pwdBox"
MaxLength="64"
PasswordChar="#"
PasswordChanged="PasswordChangedHandler"
/>
You can see the basic usage here
Password Box

Related

WPF button to update a textbox in xaml with validator

How can I get a button to reset text in a textbox to a known value? In my example the validation rule is triggered so I can't use the button command to update the property, which is good because I don't want the property set if it's not valid. I just want the button to basically reset the value in xaml without involving the property. How would I do that?
<TextBox Grid.Column="1" Grid.Row="9" HorizontalAlignment="Left" Height="20" Width="105" Name="Elevation"
Validation.ErrorTemplate="{StaticResource validationTemplate1}" Style="{StaticResource textBoxInError}">
<TextBox.Text >
<Binding Path="Elevation" Mode="TwoWay" UpdateSourceTrigger="LostFocus">
<Binding.ValidationRules>
<rules:NotEmptyValidationRule ValidatesOnTargetUpdated="True" ValidationStep="RawProposedValue"/>
<rules:DoubleValidationRule ValidatesOnTargetUpdated="True" ValidationStep="RawProposedValue"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<Button Grid.Column="2" Grid.Row="9" Height="20" Command="{Binding ClickResetCommand}" CommandParameter="Elevation" >R</Button>
Still looking for a XAML solution but for now I've change the button to use OnClick and in the code behind using the following...
case "Elevation":
Elevation.Text = "50";
Validation.ClearInvalid(Elevation.GetBindingExpression(TextBox.TextProperty) ?? throw new InvalidOperationException());
Elevation.Focus();
break;

why can't I use multibinding with the IsEnabled property of the button?

I am trying to set the IsEnabled property of a button using multibinding, because this property depends on three variables.
If I would set the content propery, I could use this code:
<Button Height="23" HorizontalAlignment="Left" Margin="629,49,0,0" Name="btnMyButton" VerticalAlignment="Top" Width="75">
<Button.Content>
<MultiBinding Converter="{StaticResource myMultiValueConverter}">
<Binding ElementName="MyElement"/>
<Binding />
</MultiBinding>
</Button.Content>
</Button>
I try to use this code:
<Button Height="23" HorizontalAlignment="Left" Margin="629,49,0,0" Name="btnMyButton" VerticalAlignment="Top" Width="75">
<Button.IsEnabled>
<?????
But in this case, although the Button.IsEnabled is avaliable, in the next line I can't find the Multibinding keyword, so I can't use multibinding with the IsEnabled property.
why? Is there any way to set the IsEnabled property with a multivalue converter?
Thanks.
The syntax should be the exact same as what you have for Button.Content - just replace "Content" with "IsEnabled".
<Button.IsEnabled>
<MultiBinding Converter="{StaticResource myMultiValueConverter}">
<Binding ... />
<Binding ... />
<Binding ... />
</MultiBinding>
</Button.IsEnabled>
It might not auto-complete for you because the IsEnabled property expects a boolean value, not a MultiBinding object, but it shouldn't give you any errors, and will compile and execute just fine.
(It auto-completes for Button.Content because the Content property is of type object, which includes a MultiBinding object)

WPF ValidationRules disables PropertyChanged

I have the following textbox that have a propertychanged in the viewmodel.
When I insert the Binding.ValidationRules and I insert some wrong value, it doesn't trigger the propertychanged event and I don't understand why. Any help?
<TextBox Name="RiskCode" HorizontalAlignment="Left" Margin="101.923,8,0,81" TextWrapping="Wrap" Width="56.077" MaxLength="6" Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource textBoxInError}">
<TextBox.Text>
<Binding Path="RiskCode" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<vm:RiskCodeValidation/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Use ValidationStep.
http://msdn.microsoft.com/en-us/library/system.windows.controls.validationrule.validationstep.aspx
RawProposedValue - Runs the ValidationRule before any conversion occurs.
ConvertedProposedValue - Runs the ValidationRule after the value is converted.
UpdatedValue - Runs the ValidationRule after the source is updated.
CommittedValue - Runs the ValidationRule after the value has been committed to the source.
By default, it's RawProposedValue, which prevents the binding to source from ever occurring - hence your confusion. Use a different option instead:
<Binding.ValidationRules>
<vm:RiskCodeValidation ValidationStep="UpdatedValue" />
</Binding.ValidationRules>

WPF TextBox control filter + validation

I have TextBox bound to a text property in my window with a validation rule (length is = 4). I needed to add filtering so I took ThomasLebruns WPFDeveloperTools FilteredTextBox. That works but when I use the control I specify each time the Validation.ErrorTemplate and the Binding.ValidationRules - I want to build those into the FilteredTextBox control so they apply automatically to every FilteredTextBox - how to manage that?
Currently I have to:
<Window>
<Grid>
<ps:FilteredTextBox x:Name="textboxPanId" Type="Hex" Grid.Column="1" Grid.Row="1" Height="35" Margin="0,2,0,2" MaxLength="4"
Validation.ErrorTemplate="{StaticResource TextBoxErrorTemplate}">
<TextBox.Text>
<Binding Path="pan_id" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<ps:PanIdValidation />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</ps:FilteredTextBox>
I would like just:
<ps:FilteredTextBox x:Name="textboxPanId" Type="Hex" Grid.Column="1" Grid.Row="1" Height="35" Margin="0,2,0,2" MaxLength="4">
<TextBox.Text>
<Binding Path="pan_id" UpdateSourceTrigger="PropertyChanged"/>
</TextBox.Text>
but have the error template and validation rule applied. How to move the setting of the Validation.ErrorTemplate and Binding.ValidationRule into resource dictionary with rest of FilteredTextBox xaml?.
<ResourceDictionary>
<ControlTemplate x:Key="TextBoxErrorTemplate">
...
</ControlTemplate>
I think if you create a style for FilteredTextBox in your ResourceDictionary you can set these there, but at least the ErrorTemplate for almost sure.

Silverlight 3 ValidatesOnException not found

I'm trying to add the validation and get the following from the compiler.
The property 'ValidatesOnException' was not found in type 'Binding'.
The property 'ValidatesOnException' does not exist on the type 'Binding' in the XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.
I tried adding System.Windows.Data in System.Windows as a xmlns but no luck.
The following are my attempts at the markup.
<TextBox x:Name="Office" Style="{StaticResource PhoneNumber}">
<TextBox.Text>
<Binding Path="Office" Mode="TwoWay" NotifyOnValidationError="true" ValidatesOnException="true" Converter="{StaticResource PhoneNumberConverter}" />
</TextBox.Text>
</TextBox>
<TextBox x:Name="Office" Style="{StaticResource PhoneNumber}" Text="{Binding Office, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnException=true, Converter={StaticResource PhoneNumberConverter}}" />
Any ideas?
The property is not called ValidatesOnException it is called ValidatesOnExceptions note the plural.

Resources