Na potrzeby swojego programu potrzebuję stworzyć/znaleźć kontrolkę o cechach:
wygląda jak DataGridView z tą różnicą, że potencjalne wiersze są traktowane jak kolumny i vice versa
Dla zobrazowania dodam, że taką cechę ma w Visual Studio okienko "properties" dla dowolnej kontrolki.
Tam, wiersze są a'la kolumnami. Relacja zachodzi 1:1.
No to czemu Ci standardowy PropertyGrid nie pasuje? Przecież okno Properties jest właśnie o niego oparte.
somekind napisał(a)
No to czemu Ci standardowy PropertyGrid nie pasuje? Przecież okno Properties jest właśnie o niego oparte.
dzięki, nie znałem tego komponentu.
Zrobiłem wszystko co konieczne w ramach przykładu, (http://msdn.microsoft.com/en-us/library/aa302326.aspx) ale nie działa mi rozwijanie obiektu innej klasy (SpellingOptions), który jest zawarty w obiekcie klasy posiadającej inne typy zwykłe jako właściwości (AppSettings).
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
AppSettings appset = new AppSettings();
propertyGrid1.SelectedObject = appset;
}
}
[DefaultPropertyAttribute("SaveOnClose")]
public class AppSettings
{
private bool saveOnClose = true;
private string greetingText = "Welcome to your application!";
private int maxRepeatRate = 10;
private int itemsInMRU = 4;
private bool settingsChanged = false;
private string appVersion = "1.0";
private Size windowSize = new Size(100, 100);
private Font windowFont = new Font("Arial", 8, FontStyle.Regular);
private Color toolbarColor = SystemColors.Control;
private SpellingOptions spellingCheckOptions;
private string defaultFileName;
public string DefaultFileName
{
get { return defaultFileName; }
set { defaultFileName = value; }
}
public SpellingOptions SpellingCheckOptions
{
get { return spellingCheckOptions; }
set { spellingCheckOptions = value; }
}
[CategoryAttribute("Document Settings"),
DefaultValueAttribute(true)]
public bool SaveOnClose
{
get { return saveOnClose; }
set { saveOnClose = value; }
}
[CategoryAttribute("Document Settings")]
public Size WindowSize
{
get { return windowSize; }
set { windowSize = value; }
}
[CategoryAttribute("Document Settings")]
public Font WindowFont
{
get { return windowFont; }
set { windowFont = value; }
}
[CategoryAttribute("Global Settings")]
public Color ToolbarColor
{
get { return toolbarColor; }
set { toolbarColor = value; }
}
[CategoryAttribute("Global Settings"),
ReadOnlyAttribute(true),
DefaultValueAttribute("Welcome to your application!")]
public string GreetingText
{
get { return greetingText; }
set { greetingText = value; }
}
[CategoryAttribute("Global Settings"),
DefaultValueAttribute(4)]
public int ItemsInMRUList
{
get { return itemsInMRU; }
set { itemsInMRU = value; }
}
[DescriptionAttribute("The rate in milliseconds that the text will repeat."),
CategoryAttribute("Global Settings"),
DefaultValueAttribute(10)]
public int MaxRepeatRate
{
get { return maxRepeatRate; }
set { maxRepeatRate = value; }
}
[BrowsableAttribute(false),
DefaultValueAttribute(false)]
public bool SettingsChanged
{
get { return settingsChanged; }
set { settingsChanged = value; }
}
[CategoryAttribute("Version"),
DefaultValueAttribute("1.0"),
ReadOnlyAttribute(true)]
public string AppVersion
{
get { return appVersion; }
set { appVersion = value; }
}
}
// The TypeConverter attribute applied to the SpellingOptions class.
[TypeConverterAttribute(typeof(SpellingOptionsConverter)),
DescriptionAttribute("Expand to see the spelling options for the application.")]
public class SpellingOptions
{
private bool spellCheckWhileTyping = true;
private bool spellCheckCAPS = false;
private bool suggestCorrections = true;
[DefaultValueAttribute(true)]
public bool SpellCheckWhileTyping
{
get { return spellCheckWhileTyping; }
set { spellCheckWhileTyping = value; }
}
[DefaultValueAttribute(false)]
public bool SpellCheckCAPS
{
get { return spellCheckCAPS; }
set { spellCheckCAPS = value; }
}
[DefaultValueAttribute(true)]
public bool SuggestCorrections
{
get { return suggestCorrections; }
set { suggestCorrections = value; }
}
}
public class SpellingOptionsConverter : ExpandableObjectConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context,
System.Type destinationType)
{
if (destinationType == typeof(SpellingOptions))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture,
object value,
System.Type destinationType)
{
if (destinationType == typeof(System.String) &&
value is SpellingOptions)
{
SpellingOptions so = (SpellingOptions)value;
return "Check while typing:" + so.SpellCheckWhileTyping +
", check CAPS: " + so.SpellCheckCAPS +
", suggest corrections: " + so.SuggestCorrections;
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool CanConvertFrom(ITypeDescriptorContext context,
System.Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, object value)
{
if (value is string)
{
try
{
string s = (string)value;
int colon = s.IndexOf(':');
int comma = s.IndexOf(',');
if (colon != -1 && comma != -1)
{
string checkWhileTyping = s.Substring(colon + 1,
(comma - colon - 1));
colon = s.IndexOf(':', comma + 1);
comma = s.IndexOf(',', comma + 1);
string checkCaps = s.Substring(colon + 1,
(comma - colon - 1));
colon = s.IndexOf(':', comma + 1);
string suggCorr = s.Substring(colon + 1);
SpellingOptions so = new SpellingOptions();
so.SpellCheckWhileTyping = Boolean.Parse(checkWhileTyping);
so.SpellCheckCAPS = Boolean.Parse(checkCaps);
so.SuggestCorrections = Boolean.Parse(suggCorr);
return so;
}
}
catch
{
throw new ArgumentException(
"Can not convert '" + (string)value +
"' to type SpellingOptions");
}
}
return base.ConvertFrom(context, culture, value);
}
}
}
gdzie jest błąd?
jakby ktoś miał podobny problem w przyszłości, rozwiązanie:
Przy obiektach typów referencyjnych trzeba utworzyć jawnie taki obiekt ;p
private SpellingOptions spellingCheckOptions = new SpellingOptions();
solution napisał(a)
Przy obiektach typów referencyjnych trzeba utworzyć jawnie taki obiekt ;p
Hmm.... A da się inaczej?
tak, przy NIE-referencyjnych :)