c# - 动态生成的组合框值

标签 c# wpf combobox wpftoolkit propertygrid

是否可以动态生成(例如从数据库获取)嵌入 WPF 工具包 PropertyGrid 中的组合框项目?我找到了以下代码,但它生成固定值。

public class Person
{
    [ItemsSource(typeof(FontSizeItemsSource))]
    public double WritingFontSize { get; set; }
}

public class FontSizeItemsSource : IItemsSource
{
    public ItemCollection GetValues()
    {
        ItemCollection sizes = new ItemCollection();
        sizes.Add(5.0, "Five");
        sizes.Add(5.5);
        return sizes;
    }
}

最佳答案

您可以设置自己的编辑模板,并将项目提供给其中的 ComboBox,并绑定(bind)到 ItemsSource:

public class Person
{
    public double WritingFontSize { get; set; }

    public ObservableCollection<double> FontSizeItemsSource
    {
        get
        {
            ObservableCollection<double> sizes = new ObservableCollection<double>();

            // Items generation could be made here
            sizes.Add(5.0);
            sizes.Add(5.5);
            return sizes;
        }

    }
}


    <xctkpg:PropertyGrid SelectedObject="{Binding MyPersonObject}" AutoGenerateProperties="False">
        <xctkpg:PropertyGrid.EditorDefinitions>
            <xctkpg:EditorTemplateDefinition TargetProperties="WritingFontSize">
                <xctkpg:EditorTemplateDefinition.EditingTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Instance.FontSizeItemsSource}" SelectedValue="{Binding Instance.WritingFontSize}" />
                    </DataTemplate>
                </xctkpg:EditorTemplateDefinition.EditingTemplate>
            </xctkpg:EditorTemplateDefinition>
        </xctkpg:PropertyGrid.EditorDefinitions>

        <xctkpg:PropertyGrid.PropertyDefinitions>
            <xctkpg:PropertyDefinition TargetProperties="WritingFontSize" />
        </xctkpg:PropertyGrid.PropertyDefinitions>
    </xctkpg:PropertyGrid>

关于c# - 动态生成的组合框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35162923/

相关文章:

C# Accord.net。文本分类

WPF:如何开始为使用 MVVM 构建的 DataGrid 设置样式?

wpf - 使用 MEF 时创建 subview 模型

c++ - 我如何使用与 QVariant 相关的 QComboBox?

C# databound ComboBox 更改其他控件中的数据

C# 如何从 VS2008 代码指标中排除生成的代码?

c# - Entity Framework 、依赖注入(inject)和共享对象

c# - WPF - ComboBox SelectedItem 未绑定(bind)

javascript - 从 C# 后端发送脚本到浏览器

c# - 是否可以将用户在 WPF 数据网格上输入的数据存储到字符串变量中?