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

Re: PropertyGrid by Marc

Marc
Thu Mar 13 12:53:38 CDT 2008

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);
}
}

Re: PropertyGrid by Marc

Marc
Thu Mar 13 12:55:37 CDT 2008

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

Re: PropertyGrid by Marc

Marc
Thu Mar 13 13:04:23 CDT 2008

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

Re: PropertyGrid by J-L

J-L
Fri Mar 14 04:02:31 CDT 2008

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.



Re: PropertyGrid by Marc

Marc
Fri Mar 14 15:49:01 CDT 2008

> 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".


Re: PropertyGrid by J-L

J-L
Mon Mar 17 05:42:41 CDT 2008

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.