MVVM approved method for disabling certain characters in textbox? - c#

Is there a way to disable certain characters from being written into a wpf textbox without using code in the code behind file?
I have a few int fields that are bound to text boxes that I would like to limit to keys 0-9 only. If I enter anything else I do get the red validation error but that is not enough.

I recommend using IDataErrorInfo for WPF validation since WPF already understands how to use it, and its easy to implement.
You have to add the interface on your class, and the required methods will look like this:
#region IDataErrorInfo Members
public string Error
{
get { return null; }
}
public string this[string columnName]
{
get
{
if (columnName == "YourProperty")
{
int property = Convert.ToInt32(YourProperty);
if (property < 0 || property > 9)
return "The value must be between 0 and 9";
}
return string.Empty;
}
}
#endregion
Next, you need to set ValidatesOnDataErrors=True in your TextBox binding so it runs the validation whenever the property changes.
When something has gone wrong, it adds a red border on your control and the message you put on your validation:
You can read more about how to use the interface:
WPF: Validation made easy with IDataErrorInfo
Really simple WPF form data validation - how to?

In fact, this task is not possible enterily in xaml.
At some point, you need to write some code.
These are some links could be useful
http://blog.magnusmontin.net/2013/08/26/data-validation-in-wpf/
http://soumya.wordpress.com/2010/05/09/wpf-simplified-part-15-data-validation/

Related

Int field in Winform does not update null values in SQL Server database using C#

In my Winforms C# application, I have fields with Int data type and they are set to accept null values in SQL Server database (allow nulls).
In the forms I have some textboxes which are bound to those int data type fields. If I don't enter anything while creating a new record, it accepts. If I enter a number in the textbox, it also accepts it, and then if I delete it, it doesn’t accept it anymore and even doesn't allow me to move to the next field.
If I set its value as null or "" through code, it simply ignores and does not even update changes which I made in other non int text fields.
I am using following method to update.
this.Validate();
this.itemsbindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.sBSDBDataSet);
What can I do for the textbox to accept null values?
I have tried following.
IDTextBox.Text = "";
IDTextBox.Text = null;
I have tried following with the help of above solutions (specially Mr. Ivan) and this is how it worked out.
To clear the int field on the form:
IDTextBox.Text = String.Empty;
Then on Designer.cs file of the form, as suggested by Mr. Ivan, I searched for 'IDtextbox.DataBindings.Add' and replaced
this.IDTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.itemsbindingSource, "PictureID", true));
with
this.IDTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.itemsbindingSource, "PictureID", true, System.Windows.Forms.DataSourceUpdateMode.OnValidation, ""));
It took me a whole day to search and finally I posted my problem here, and it got solved in 1 hour.
This seems to be one of the WF data binding bugs. I can't say what exactly is causing it, but in order to make it work one should set Binding.NullValue property to "" (empty string, the default is null).
I couldn't find a way to do that in the designer, and also it would be quite annoying to locate all text boxes needed. So I would suggest you the following quick-and-dirty approach. Create a helper method like this:
public static class ControlExtensions
{
public static void FixTextBoxBindings(this Control control)
{
if (control is TextBox)
{
foreach (Binding binding in control.DataBindings)
if (binding.NullValue == null) binding.NullValue = "";
}
foreach (Control child in control.Controls)
child.FixTextBoxBindings();
}
}
and then simply include the following in your form Load event:
this.FixTextBoxBindings();
TextBox dont accept null value.
You can check if it null and set String.Empty;
If(dbValue == null)
{
IDTextBox.Text = String.Empty;
}
else
{
// here set value to your textbox
}

set string in TextEdit which allows only int

I'm using DevExpress.
In my project i have control (textEdit), which EditValue is binded to the property of "int" type.
Problem is that control allow to enter only numbers.
My task is: while form is in edit mode, the textEdit should display word "Automatic", and only after safe button press there should be generated number.
Now in edit mode textbox shows "0", is it possible to make it show "Automatic" in case of "0".
there is the property, to which the textBox is binded:
int fEventNr;
public int EventNr {
get { return fEventNr; }
set { SetPropertyValue<int>("EventNr", ref fEventNr, value); }
}
everything works except that it shows "0" and I don't know how to make him show "automatic"
maybe someone has any ideas?
This is a solution to your problem:
textEdit1.Properties.CustomDisplayText += new Properties_CustomDisplayText;
void Properties_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
{
if (yourCondition)
e.DisplayText = "Automatic";
}
txtEdit.Properties.NullText = "Automatic";
txtEdit.EditValue = null;
Consider changing public int EventNr to public int? EventNr so that you can be sure that the user HAS NOT supplied any value if the EditValue is null and you should generate it "Automatic"aly :)
I believe it's a bad practice to consider 0 as [value not set]. That is the reason why they invented the null.
On the properties panel go to Properties -> Mask . Set "MaskType" to RegEx and set "EditMask" to \d*. If you don't want integers to begin with zero(s) then set "EditMask" to [1-9]+\d* instead.
Alternatively you can do it by code :
this.textEditIntegersOnly.Properties.Mask.EditMask = "[1-9]+\\d*";
this.textEditIntegersOnly.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;

