コードビハインドでDataTemplateをロードしようとしていますが、コンバーターを削除するとうまく動作しますが、そこに置くとすぐに吹きます。さて、私は自分の状態空間をネームスペースに設定し、XAMLのコンバータへの参照を配置しました。例えば:コード内のコンバーターを使用したDataTemplate
<Window.Resources>
<local:StatCellConverter x:Key="myConverter" />
</Window.Resources>
、これは私がするDataTemplateを生成する私の方法である:私は間違っ
private DataTemplate GenerateStatRowDataTemplate()
{
ParserContext pc = new ParserContext();
pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
pc.XmlnsDictionary.Add("xcdg", "http://schemas.xceed.com/wpf/xaml/datagrid");
string statRowTemplate = "<DataTemplate><xcdg:StatRow>";
statRowTemplate += "<xcdg:StatCell FieldName=\"Column4\" ResultPropertyName=\"AvgColumn4\">";
statRowTemplate += "<xcdg:StatCell.ContentTemplate>";
statRowTemplate += "<DataTemplate>";
statRowTemplate += "<TextBlock Text=\"{Binding ., Converter={StaticResource ResourceKey=myConverter}}\" />";
statRowTemplate += "</DataTemplate>";
statRowTemplate += "</xcdg:StatCell.ContentTemplate>";
statRowTemplate += "</xcdg:StatCell>";
statRowTemplate += "</xcdg:StatRow>";
statRowTemplate += "</DataTemplate>";
StringReader stringReader = new StringReader(statRowTemplate);
XmlReader xmlReader = XmlReader.Create(stringReader);
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(statRowTemplate.ToString()));
DataTemplate dt = (DataTemplate)XamlReader.Load(ms,pc);
dt.LoadContent();
return dt;
}
何をしているのですか?私のコンバーターもコードの中に定義しなければならないのでしょうか?
マイコンバータ
public class StatCellConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Debug.WriteLine(value);
if (value.Equals("#DIV/0#"))
return "0";
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
私はそれが実際にはDataTemplateを
あなたのごコンバータを投稿し、「それを定義してください吹く " – Paparazzi
私は質問を編集しました –
私はそれを理解しました。コードの後ろにリソースを設定する必要がありました... string statRowTemplate = ""; statRowTemplate + = " DataTemplate.Resources>"; –