Validating data entered on the form page : WPF - validation

I have around 20 controls which are binded to different properties of a class along with validation rules like following. For sake of understanding i am writing the code for one control as other are same.
<TextBox Style="{StaticResource errorStyle}" Grid.Column="0" Grid.Row="2" Grid.RowSpan="1" HorizontalAlignment="Left" Margin="110,100,0,0" Name="balesText" VerticalAlignment="Top" Width="170" >
<TextBox.Text>
<Binding Source="{StaticResource insertTransaction}" UpdateSourceTrigger="Explicit" Path="Bales">
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Right now i am checking for the input errors in code-behind in the following manner
BindingExpression balesBe = balesText.GetBindingExpression(TextBox.TextProperty);
balesBe.UpdateSource();
.
.
if (balesBe.HasError)
{
MessageBox.Show("Please correct Errors", "Insert Aborted");
}
else
{
Binding insertTransactionBinding = BindingOperations.GetBinding(balesText, TextBox.TextProperty);
InsertTransaction insertTransaction = insertTransactionBinding.Source as InsertTransaction;
insertMessage = insertTransaction.Add();
MessageBox.Show(insertMessage, "Transaction");
this.NavigationService.Refresh();
}
Now, the Question is : Is there any way i can validate these 20 controls in one go or i need to manually define their BindingExpression and check for validation error??

Make your own UserControl "OwnTextBox". Inherit it from TextBox (OwnTextBox: TextBox) and define there DependencyProperty Validate. Then you only have to remember, that TextBox is part of logical tree and make search from it.

Related

Xamarin: Binding grouped RadioButton isChecked property with a viewModel

Just to be clear I'm talking here about the RadioButton released with Xamarin 4.6 in 2006.
My aim here is to create one binding which will work with multiple RadioButtons in the same group. Ultimately so I can leave my screen and come back to it and set the last selected RadioButton item.
So I know you can set a true of false property binding to the IsChecked property. However, this single item has no context of which item it is.
I know you can also set a binding context to a page or a control... so I guess I could set a context to a StackLayout for all the items inside it... but this still won't help.
This is something like what I want to achieve... yes I won't acctually have 10, I'm just making a point that I don't want to have to use lots of different bindings..
<RadioButton GroupName="Numbers" Text="One"
IsChecked="{Binding IsCheckedNumbersgroup}" />
<RadioButton GroupName="Numbers" Text="Two"
IsChecked="{Binding IsCheckedNumbersgroup}" />
....
<RadioButton GroupName="Numbers" Text="Ten"
IsChecked="{Binding IsCheckedNumbersgroup}" />
If we binding the same source with multi RadioButton , it will cause issue as when you click one of them , other RadioButton will also been effected .
So in your case if you do not want to define multi property , you could define a List and binding them with the item .
in Xaml
Command, which is executed when the RadioButton is selected.
<RadioButton GroupName="Numbers" Text="One" IsChecked="{Binding MySource[0]}" Command="{Binding ButtonCommand}" CommandParameter="0" />
<RadioButton GroupName="Numbers" Text="Two" IsChecked="{Binding MySource[1]}" Command="{Binding ButtonCommand}" CommandParameter="1" />
<RadioButton GroupName="Numbers" Text="Three" IsChecked="{Binding MySource[2]}" Command="{Binding ButtonCommand}" CommandParameter="2" />
in ViewModel
public ObservableCollection<bool> MySource { get; set; }
public xxxViewModel()
{
MySource = new ObservableCollection<bool>() {true,false,false };
ButtonCommand = new Command((org)=> {
var index = int.Parse(org.ToString());
App.Current.MainPage.DisplayAlert("Title","the status of RadioButton"+(index+1).ToString()+"is"+MySource[index].ToString() ,"OK");
});
}

Is there a way I can pass a grid name or a parameter into a tapped event?

