http://msdn.microsoft.com/en-us/vstudio/bb614548.aspx
Friday, December 13, 2013
Sharing a VS video for developing add-ins.
This developer's video is incredibly good, so I thought i share, jere it is.
http://msdn.microsoft.com/en-us/vstudio/bb614548.aspx
http://msdn.microsoft.com/en-us/vstudio/bb614548.aspx
Add-in name cannot have a space while developing VS Add-in.
Add-in name cannot have a space, as ridiculous as it sounds, so that next time I rememeber the cause of the exception, here is the link I found and it works after I removed the space.
http://social.msdn.microsoft.com/Forums/en-US/90300b8f-3cfa-4072-8704-bbff2297f954/addinexception-in-addnamedcommand
http://social.msdn.microsoft.com/Forums/en-US/90300b8f-3cfa-4072-8704-bbff2297f954/addinexception-in-addnamedcommand
Saturday, November 30, 2013
Exception Validation in WPF
By default WPF provides validation control template which displays red border around the control whose input is being validated.
There are two simple ways one can indicate validation error due to exception.
There are two simple ways one can indicate validation error due to exception.
<Binding.ValidatesOnExceptions>True</Binding.ValidatesOnExceptions>
Or by specifying ValidatingRules and
including an instance of built-in ExceptionValidationRule.
<Binding.ValidationRules>
<ExceptionValidationRule></ExceptionValidationRule>
</Binding.ValidationRules>
Friday, November 29, 2013
Simple Validation in WPF
http://www.codeproject.com/script/Articles/ArticleVersion.aspx?waid=103359&aid=690130
Let us add code so that when values are entered in the above text boxes, they automatically run validation and if there is validation errors, they will be displayed next to the corresponding control. In order to do this the following two steps are needed.
1. Create a ControlTemplate with AdornedPlaceHolder
2. Implement validation class inheriting abstract class called ValidationRule.
Here is the sample validation control template. Let us start with a very simple validation control template where all we have a text block which will display a red exclamatory sign next to the control that has error.
Now let us also create a valiadtor class by inheriting from ValidationRule class and implementing its' abstract method as below.
When we run this now and enter a name longer than three characters long, it displays the red exclamatory sign indicating validation error.
Now let us just replace the TextBlock in the above validation control template code (the line that is in bold) by StackPanel containing an ellipse and textblock to display the same validation error as below.
Now when we run the code and validation fails, validation error will be displayed as shown in the screenshot below. (coded validator class for age and phone number as well)
That is all that is needed for the simplest validation error to show up next to the control. Notice in the validation control template, we are using a DockPanel as the layout control, therefore we can easily change where the error icon and error message will be displayed, we can display them on top of control that is failing validation (as above picture) or on the left, right, bottom.
Introduction
A simple example of validation in xaml for wpf controls and displaying error messages.Background
I was looking for something out-of-the-box from WPF where no extra coding of style or template is needed for displaying validation errors, where we just need to code the validation logic for each control and it should automatically display error icon or message next to the control. However I did not find anything straight forward like that in WPF. But in two simple steps it can be achieved.Using the code
Here is a very simple form in xaml that is created which has three text box controls.Let us add code so that when values are entered in the above text boxes, they automatically run validation and if there is validation errors, they will be displayed next to the corresponding control. In order to do this the following two steps are needed.
1. Create a ControlTemplate with AdornedPlaceHolder
2. Implement validation class inheriting abstract class called ValidationRule.
Here is the sample validation control template. Let us start with a very simple validation control template where all we have a text block which will display a red exclamatory sign next to the control that has error.
<ControlTemplate x:Key="validationErrorTemplate">
<DockPanel>
<TextBlock Foreground="Red" DockPanel.Dock="Top">!</TextBlock>
<AdornedElementPlaceholder x:Name="ErrorAdorner" >
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
Now let us also create a valiadtor class by inheriting from ValidationRule class and implementing its' abstract method as below.
public class NameValidator : ValidationRule
{
public override ValidationResult Validate(object value,
System.Globalization.CultureInfo cultureInfo)
{
if (value == null)
return new ValidationResult(false, "value cannot be empty.");
else
{
if (value.ToString().Length > 3)
return new ValidationResult(false,
"Name cannot be more than 3 characters long.");
}
return ValidationResult.ValidResult;
}
}
Let plug this validation control template and the validation rule with control that we want to validate. <TextBox Height="23" HorizontalAlignment="Left" Grid.Column="1" Grid.Row="0"
Name="textBox1" VerticalAlignment="Top" Width="120"
Validation.ErrorTemplate="{StaticResource validationErrorTemplate}"
>
<TextBox.Text>
<Binding Path="Name" Mode="TwoWay" UpdateSourceTrigger="LostFocus">
<Binding.ValidationRules>
<local:NameValidator></local:NameValidator>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
When we run this now and enter a name longer than three characters long, it displays the red exclamatory sign indicating validation error.
Now let us just replace the TextBlock in the above validation control template code (the line that is in bold) by StackPanel containing an ellipse and textblock to display the same validation error as below.
<ControlTemplate x:Key="validationErrorTemplate">
<DockPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
<Grid Width="12" Height="12">
<Ellipse Width="12" Height="12" Fill="Red"
HorizontalAlignment="Center" VerticalAlignment="Center"
></Ellipse>
<TextBlock Foreground="White" FontWeight="Heavy" FontSize="8"
HorizontalAlignment="Center" VerticalAlignment="Center"
TextAlignment="Center"
ToolTip="{Binding ElementName=ErrorAdorner,
Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
>X</TextBlock>
</Grid>
<TextBlock Foreground="Red" FontWeight="12" Margin="2,0,0,0"
Text="{Binding ElementName=ErrorAdorner,
Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
></TextBlock>
</StackPanel>
<AdornedElementPlaceholder x:Name="ErrorAdorner" >
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
Now when we run the code and validation fails, validation error will be displayed as shown in the screenshot below. (coded validator class for age and phone number as well)
That is all that is needed for the simplest validation error to show up next to the control. Notice in the validation control template, we are using a DockPanel as the layout control, therefore we can easily change where the error icon and error message will be displayed, we can display them on top of control that is failing validation (as above picture) or on the left, right, bottom.
Subscribe to:
Posts (Atom)