suppose I have one class like this:
class MyClass
{
... (some more properties here)
public int Min {get;set;}
public int Max {get;set;}
... (some more properties here)
}
Now I place a textbox in the designer and I want it to display Min and Max as text seperated with a dash.
For example if Min=3 and Max=10 then the textbox should display "3-10".
When the text is changed / the binding is updated it should parse the string "3-10" like this:
Split the string by '-' and parse the two strings with int.Parse(...)
If that doesn't work (an exception happens) I'd like to react on that somehow. For example displaying an error message would work.
How do I do this?
The VisualStudio designer only allows me to bind the Text to one property of an object.
For displaying 3-10, you can write
TextBoxName.Text=Min + "-" + Max;
and, You may raise an exception and show the MessageBox as:
try{
int.Parse(Min);
int.Parse(Max);
}
catch(Exception ae){
MessageBox.Show("Some error message");
}
EDIT:
For Binding,
textBoxName.DataBindings.Add("Text",this,"StringVariable");
//Text property,this form, name of the variable.
where StringVariable is some property returning Min + "-" + Max;
Related
I have an enum declared as follows:
public enum DirectionTypes
{
IN = 2,
OUT = 1
}
This enum is used on user controls where I need to specify in XAML which direction the control needs to work. I created a dependency property on each user control as follows:
public static readonly DependencyProperty DirectionTypeProperty =
DependencyProperty.Register(
"DirectionType",
typeof(DirectionTypes),
typeof(TransactionGrid), new PropertyMetadata(DirectionTypes.IN));
public DirectionTypes DirectionType
{
get
{
return (DirectionTypes)GetValue(DirectionTypeProperty);
}
set
{
SetValue(DirectionTypeProperty, value);
}
}
I can then use the user control as follows:
<local:TransactionGrid x:Name="theGrid" DirectionType="OUT" />
I can run the program just fine. The problem is that DirectionType="OUT" causes an intellisense error in Visual Studio 2015. I get blue squiglies under the XAML property and my designer won't show the preview, saying instead "Invalid Markup". The error says The Type converter for DirectionTypes does not support converting from a string.
What am I missing that will allow the XAML parse properly.
Specify the enumeration value explicitly as follows (assuming DirectionTypes is in the same name space as local):
<local:TransactionGrid x:Name="theGrid" DirectionType="{x:Static local:DirectionTypes.OUT}" />
I have an application with a plugin architecture using MEF. For every exported part there is an attribute with the part's name, and I want to have the names translated, because I use these strings to display the available parts in ListBoxes (or the like).
So, I tried to set the 'Name = Strings.SomeText" in the [Export] annotation, but I get the following error:
"An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"
Is there a solution to this? I find the use of the Metadata very useful (I do lazy loading) and I would not want to redesign everything just to get a few texts translated.
Any ideas? Thanks.
Unfortunately you can't directly provide the translated text to the attributes because an attribute can only contain data that is known at compile time. So you will need to provide some compile time constant value that you can later use to look up the translated test.
One solution would be to pass the resource name to the attribute. Then when you want to display the translated text you grab the resource name, look up the text in the resources and display the result.
For instance your attribute could look something like:
[Export(Name = "SomeText")]
public class MyExport
{
}
Then when you want to display the string you load the resources from the assembly that defines the export and you extract the actual text from the loaded resources. For instance like this (as borrowed from another answer):
var assembly = typeof(MyExport).Assembly;
// Resource file.. namespace.ClassName
var rm = new ResourceManager("MyAssembly.Strings", assembly);
// exportName contains the text provided to the Name property
// of the Export attribute
var text = rm.GetString(exportName);
The one obvious drawback about this solution is that you lose the type-safety that you get from using the Strings.SomeText property.
--------- EDIT ---------
In order to make it a little easier to get the translated text you could create a derivative of the ExportAttribute which takes enough information to extract the translated text. For example the custom ExportAttribute could look like this
public sealed class NamedExportAttribute : ExportAttribute
{
public NamedExportAttribute()
: base()
{
}
public string ResourceName
{
get;
set;
}
public Type ResourceType
{
get;
set;
}
public string ResourceText()
{
var rm = new ResourceManager(ResourceType);
return rm.GetString(ResourceName);
}
}
Using this attribute you can apply it like this
[NamedExport(
ResourceName = "SomeText",
ResourceType = typeof(MyNamespace.Properties.Resources))]
public sealed class MyClass
{
}
Finally when you need to get the translated text you can do this
var attribute = typeof(MyClass).GetCustomAttribute<NamedExportAttribute>();
var text = attribute.ResourceText();
Another option is to use the DisplayAttribute
DocumentControl is my usercontrol, and I have written following lines in my function, but I am getting the value as blank.
var DocumentControl1 = new DocumentControl();
string docCode= DocumentControl1.txtDocCode.Text;
Save the value of txtDocCode in a public variable as docCode. you can access the value like this,
DocumentControl1 d= new DocumentControl();
string docCode = d.DocCode;
I have taken the values from database using Entity Framework , I have that a class called EmployeeDetail in that I have one property called JOined AS.
It gives the values as 'F' for fresher and 'E' for experience.. I need to display this value in
DataGrid as Experience or Fresher instead of F or E..
For that I have created the partial class for EmployeeDetail and created one property CustJoinedAs ...
But I didn't get idea to compare the value and return the required value to DataGrid Column...
Here I also have to give each value to DataGrid.. is it automatically bind when I give the property to Column in DataGrid...or any propetyChanged Events have to be Write..
please give a brief idea.. and Code..
Thank you
You can implement the property in partial class like
public partial class EmployeeDetail
{
public string CustJoinedAs
{
get{return this.JoinedAs = "E"? "Experienced" : "Fresh";}
}
}
And bind column of grid to CustJonedAs despite of JoinedAs.
i have several intboxes under the same window parent. I created a custom validator with custom message for an intbox. For displaying the error message i use a label which i give a unique id. Now i need to use the same constraint for all the intboxes. As in the custom error message i have a unique label id for displaying the error so how do i use the same message for all the intboxes?
Here is the code for my custom validator with custom message:
<zscipt> <![CDATA[
class MyConst implements Constraint, CustomConstraint {
//Constraint//
public void validate(Component comp, Object value) {
if (value == null || ((Integer)value).intValue() >8)
throw new WrongValueException(comp, "values only b/w 0 and 8");
}
//CustomConstraint//
public void showCustomError(Component comp, WrongValueException ex) {
errmsg.setValue(ex != null ? ex.getMessage(): "");
}
}
Constraint ctt = new MyConst();
]]>
Thanks.
There are a couple solutions. First, you could enhance MyConst's constructor to accept Label.
Second, you could use a name pattern. For example, if the label's ID is always a catenation of the textbox's ID and, say, "Error". Then, you could use comp.getFellow(comp.getId()+"Error") to retrieve the label.
In addition, you could use the server-side component selector to get the label.