PropertyGrid

J

J-L

Hi,

When I use the propertygrid to display boolean value property, le
choice is always "true or false", in english.

Is it possible to modify it to strings for used culture ?
For exemple, "vrai ou faux" in french ...

Thanks,

J-L
 
M

Marc Gravell

Like below? Note that really you should take the culture from the arg
to ConvertTo / ConvertFrom - but I was in a hurry ;-p

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.ComponentModel;

class MyDisplayNameAttribute : DisplayNameAttribute
{
public MyDisplayNameAttribute(string displayName) :
base(displayName) { }
public override string DisplayName {
get { // TODO: localize the display name
return "Cultured " + base.DisplayName;
}
}
}
class MyBooleanConverter : BooleanConverter
{
private readonly string trueVal, falseVal;
public MyBooleanConverter()
{
trueVal = "vrai"; falseVal = "faux";
}
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new string[] { trueVal,
falseVal });
}
public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
string sVal = value as string;
if (sVal != null) {
if (sVal == trueVal) return true;
if (sVal == falseVal) return false;
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type
destinationType)
{
if (destinationType == typeof(string) && value != null &&
value is bool)
{
return ((bool)value) ? trueVal : falseVal;
}
return base.ConvertTo(context, culture, value,
destinationType);
}
}
class Foo
{
[MyDisplayName("Some Prop")]
[TypeConverter(typeof(MyBooleanConverter))]
public bool Bar { get; set; }
}


static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form form = new Form();
PropertyGrid grid = new PropertyGrid();
grid.SelectedObject = new Foo();
grid.Dock = DockStyle.Fill;
form.Controls.Add(grid);
Application.Run(form);
}
}
 
M

Marc Gravell

I forgot to say... if you get the values from a resx that will save
you having to do your own culture lookup; note you can also localize
DescriptionAttribute etc just as easily
 
M

Marc Gravell

Oops - one minor bug; you don't need to override GetStandardValues,
since this is meant to return the *typed* values (i.e. true/false, not
the strings). If you simply remove this override, then the double-
click behavior starts working correctly ;-p

Any other glitches, let me know...

Marc
 
J

J-L

Marc Gravell a pensé très fort :
Oops - one minor bug; you don't need to override GetStandardValues,
since this is meant to return the *typed* values (i.e. true/false, not
the strings). If you simply remove this override, then the double-
click behavior starts working correctly ;-p

Any other glitches, let me know...

Marc

Thanks a lot, it work very well ...

Just 2 questions :

public MyBooleanConverter()
{
trueVal = "vrai";
falseVal = "faux";
}

Is it in this place that I need to localize trueval and falseval in
function of culture ?


and in :

public override string DisplayName
{
get
{
// TODO: localize the display name
return "Cultured " + base.DisplayName;
}
}

Is it for the CategoryAttribute ?
I have tried to modify base.DisplayName but it is read-only.
 
M

Marc Gravell

Is it in this place that I need to localize trueval and falseval in
function of culture ?
Actually, no. The "right" place depends on the app. In the more
complex case, if you need to support multiple cultures *at the same
time* then the correct approach would be to use the culture passed
into ConvertTo/ConvertFrom. However, if you only need to support a
single culture (typical for a windows client), then I would use some
static fields:
(this example uses resx file)

// public ctor (required to work)
public MyBooleanConverter() { }

// private static true/false values
private static readonly string trueVal, falseVal;
static MyBooleanConverter()
{
trueVal =
WindowsFormsApplication1.Properties.Resources.TrueText;
falseVal =
WindowsFormsApplication1.Properties.Resources.FalseText;
}
Is it for the CategoryAttribute ?
The one I posted (DisplayName) controls the name that appears for the
property. Category is done slightly differently:
(this example just shows simple case - but you can generalise using
Properties.Resources.ResourceManager.GetString(whatever))

class MyCategoryAttribute : CategoryAttribute
{
public MyCategoryAttribute(string category) : base(category) { }

protected override string GetLocalizedString(string value)
{
return "Translated";
}
}

If you return null from GetLocalizedString() then it uses the original
text as a default. If you return an empty string ("") it dispays
"Misc".
 
J

J-L

thanks a lot more ...

Yes, ma soft need to change of culture if the user want it.
An option for that is in a settings panel , and the user restart the
application to get new language in interface.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top