我使用a Extended WPF Toolkit的Property Grid控件,需要显示通过REST调用动态加载的帐户列表。 Enum类型在ComboBox编辑器中显示良好,而动态列表确实显示类型名称而不是Item类的DisplayName。我使用了ItemsSourceAttribute并尝试通过import tkinter as tk
import subprocess
import time
class App:
def __init__(self, master):
self.master = master
self.saveBtn = tk.Button(self.master, text="Save", command=self.saveFn)
self.saveBtn.pack()
def saveFn(self):
try:
oldFile = open('log.txt', 'r')
oldFileContent = oldFile.read()
oldFile.close()
newFile = open('log.txt', 'w')
newFile.write(oldFileContent + '\n' + str(time.asctime()) + ';')
newFile.close()
except:
file = open('log.txt', 'w')
file.write(str(time.asctime()) + ';')
file.close()
root = tk.Tk()
myApp = App(root)
root.mainloop()
显式编辑,但两者都不起作用。
以下是相应的代码部分。
MainWindow.xaml
[Editor(typeof(ItemsSourceAttributeEditor), typeof(ItemsSourceAttributeEditor))]
MainWindow.xaml.cs
<Window x:Class="PropertyGridXceedDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="600">
<Grid>
<xctk:PropertyGrid Margin="0,62,0,35" Name="control" AutoGenerateProperties="True" ShowSortOptions="False"/>
</Grid>
</Window>
Settings.cs
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
this.control.SelectedObject = new Settings();
}
}
AccountsItemSource.cs
[Serializable]
[DisplayName("Settings")]
public class Settings
{
public Settings()
{
_mAccounts = new List<string>();
}
private DefaultOrderTypes _mOrderType= (DefaultOrderTypes)1;
private string _account = "Account-1";
private List<string> _mAccounts;
public enum DefaultOrderTypes
{
OrderType1,
OrderType2
};
[Category("General"), Description("Select order type")]
[Browsable(true)]
[DisplayName("Enum List")]
public DefaultOrderTypes DefaultOrderType
{
get
{
return _mOrderType;
}
set
{
_mOrderType = value;
}
}
[Category("General"), Description("Select account to use")]
[DisplayName("Select Account")]
[Editor(typeof(ItemsSourceAttributeEditor), typeof(ItemsSourceAttributeEditor))]
[ItemsSource(typeof(AccountsItemSource))]
public string Account
{
get
{
return _account;
}
set
{
_account = value;
}
}
}
输出显示如下:
[Serializable]
public class AccountsItemSource : IItemsSource
{
public Xceed.Wpf.Toolkit.PropertyGrid.Attributes.ItemCollection GetValues()
{
Xceed.Wpf.Toolkit.PropertyGrid.Attributes.ItemCollection c = new Xceed.Wpf.Toolkit.PropertyGrid.Attributes.ItemCollection();
c.Add(new Item { DisplayName = "Account-1",Value="Account-1" });
c.Add(new Item { DisplayName = "Account-2", Value = "Account-2" });
return c;
}
}
[Serializable]
public class Account
{
public Account(string name)
{
this.Name = name;
}
public string Name { get; set; }
public override bool Equals(object obj)
{
if (obj == null) return false;
if (Object.ReferenceEquals(this, obj)) return true;
if ( !(obj is Account) ) return false;
return string.Compare(this.Name, ((Account)obj).Name, StringComparison.OrdinalIgnoreCase) == 0;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
public override string ToString()
{
return this.Name;
}
}
请指教。如果需要,我可以通过电子邮件发送示例应用程序。
最佳, 禾企