WPF C#将字典绑定到DataGrid中的Combox

时间:2016-05-23 22:12:15

标签: c# wpf xaml datagrid

我正在尝试将字典中的值绑定到DataGrid中的组合框。但是,组合框中没有显示任何内容,我收到以下输出:

System.Windows.Data Error: 35 : ObjectDataProvider: Failure trying to invoke method on type; Method='getConditions'; Type='databaseAccess'; Error='The specified member cannot be invoked on target.' TargetException:'System.Reflection.TargetException: Non-static method requires a target.
   at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at System.Windows.Data.ObjectDataProvider.InvokeMethodOnInstance(Exception& e)'
System.Windows.Data Error: 35 : ObjectDataProvider: Failure trying to invoke method on type; Method='getConditions'; Type='databaseAccess'; Error='The specified member cannot be invoked on target.' TargetException:'System.Reflection.TargetException: Non-static method requires a target.
   at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at System.Windows.Data.ObjectDataProvider.InvokeMethodOnInstance(Exception& e)'

如果答案很明显,我很抱歉 - 我对WPF很新。我的课程:

namespace Leisurely_Diversion_Interface
{

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
           DataSet ds = new DataSet();
           databaseAccess access = new databaseAccess(ConfigurationManager.ConnectionStrings["leisurelyDiversion"].ConnectionString);

           Dictionary<int, String> conditionMap = new Dictionary<int, string>();
           conditionMap = access.getConditions();

            ds.Tables.Add(access.getTable("stock"));

            itemGrid.ItemsSource = ds.Tables[0].DefaultView;
        }
    }
}

databaseAccess.cs

namespace Leisurely_Diversion_Interface
{
    class databaseAccess
    {
        private MySqlConnection con = new MySqlConnection();
        private MySqlDataAdapter adapter = new MySqlDataAdapter();



        public databaseAccess(String connectionString)
        {
            try {         
                con.ConnectionString = connectionString;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
        }

        private bool connect()
        {
            try
            {
                con.Open();
                return true;
            } catch(MySqlException e) {
                Console.WriteLine(e.StackTrace);
                return false;
            }
        }

        public DataTable getTable(String tableName)
        {
            if (this.connect())
            {
                DataTable table = new DataTable();
                MySqlCommand command = new MySqlCommand("SELECT * FROM " + tableName, con);
                MySqlDataAdapter adapter = new MySqlDataAdapter(command);
                adapter.Fill(table);

                con.Close();
                return table;

            }
            con.Close();
            return null;
        }

        public Dictionary<int, String> getConditions()
        {
            Dictionary<int, string> conditionMap = new Dictionary<int, string>();
            if (this.connect())
            {
                try
                {
                    MySqlCommand com = new MySqlCommand("SELECT * FROM conditions", con);
                    MySqlDataReader reader = com.ExecuteReader();
                    while (reader.Read())
                    {
                        conditionMap.Add((int)reader["conditionID"], (string)reader["name"]);


                    }
                    con.Close();
                    return conditionMap;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                }
            }
            con.Close();
            return null;
        }

        public Dictionary<int, string> getCategories()
        {
            Dictionary<int, string> categoryMap = new Dictionary<int,string>();
            MySqlCommand com = new MySqlCommand("SELECT * FROM categories", con);
            MySqlDataReader reader = com.ExecuteReader();
            while (reader.Read())
            {
                categoryMap.Add((int)reader["id"], (string)reader["name"]);


            }
            return categoryMap;
        }

    }
}

和我的XAML

<Window x:Class="Leisurely_Diversion_Interface.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:Leisurely_Diversion_Interface"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1">

    <Window.Resources>
        <ObjectDataProvider x:Key="conDictionary" ObjectType="{x:Type local:databaseAccess}" MethodName="getConditions" />
    </Window.Resources>
    <StackPanel>
        <DataGrid x:Name="itemGrid" ItemsSource="{Binding}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding name}"></DataGridTextColumn>
                <DataGridTextColumn Header="Price" Binding="{Binding price}"></DataGridTextColumn>
                <DataGridTemplateColumn Header="Condition">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox  ItemsSource="{Binding Source={StaticResource conDictionary}}" SelectedValuePath="Key" DisplayMemberPath="Value"></ComboBox>
                    </DataTemplate>

                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTextColumn Header="Image Path" Binding="{Binding image}"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>


</Window>

非常感谢任何指导!

0 个答案:

没有答案