I have this code and somehow I want to get the text value of the first label to the tapped event in the CS code. As the tapped event is on the grid I had an idea of putting the text value into x:Name.
<ViewCell >
<Grid VerticalOptions="CenterAndExpand" x:Name="{Binding [0].Name}" Tapped="atiSelectValueX" >
<local:StyledLabel Text="{Binding [0].Name}" HorizontalOptions="StartAndExpand" />
<local:StyledLabel IsVisible="{Binding [0].IsSelected}" TextColor="Gray" HorizontalOptions="End" Text="x" />
</Grid>
</ViewCell>
The CS code I have so far:
void atiSelectValueX(object sender, EventArgs e)
{
var cell = sender as Grid;
if (cell == null)
return;
var selected = cell. <<< I want to get the name here
What I would like to do is to get the x:Name value in the CS code. I was hoping to get the sender information but it seems like I cannot enter
cell.Name
Is there another way I can get a parameter like the name (which is the same as the text in the first label always) in the C# code?
The x:Name is metadata used by the XAML tools, not an actual property of the object. You could also bind Name to an unused property, like StyleID, and access that instead
<Grid VerticalOptions="CenterAndExpand" StyleId="{Binding [0].Name}" ...
and then
var selected = cell.StyleId;

windows phone 8 longlistselector show and hide button based on recordid

I have a books list displayed in my long list selector like this
<DataTemplate x:Key="BooksItemTemplate">
<StackPanel Grid.Column="1" Grid.Row="0" VerticalAlignment="Top">
<TextBlock Name="booktitle" Text="{Binding BookTitle,Mode=OneWay}" Style="{StaticResource PhoneTextNormalStyle}" TextWrapping="Wrap" FontFamily="{StaticResource PhoneFontFamilySemiBold}"/>
<TextBlock Text="{Binding AuthorName,Mode=OneWay}" Style="{StaticResource PhoneTextNormalStyle}" TextWrapping="Wrap" FontFamily="{StaticResource PhoneFontFamilySemiLight}"/>
<Button Content="Add To Favourites" Tag="{Binding BookId,Mode=OneWay}" Click="Button_Click_1" ></Button>
</StackPanel>
</Grid>
</DataTemplate>
<phone:LongListSelector x:Name="bookslist" Grid.Row="1"
ListFooter="{Binding}"
ItemsSource="{Binding BooksList}"
Background="Transparent"
IsGroupingEnabled="False"
ListFooterTemplate ="{StaticResource booksListFooter}"
ItemTemplate="{StaticResource BooksItemTemplate}"/>
so there is an add to favourites button beside every book in the list . pressing that button i am entering the pressed book's id in my isolatedstoragesetting like this
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Button bt = (Button)sender;
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
List<long> ListFavourites;
if (settings.Contains("ListFavourites"))
{
ListFavourites = settings["ListFavourites"] as List<long>;
}
else
{
ListFavourites = new List<long>();
}
if(!ListFavourites.Contains(Convert.ToInt64(bt.Tag)))
{
ListFavourites.Add(Convert.ToInt64(bt.Tag));
}
settings["ListFavourites"] = ListFavourites;
settings.Save();
}
problem:
now when loading the above book list(longlistselector) when the page loads i want to show or hide the add to favorites button based on whether the bookid is present in the isolatedstoragesetting or not. what i have tried here is that i have tried to bind the converter to the add to favourite button and also bind the convertparameter with bookid. but the convertparameter doesn't support binding.
so what is the technique i can use to show or hide the add to favourite button based on the book id presence in the favourite list in the isolatedstoragesetting?
how can i hide the button based when clicking it based on the bookid?
You are almost there in thinking to use a converter. The actual idea, when materialized, should look something like this.
First, you will need to implement a converter, in this case, you will need to convert the bookid to a Visibility value.
public class BookIdToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
//value is booking id here, which means that you just need to check against the isolatedStorageSettings
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return null;
}
}
In your DataTemplate, the binding should take place like this:
<Button Content="Add To Favourites" Tag="{Binding BookId,Mode=OneWay}"
Click="Button_Click_1" Visibility={Binding BookId,Converter={StaticResource TheConverterCreatedAbove}} >
</Button>
That should do the trick.
The MVVM way would be to expand your ViewModel. It would be much better to add an AddToFavoritesCommand to BookViewModel instead of putting the logic in code behind. If the button is bound to that Command it would automatically go disabled when the Command would properly (with CanExecuteChanged) switch CanExecute to false.
In your case, you can add a property IsFavourite or CanAddToFavoirtes and then use a standard BoolToVisibility converter (the Command execution would set this property and the BookViewModel would be initialized with the correct value read from IsolatedStorage).
All logic behind the presentation of Book and functionalities related to Book belong to BookViewModel.

Mvvm TextBox KeyDown Event

