2016-05-23 16 views
1

:場合WPF ComboBoxからDataRowViewを直列化および逆シリアル化できますか?私はそのような列「Listkey」および「Listvalue」を含むデータテーブルを用いてコードビハインドでWPFコンボボックスののItemsSourceを充填してい

SetElementProperty(element, "ItemsSource", (new ListtablesRead()).ReadListtable(changeTextProperties.SelectedListTable).DefaultView); 
SetElementProperty(element, "DisplayMemberPath", "Listvalue"); 
SetElementProperty(element, "SelectedValuePath", "Listkey"); 

SetElementPropertyは、反射によって確認する方法でありますframeworkelement(その場合はコンボボックス)は、指定されたプロパティを持ち、それを設定します。

次に、私はXmlWriterでコントロールをシリアル化したいと思います。 は、だから私はDataRowView型用のコンバータクラスを書いた:

using System; 
using System.ComponentModel; 
using System.Data; 
using System.Windows; 
using System.Windows.Markup; 

namespace WPFDesignerConverterLibrary 
{ 
    public class DataRowViewConverter : ExpressionConverter 
    { 
     /// <summary> 
     /// Finds out if the converter can convert an expression-object to the given destinationtype. 
     /// </summary> 
     /// <param name="context">An ITypeDescriptorContext-interface which provides a context for formatting.</param> 
     /// <param name="destinationType">A type-class which represents the target-type of the conversion.</param> 
     /// <returns>Returns an object of type bool. True = the destinationtype can be converted.</returns> 
     public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
     { 
      if (destinationType == typeof(MarkupExtension)) 
       return true; 

      return false; 
     } 

     /// <summary> 
     /// Converts the expression to the given destinationtype. 
     /// </summary> 
     /// <param name="context">An ITypeDescriptorContext-interface which provides a context for formatting.</param> 
     /// <param name="culture">The System.Globalization.CultureInfo which is actually used as culture.</param> 
     /// <param name="value">The object to convert.</param> 
     /// <param name="destinationType">A type-class which represents the target-type of the conversion.</param> 
     /// <returns></returns> 
     public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, 
      Type destinationType) 
     { 
      if (destinationType == typeof(MarkupExtension)) 
      { 
       DataRowView datarowview = value as DataRowView; 
       if (datarowview == null) 
        throw new Exception(); 
       return datarowview.Row; 
      } 

      return base.ConvertTo(context, culture, value, destinationType); 
     } 
    } 
} 

このコンバータの動作とシリアル化されたXMLに次の行を生成します。

<sd:DataRow RowError=""> 
    <sd:DataRow.ItemArray> 
     <x:Array Type="s:Object" xml:space="preserve"><s:String>01</s:String><s:String>Ersttest      </s:String></x:Array> 
    </sd:DataRow.ItemArray> 
    </sd:DataRow> 
    <sd:DataRow RowError=""> 
    <sd:DataRow.ItemArray> 
     <x:Array Type="s:Object" xml:space="preserve"><s:String>02</s:String><s:String>Wiederholungstest    </s:String></x:Array> 
    </sd:DataRow.ItemArray> 
    </sd:DataRow> 
    <sd:DataRow RowError=""> 
    <sd:DataRow.ItemArray> 
     <x:Array Type="s:Object" xml:space="preserve"><s:String>03</s:String><s:String>Konstanzprüfung    </s:String></x:Array> 
    </sd:DataRow.ItemArray> 
    </sd:DataRow> 

しかし、私は取得シリアル化されたXMLをリロードしようとすると、 DataRow型の標準コンストラクタが見つからないというエラーメッセージが表示されます。 どうしたのですか? さらに:データテーブルのdefaultviewでコンボボックスのitemssourceを設定するのがこれを行う最も簡単な方法ですが、別の方法をとらなければならないのでしょうか?

