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)
を使用します。 'DataRow'コンストラクタは' private'ですから。異なるデータ型を使用する必要があります。 – XAMlMAX
ありがとうございますXAMIMAX。つまり、コンボボックスを塗りつぶしてシリアライズしなければならないということです。あなたは私にヒントをお願いできますか? –
適切なプロパティを持つクラスを作成する必要があります。その場合は2にすぎず、パブリックなパラメータのないコンストラクタが必要です。あなたの質問に答える前に、どのようにデータを取得するのですか?それはあなたがそれを直列化する前ですか?ありがとう – XAMlMAX