【问题标题】:How to bind an combobox to an extended class with type?如何将组合框绑定到具有类型的扩展类?
【发布时间】:2019-02-12 10:16:43
【问题描述】:

我在 wpf 中有一个组合框,想将它绑定到对象的属性。我的问题是,这个属性是一个基类,可以是两种具体类型。

这些是我的课程:

    public abstract class Database
    {
        public DBTypes Type { get; set; }
        public abstract void connect();

        public abstract void disconnect();

        public abstract void initDB();


        public Database()
        {

        }
    }

 public class OracleDB : Database
    {
        public string Sid { get; set; }
        public string User { get; set; }
        public string Password { get; set; }

        private OracleConnection m_dbConnection;

        public OracleDB()
        {
            Type = DBTypes.ORACLE;
        }

        public OracleDB( string sid, string user, string passwd )
        {
            Sid = sid;
            User = user;
            Password = passwd;
        }
        .
        . 
        .
}

    public class SqliteDB : Database
    {
        public string DBFile { get; set; }

        private SQLiteConnection m_dbConnection;

        public SqliteDB()
        {
            DBFile = "database.db";
            Type = DBTypes.SQLITE;
        }

        .
        .
     }
}

 public enum DBTypes
    {
        ORACLE,
        SQLITE
    }

WPF 部分是:

<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type System:Enum}" x:Key="EnumValues">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="lib:DBTypes" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

.
.
.

<ComboBox x:Name="DBTyp"  Grid.Column="2" Grid.Row="0" HorizontalAlignment="Stretch" 
                      VerticalAlignment="Center" SelectedIndex="0" 
                      ItemsSource="{Binding Source={StaticResource EnumValues}}"
                      SelectedValue="{Binding Path=DbSettings.Type}"
                      SelectedItem="{Binding Path=DbSettings}">
            </ComboBox>

DbSettings 是数据库类型的属性。

那么,当从组合框中选择另一个条目时,我该如何实现,选择正确的对象?

我的代码得到了

Cannot convert 'SQLITE' from type 'DBTypes' to type 'Database' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: TypeConverter kann nicht von DBTypes konvertieren.

【问题讨论】:

  • 什么是 DbSettings 的类型。如果它的类型是数据库,您应该删除 SelectedItem="{Binding Path=DbSettings}"。那是导致错误的原因。如果我理解正确,您正在尝试在 DbSettings 上设置 Type 属性。
  • 是的,DbSettings 的类型是数据库。我读你的答案太晚了,但它与@mm8 提到的方向相同。 SelectedItem 和 SelectedValue 不能同时存在。

标签: c# wpf combobox binding


【解决方案1】:

将SelectedItem 属性绑定到DbSettings.Type:

<ComboBox x:Name="DBTyp" Grid.Column="2" Grid.Row="0" HorizontalAlignment="Stretch" 
          VerticalAlignment="Center" SelectedIndex="0" 
          ItemsSource="{Binding Source={StaticResource EnumValues}}"
          SelectedItem="{Binding Path=DbSettings.Type}">
</ComboBox>

您不应同时绑定SelectedValue 和SelectedItem。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多