Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

PropertyGrid

1 réponse
Avatar
J-L
Bonjour,

Quand on utilise la propertygrid pour afficher des valeurs booléennes,
le choix est toujours true ou false.

Est-il possible de changer ça au runtime en fonction du choix de la
culture utilisée ? J'aurais besoin par exemple de vrai/faux en
français, et des traductions pour divers autres langages de mon appli.

Merci

J-L

1 réponse

Avatar
Jean-Luc M.
Merci à Marc Grawell

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


--
Jean-Luc M.