プロパティグリッドを、UIをもう少しカスタマイズできるものに置き換えています。フォームにボタンを配置しました。クリックするとCollectionEditorがポップアップ表示され、コードを変更できるようになります。PropertyGridを使用していたとき、必要なのは、CollectionEditorを指すプロパティにいくつかの属性を追加することだけでした。しかし、CollectionEditorを手動で呼び出すにはどうすればよいですか?ありがとう!
1863 次
2 に答える
12
ここで答えを見つけました:http ://www.devnewsgroups.net/windowsforms/t11948-collectioneditor.aspx
上記のリンク先のサイトがいつかなくなる場合に備えて、その要点を以下に示します。ただし、コードは上記のリンクからそのままです。コメントは私のものです。
リストボックスとボタンを備えたフォームがあるとします。CollectionEditorを使用してリストボックス内のアイテムを編集する場合は、EventHandlerで次のようにします。
private void button1_Click(object sender, System.EventArgs e)
{
//listBox1 is the object containing the collection. Remember, if the collection
//belongs to the class you're editing, you can use this
//Items is the name of the property that is the collection you wish to edit.
PropertyDescriptor pd = TypeDescriptor.GetProperties(listBox1)["Items"];
UITypeEditor editor = (UITypeEditor)pd.GetEditor(typeof(UITypeEditor));
RuntimeServiceProvider serviceProvider = new RuntimeServiceProvider();
editor.EditValue(serviceProvider, serviceProvider, listBox1.Items);
}
次に行う必要があるのは、RuntimeServiceProvider()を作成することです。これは、これを実装するために上記のリンクのポスターが書いたコードです。
public class RuntimeServiceProvider : IServiceProvider, ITypeDescriptorContext
{
#region IServiceProvider Members
object IServiceProvider.GetService(Type serviceType)
{
if (serviceType == typeof(IWindowsFormsEditorService))
{
return new WindowsFormsEditorService();
}
return null;
}
class WindowsFormsEditorService : IWindowsFormsEditorService
{
#region IWindowsFormsEditorService Members
public void DropDownControl(Control control)
{
}
public void CloseDropDown()
{
}
public System.Windows.Forms.DialogResult ShowDialog(Form dialog)
{
return dialog.ShowDialog();
}
#endregion
}
#endregion
#region ITypeDescriptorContext Members
public void OnComponentChanged()
{
}
public IContainer Container
{
get { return null; }
}
public bool OnComponentChanging()
{
return true; // true to keep changes, otherwise false
}
public object Instance
{
get { return null; }
}
public PropertyDescriptor PropertyDescriptor
{
get { return null; }
}
#endregion
}
于 2010-09-28T20:02:16.173 に答える
1
コメントできないので、ここに投稿します:
DialogResultを取得するには、WindowsFormsEditorService.ShowDialogのCollectionEditorのokButtonにClickイベントを追加します。
public System.Windows.Forms.DialogResult ShowDialog(Form dialog)
{
((System.Windows.Forms.Button)dialog.Controls.Find("okButton", true)[0]).Click += WindowsFormsEditorService_Click;
return dialog.ShowDialog();
}
..。
private void WindowsFormsEditorService_Click(object sender, EventArgs e)
{
dr = DialogResult.OK;
}
于 2017-10-05T19:30:14.967 に答える