When I create a DataTemplate in code I am getting a error when defining the namespaces.
Below is the code I am using
public static DataTemplate CreateDataTemplate(string bindingValue)
{
StringBuilder dataTemplateText = new StringBuilder();
dataTemplateText.Append("<DataTemplate ");
dataTemplateText.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
dataTemplateText.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
dataTemplateText.Append("xmlns:interactivity='http://schemas.microsoft.com/expression/2010/interactivity' ");
// Custom NameSpace
dataTemplateText.Append("xmlns:valueConverter='clr-namespace:AttachedPropertiesTest.ValueConverter;assembly=AttachedPropertiesTest'> ");
dataTemplateText.Append("xmlns:wtf='clr-namespace:AttachedPropertiesTest.wtf;assembly=AttachedPropertiesTest'> ");
//
dataTemplateText.Append("<Grid>");
dataTemplateText.Append("<Grid.Resources>");
dataTemplateText.Append("<valueConverter:RowIndexConverter x:Key='rowIndexConverter' />");
dataTemplateText.Append("</Grid.Resources>");
dataTemplateText.Append("<TextBlock ");
dataTemplateText.Append("Text = '{Binding ");
dataTemplateText.Append("Converter={StaticResource rowIndexConverter}, ConverterParameter=" + bindingValue + " }' >");
dataTemplateText.Append(" <interactivity:Interaction.Triggers> ");
dataTemplateText.Append(" <interactivity:EventTrigger EventName='MouseLeftButtonDown' > ");
dataTemplateText.Append(" <wtf:InvokeDelegateCommandAction CommandName='CellCommand' Command='{Binding CellCommand, Source={StaticResource mainViewModel}}' CommandParameter='{Binding Converter={StaticResource rowIndexConverter}, ConverterParameter=" + bindingValue + " }' /> ");
dataTemplateText.Ap开发者_StackOverflowpend(" </interactivity:EventTrigger> ");
dataTemplateText.Append(" </interactivity:Interaction.Triggers> ");
dataTemplateText.Append("</TextBlock>");
dataTemplateText.Append("</Grid>");
dataTemplateText.Append("</DataTemplate>");
DataTemplate dataTemplate = (DataTemplate)XamlReader.Load(dataTemplateText.ToString());
return dataTemplate;
}
I get the following error:
Failed to create a 'System.Windows.DataTemplate' from the text 'xmlns:wtf='clr-namespace:AttachedPropertiesTest.wtf;assembly=AttachedPropertiesTest'> '.
When I only have one namespace where I set the assembly it works.
I can get my code to work by putting all my code in one Namespace definition. I just am not a fan of this work around.
Does anyone have any idea what I am doing wrong by defining my namespaces.
The first custom namespace contains a closing bracket that should not be there.
dataTemplateText.Append("xmlns:valueConverter='clr-namespace:AttachedPropertiesTest.ValueConverter;assembly=AttachedPropertiesTest'> ");
Should probably be:
dataTemplateText.Append("xmlns:valueConverter='clr-namespace:AttachedPropertiesTest.ValueConverter;assembly=AttachedPropertiesTest' ");
You might try declaring your strings like this:
dataTemplateText = @"
<DataTemplate
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:interactivity='http://schemas.microsoft.com/expression/2010/interactivity'
{more here...}
";
so the values are more readable.
精彩评论