InnerException: = -2146233069 HResult メッセージ= "System。Data。DataRow"タイプの標準コンストラクタが見つかりませんでした。型は引数またはFactoryMethodディレクティブで指定できます。 ソース=システム。スタックトレース: at system。 Xaml。パターン。 XamlTypeInvoker。 DefaultCtorXamlActivator。 EnsureConstructorDelegate(XamlTypeInvoker type) をシステムにインストールします。 Xaml。パターン。 XamlTypeInvoker。 CreateInstance(引数のObject []) (MS.Internal)。 Xaml。ランタイム。 ClrObjectRuntime。 CreateInstanceWithCtor(XamlType xamlType、Object [] args) at MS.Internal。 Xaml。ランタイム。 ClrObjectRuntime。 CreateInstance(XamlType xamlType、Object [] args)

+1

を使用します。 'DataRow'コンストラクタは' private'ですから。異なるデータ型を使用する必要があります。 – XAMlMAX

+0

ありがとうございますXAMIMAX。つまり、コンボボックスを塗りつぶしてシリアライズしなければならないということです。あなたは私にヒントをお願いできますか? –

+0

適切なプロパティを持つクラスを作成する必要があります。その場合は2にすぎず、パブリックなパラメータのないコンストラクタが必要です。あなたの質問に答える前に、どのようにデータを取得するのですか?それはあなたがそれを直列化する前ですか?ありがとう – XAMlMAX

答えて

0

一方私は別の方法を見つけました。 は私がKeyAndValueと呼ばれるダミーのクラスを使用しています:少し1 diffenrenceでKeyValuePairを使用したようなものだ

foreach (System.Data.DataRow row in table.Rows) 
    { 
     // Add new pairs with Listkey and Listvalue as content to the Items-collection. 
     customComboBox.Items.Add(new KeyAndValue() { Key = row["Listkey"].ToString(), Value = row["Listvalue"].ToString() }); 
    } 

:そのクラスは、コンボボックスのアイテムコレクションを埋めるのに役立ちます

/// <summary> 
/// Dummy class with key and value properties for construction of comboboxitems. 
/// </summary> 
public class KeyAndValue 
{ 
    /// <summary> 
    /// Gets or sets the key. 
    /// </summary> 
    public string Key { get; set; } 

    /// <summary> 
    /// Gets or sets the value. 
    /// </summary> 
    public string Value { get; set; } 
} 

: KeyValuePairのような汎用リストは直列化できません。 しかし、私のダミークラスはそれを行います。

+0

これは基本的に私の答えがどうなるか – XAMlMAX

0

あなたが必要とするのは、Serializationを可能にするパラメーターのない管理人別名Default Constructorのクラスを持つことです。
手順1. データベースの結果を保持するclassを作成します。

  • 手動コードで行うこともできますが、DataSetを使用することもできます。

ステップ2.
クラスを作成した後、またはデータセットを使用してシリアル化しました。

FileStream fs = new FileStream(savePath + "\\" + a.Location + ".bin", FileMode.Create); 
BinaryFormatter serializer = new BinaryFormatter(); 
try 
{ 
    serializer.Serialize(fs, a); 
} 
catch (Exception e) 
{ 
    //handle your fail; 
} 
finally 
{ 
    fs.Close(); 
} 

またはそれをデシリアライズ:

List<Model.YOURCLASS> _l = new List<Model.YOURCLASS>(); 
string[] files = Directory.GetFiles(pathToSearch); 
FileStream fs; 
BinaryFormatter deserializer = new BinaryFormatter(); 
foreach (var a in files) 
{ 
    if(Path.GetExtension(a) == ".bin") 
    { 
     fs = new FileStream(a, FileMode.Open); 
     _l.Add((Model.YOURCLASS)deserializer.Deserialize(fs)); 
    } 
} 
return _l; 

あなたのリターンがDataTableあなたListViewである必要はありませんタイプが自動的にそれを渡す任意のコレクション、だとしてもarray[]

関連する問題