I'd like to know how I can handle a KeyDown Event with MVVM in my ViewModel.
I have a TextBox and when the user hits a key, which is not a number, the input shouldn't be allowed. I'd normally do it with Code behind like this (not full code, just an easy example):
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
// Determine whether the keystroke is a number from the top of the keyboard.
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
e.Handled = true;
}
}
Now I want to put this somehow in my ViewModel with a Command. I'm new to MVVM and I'm only using Bindings right now (which works fine :) ), but I don't know how to use a Command at all...
My TextBox looks i.e. like this:
<TextBox Text="{Binding MyField, Mode=TwoWay}"/>
ViewModel:
private string _myfield;
public string MyField{
get { return _myfield; }
set {
_myfield= value;
RaisePropertyChanged( ()=>MyField)
}
}
But the setter will only be called, when I leave the TextBox + I don't have access to the entered Key.
I know my answer is late but if someone has a similar problem. You must just set your textbox like this:
<TextBox Text="{Binding MyField, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
The following works for handling the "Enter" key in a TextBox:
<TextBox Text="{Binding UploadNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding
Key="Enter"
Command="{Binding FindUploadCommand}" />
</TextBox.InputBindings>
</TextBox>
I do this by using the interaction triggers. (this example uses the MVVM_Light framework for the command binding)
here is an example:
<textBox Text="{Binding MyField}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cmd:EventToCommand Command="{Binding MyCommandName}" CommandParameter="YouCommandParameter"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<TextBox/>
Create a ICommand object in your view model with the name MyCommandName and add these to the top of your xaml:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="http://www.galasoft.ch/mvvmlight"
You don't have to use the mvvm-light command. This is just what I use and I like it because it allows me to use the CanExecute method of the ICommand interface
hope this helps

WPF4: How do you set a value in a UserControl from the Main Window?

This has to be simple, at least it was in good old .Net where it took maybe four lines of code. I'm working in VS2010, C#, WPF4.
I have a user control with a textbox. When I click a button in the main window, I want my user control textbox to reflect some text. Is this possible in WPF4 with less than 500 lines of esoteric code?
The problem is that while I know the textbox is getting the new text as evidenced from breakpoints in the user control code, that text is never being reflected to the main window. The main window still shows the original text. It has to be some kind of binding thing, and I really don't think I should have to create templates and resources and all for this simple situation. It's got to be something simple that I'm forgetting in the forest of WPF4. Below is what I have. After clicking the button, the textbox is still blank; it does not say "hello earthlings."
In the user control code:
public partial class UserControl1 : UserControl
{
public static readonly DependencyProperty TextProperty;
public UserControl1()
{
InitializeComponent();
}
static UserControl1()
{
TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(UserControl1), new UIPropertyMetadata(null));
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
}
User control xaml:
<UserControl x:Class="WTFUserControlLibrary.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<Grid Height="164" Width="220">
<TextBox Name="txtTest" BorderBrush="red" BorderThickness="2" Height="25" Text="{Binding ElementName=UserControl1, Path=Text, Mode=TwoWay}"></TextBox>
</Grid>
(I have no idea what the text binding is supposed to be doing in this case.)
Main window code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
WTFUserControlLibrary.UserControl1 uc = new WTFUserControlLibrary.UserControl1();
uc.Text = "hello earthlings";
}
}
and the main window xaml:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:WTFUserControlLibrary;assembly=WTFUserControlLibrary"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="71,65,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<my:UserControl1 HorizontalAlignment="Left" Margin="71,94,0,0" Name="userControl11" VerticalAlignment="Top" Height="116" Width="244" />
</Grid>
Thanks Earthlings (and also those who designed this mess!)
In your method button1_Click you are creating a new user control. This is not the usercontrol in the window and is never displayed.
Instead, give your usercontrol a name in the XAML:
x:Name="uc"
Then in the button1_Click method you just remove that first line where you create a new usercontrol.
update
You want the user control XAML to look more like this:
<UserControl x:Class="WTFUserControlLibrary.UserControl1"
x:Name="thisControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<Grid Height="164" Width="220">
<TextBox Name="txtTest" BorderBrush="red"
BorderThickness="2" Height="25"
Text="{Binding ElementName=thisControl, Path=Text, Mode=TwoWay}" />
</Grid>
I added x:Name="thisControl" to the root UserControl element, and then referenced this in the binding.
I'll try to explain the binding:
You have this textbox inside your user control, but you want to be able to bind the text value to something outside the user control. What you've done is set up a dependency property on the user control, and bound that textbox to it, so you are using the binding infrastructure pass values from the top level of the UserControl to constituent controls inside it.
Basically, it looks like this:
data
---> bind UserControl1.Text to data
---> bind TextBox.Text to UserControl1.Text

Resources