Two-way bind a "virtual" list of strings to a column

I have a list of Strings.
Well, conceptually. They are stored somewhere else, but I want provide an object which acts like a list (and provides any necessary events on top of that), with properties that I could bind to.
I want to establish a two-way binding over this data, to display it as a modifiable column in a DataGrid. I have the following problems with that:
I can't make a two-way binding because the binding needs a path (i.e. I can't have it look like {Binding} or {Binding Path=.} in the column, must be {Binding Path=someField"} to be made modifiable if I got this right, which sounds reasonable).
I don't exactly know how the proxy collection object should look like, in terms of interfaces (would IEnumerable + INotifyCollectionChanged sufficient?)
Is there any solution which doesn't involve creating one proxy object per every String in the collection? Could you suggest an efficient design?
To keep the discussion on the rails, let's assume I want to bind to something like this:
class Source {
public String getRow(int n);
public void setRow(int n, String s);
public int getCount();
public void addRow(int position, String s);
public void removeRow(int position);
}
That's not exactly my case, but when I know how to bind to this, I think I'll be able to handle any situation like this.
I'm OK with having to provide an adapter object on top of that Source, with any necessary interfaces and events, but I don't want to have one adapter object per row of data.
While making an adapter for the Source is relatively clear, then, unfortunatelly, the core of the second problem ('not wrapping every string in a miniobject') is a clash built into the .Net and WPF..
The first thing is that the WPF does provide you with many ways of registering 'on data modified' callbacks, but provides no way of registering callbacks that would provide a value. I mean, the "set" phase is only extendable, not interceptable, and the "get" - nothing at all. WPF will simply keep and return whatever data it has once cached.
The second thing is that in .Net the string is ... immutable.
Now, if ever you provide a string directly as a pathless binding or as a datacontext to any control, you are screwed in a dead end. The problem is, that WPF actually passes only the actual value of the binding, without the information of "where it came from". The underlying control will be simply given the string instance, and will have no sane way of modifying it as the string cannot change itself. You will not be even notified about such attempt, just like with read-only properties. What's more - if you ever manage to intercept such a modification attempt, and if you produce a proper new string, the WPF will never ask you again for the new value. To update the UI, you'd have to mannually, literally, force the WPF to re-ask you by for example changing the original binding so it points elsewhere (to the new value) or set the datacontext (to the new instance). It is doable with some VisualTree scanning, as every 'changed' callback gives you the DependencyObjects (Controls!), so yo ucan scan upwards/downwards and tamper with their properties.. Remember that option - I'll refer to this in a minute.
So, everything boils down to the fact that to get a normal 2-way binding you do not have to have a Path, you "just" have to have a mutable underlying data object. If you have immutable one - then you have to use a binding to a mutable property that holds the immutable value..
Having said that, you simply have to wrap the strings some how if you want to modify them.
The other question is, how to do that. There's a plenty of ways to do it. Of course, you can simply wrap them like Joe and Davio suggested (note to Joe: INotify would be needed there also), or you can try to do some XAML tricks with attached properties and/or behaviours and/or converters to do that for you. This is completely doable, see for example my other post - I've shown there how to "inject a virtual property" that pulled the data completely from elsewhere (one binding+converter performed the wrapping on the fly, second binding extracted the values from the attached-wrapper). This way you could create a "Contents" property on the string, and that property could simply return the string itself, and it'd be completely 2-way bindable with no exceptions.
But.. it would NOT work 2-way-ish.
Somewhere at the root of your binding/behaviour/conveter chain, there will be an immutable string. Once your smart autowrapping binding chain fires with 'on modified' callback you will be notified with pair of old/new values. You will be able to remap the values to new and old strings. If you implemented everything perfectly, the WPF will simply use the new value. If you tripped somewhere, then you will have to push the new value artificially back to the UI (see the options I'd asked you to remember). So, it's ok. No wrapper, old value was visible, it was changeable, you've got new value, the UI displays new value. How about storage?
Somewhere in the meantime you've been given a old/new value pair. If you analyze them, you'll get old/new strings. But how do you update the old immutable string? Can't do. Even if autowrapping worked, even if UI worked, even if editing seemed to work, you are now standing with the real task: you onmodified callback was invoked and you have to actually update that immutable string piece.
First, you need your Source. Is it static? Phew. What a luck! So surely it is instanced. In the on-modified callback we got only a old+new string.. how to get the Source instance? Options:
scan the VisualTree and search for it in the datacontexts and use whatever was found..
add some more attached properties and binding to bind a virtual "Source" property to every string and read that property from the new value
Well doable, but smells, but no other options.
Wait, there's more: not only the old/new value and an instance of Source are needed! You also need the ROW INDEX. D'oh! how to get that from the bound data? Again, options:
scan the VisualTree and search for it (blaargh)...
add some more attached properties and bindings to bind a virtual "RowIndex" property to every (blaaergh)...
At this point of time, while I see that all of this seems implementable and actually might be working properly, I really think that wrapping each string in a small
public class LocalItem // + INotifyPropertyChanged
{
public int Index { get; }
public Source Source { get; }
public string Content
{
get { Source...}
set { Source... }
}
}
will simply be more readable, elegant and .. SHORTER to implement. And less error-prone, as more details will be explicit instead of some WPF's binding+attached magic..
I find your approach a little weird.
DataGrids are usually used to display Rows. Rows consist of data that belongs together.
You could for instance easily map a row to a certain class. This means that the columns in your datagrid represent properties in your class.
What you're trying to do is the opposite, you're trying to get a relation between the column values instead of the row values.
Wouldn't it be easier to have a collection of your class which you can then bound the column to?
For instance
class MyClass : INotifyPropertyChanged
{
// Remember to actually implement INotifyPropertyChanged
string Column;
}
If you would have an ObservableCollection of MyClass you could bind the DataGrid to this collection. Whenever the property which I called "Column" changes, you could update your special list.
You can do this by hooking up some events. With the implementation of INotifyPropertyChanged, your columns will be updated if you update the "Column"-value directly.
I have this bit of code I use to bind a list of custom object to a DataContextMenu. You can alter it to use a list of strings and bind it to what you need
class SampleCode
{
class Team
{
private string _TeamName = "";
private int _TeamProperty1 = 0;
ObservableCollection<Territory> _Territories = new ObservableCollection<Territory>();
public Team(string tName)
{
this.TeamName = tName;
}
public ObservableCollection<Territory> Territories
{
get { return _Territories; }
set { _Territories = value; }
}
public string TeamName
{
get { return _TeamName; }
set { _TeamName = value; }
}
public int TeamProperty1
{
get { return _TeamProperty1; }
set { _TeamProperty1 = value; }
}
}
class Territory
{
private string _TerritoryName = "";
Team _AssociatedTeam = null;
public Territory(string tName, Team team)
{
this.TerritoryName = tName;
this.AssociatedTeam = team;
}
public Team AssociatedTeam
{
get { return _AssociatedTeam; }
set { _AssociatedTeam = value; }
}
public string TerritoryName
{
get { return _TerritoryName; }
set { _TerritoryName = value; }
}
public void Method1()
{
//Do Some Work
}
}
class MyApplication
{
ObservableCollection<Team> _Teams = new ObservableCollection<Team>();
ContextMenu _TeritorySwitcher = new ContextMenu();
public MyApplication()
{
}
public void AddTeam()
{
_Teams.Add(new Team("1"));
_Teams.Add(new Team("2"));
_Teams.Add(new Team("3"));
_Teams.Add(new Team("4"));
foreach (Team t in _Teams)
{
t.Territories.Add(new Territory("1", t));
t.Territories.Add(new Territory("2", t));
t.Territories.Add(new Territory("3", t));
}
SetContextMenu();
}
private void SetContextMenu()
{
HierarchicalDataTemplate _hdtTerritories = new HierarchicalDataTemplate();
_hdtTerritories.DataType = typeof(Territory);
HierarchicalDataTemplate _hdtTeams = new HierarchicalDataTemplate();
_hdtTeams.DataType = typeof(Team);
FrameworkElementFactory _TeamFactory = new FrameworkElementFactory(typeof(TreeViewItem));
_TeamFactory.Name = "txtTeamInfo";
_TeamFactory.SetBinding(TreeViewItem.HeaderProperty, new Binding("TeamProperty1"));
FrameworkElementFactory _TerritoryFactory = new FrameworkElementFactory(typeof(TreeViewItem));
_TerritoryFactory.Name = "txtTerritoryInfo";
_TerritoryFactory.SetBinding(TreeViewItem.HeaderProperty, new Binding("TerritoryProperty1"));
_hdtTeams.ItemsSource = new Binding("Territories");
_hdtTeams.VisualTree = _TeamFactory;
_hdtTerritories.VisualTree = _TerritoryFactory;
_hdtTeams.ItemTemplate = _hdtTerritories;
_TeritorySwitcher.ItemTemplate = _hdtTeams;
_TeritorySwitcher.ItemsSource = this._Teams;
}
}
}
Lazy solution
Derive from ObservableCollection<string> and let that collection be populated from the Source. In the derived class, register to collection change events and update the source accordingly. Bind the DataGrid column to the observable collection.
This should be pretty straightforward to write, but has a big drawback of duplicating all data in the collection.
More efficient solution
Create an adapter (as you suggested) and implement IList<string> and INotifyCollectionChanged. Let the list operations fall through directly to the source. Bind the DataGrid column to the adapter.
This approach would require some tedious boilerplate, but it's a thin layer between the WPF control and your Source.
This really depends on how you're implementing the UI. Bea Stollnitz did an excellent post of virtualizing the ItemsSource for the WPF DataGrid at http://bea.stollnitz.com/blog/?p=344 . With work I used this to edit as well as display data.
The easiest way is by placing the string in a wrapper class.
public class Wrapper
{
public string Content{get;set;}
}
Then you use the string via the wrapper class. This was the list items remain the same but the content changes.
The problem is when you do this without that then an old string is being deleted and a new one is created and the collection is confused.
Start with an ObservableCollection<string>. Then set the bindable control's ItemsSource to the ObservableCollection.

INotifyDataErrorInfo not raising error changed in code behind

I am experiencing issued performing validation from the codebehind. My data is displayed in a datagrid. One of the columns (type) is a drop down and when the drop down menu is changed it triggers a DropDownClosed Event which is handled in the code behind.
What I am trying to achieve is to validate the content of the following column to match the newly selected type in the drop down. If it does not match i want a validation error to be displayed on the grid. I implemented my validation using the INotifyDataErrorInfo interface and it works really well except when I use it in the code behind. When the code behind calls the validation the ValidationSummary of the datagrid is never updated. What I am doing wrong here ??? When using the debugger I can clearly see the errors being added to the Errors dictionnary of the interface...
Here is the handler:
private void TypeBoxChanged(object sender, EventArgs e)
{
ComboBox box = (sender as ComboBox);
IncomingPolicy row = (IncomingPolicy)box.DataContext;
string ruleTypeValue = TypeList.GetKeyForText(box.SelectedItem.ToString());
//check if the type is the same
if(row.TypeWrapper == ruleTypeValue)
return;
if (row.ValidateRule(ruleTypeValue))
{
//SAVE the record
}
else
{
row.RaiseErrorsChanged("RuleWrapper");
}
}
The validate rule method will based on the ruletypevalue call this method
public bool ValidateRegularExpression(string property, string value, string expression, string errorMessage)
{
bool isValid = true;
Regex regex = new Regex(expression);
Match match = regex.Match(value);
if (match.Success)
{
RemoveError(property, errorMessage);
}
else
{
AddError(property, errorMessage, false);
isValid = false;
}
return isValid;
}
I followed the sample implementation on MSDN http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifydataerrorinfo%28VS.95%29.aspx
Some time earlier I've implemented validation helpers and created the sample solution for both interfaces IDataErrorInfo and INotifyDataErrorInfo:
http://vortexwolf.wordpress.com/2011/10/01/wpf-validation-with-idataerrorinfo/
Source code
The main implementation is here:
this.PropertyChanged += (s, e) =>
{
// if the changed property is one of the properties which require validation
if (this._validator.PropertyNames.Contains(e.PropertyName))
{
this._validator.ValidateProperty(e.PropertyName);
OnErrorsChanged(e.PropertyName);
}
}
You should always call the OnErrorsChanged (or RaiseErrorsChanged in your case) method regardless of success of validation: if the property is invalid - the red border will be displayed, if it is valid - the bound control will be returned to its normal state.

Allow multi-line String properties in the Properties window

I've got a Windows Form User Control with a string property for setting the text of a textbox. This string can be multi-line.
I've noticed that on some controls with a text property, and instead of being forced to type in the single line property textbox, you get a little pop up where you can type multiple lines. (As a matter of fact, a Windows Forms Textbox control allows this on the Text property.)
How do I enable this functionality in the properties window for the property I have designed?
The following is not real code in my app, but an example of how such a property might be defined
public string Instructions
{
get
{
return TextBox1.Text;
}
set
{
TextBox1.Text = value;
}
}
You can use the EditorAttribute with a MultilineStringEditor:
[EditorAttribute(typeof(MultilineStringEditor),
typeof(System.Drawing.Design.UITypeEditor))]
public string Instructions
{
get
{
return TextBox1.Text;
}
set
{
TextBox1.Text = value;
}
}
To avoid adding a reference to System.Design and thus requiring the Full framework, you can also write the attribute like this:
[EditorAttribute(
"System.ComponentModel.Design.MultilineStringEditor, System.Design",
"System.Drawing.Design.UITypeEditor")]
Although this is less of a problem now that they've stopped splitting the framework into a Client Profile and a Full one.

Categories

Resources