Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF使用数据模板中的转换器,该模板由phone 8上的xaml文本和XamlReader创建_C#_Wpf_Xaml_Windows Phone - Fatal编程技术网

C# WPF使用数据模板中的转换器,该模板由phone 8上的xaml文本和XamlReader创建

C# WPF使用数据模板中的转换器,该模板由phone 8上的xaml文本和XamlReader创建,c#,wpf,xaml,windows-phone,C#,Wpf,Xaml,Windows Phone,我正在使用文本xaml生成一个数据模板,如下所示: var dataTemplate = (DataTemplate)XamlReader.Load(xaml.ToString()); - InnerException {System.Exception: Error HRESULT E_FAIL has been returned from a call to a COM component. at MS.Internal.XcpImports.CheckHResult(

我正在使用文本xaml生成一个数据模板,如下所示:

var dataTemplate = (DataTemplate)XamlReader.Load(xaml.ToString());
-       InnerException  {System.Exception: Error HRESULT E_FAIL has been returned from a call to a COM component.
   at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
   at MS.Internal.XcpImports.UIElement_Measure_WithDesiredSize(UIElement element, Size availableSize)
   at System.Windows.UIElement.Measure_WithDesiredSize(Size availableSize)
   at System.Windows.Controls.VirtualizingStackPanel.MeasureChild(UIElement child, Size layoutSlotSize)
   at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
   at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)}   System.Exception
其中一个元素是与指向静态资源的转换器绑定的复选框:

    StringBuilder xaml = new StringBuilder();
    xaml.AppendFormat(@"          <CheckBox");
    xaml.AppendFormat(@" IsChecked=""{{Binding [{0}], Converter={{StaticResource stringValueToBoolConverter}}, Mode=TwoWay}}""", header.ID);
    xaml.AppendFormat(@" />");
模板用于在列表框中创建一行,控件的资源中声明了转换器:

<UserControl.Resources>
        <converters:StringValueToBoolConverter x:Key="stringValueToBoolConverter" />
</UserControl.Resources>

我假设在某个创建步骤中,xaml->objects的对象创建过程找不到转换器-有办法绕过它吗

**加成

我还测试了为复选框添加一个Loaded-与添加转换器相同的断言

StringBuilder xaml = new StringBuilder();
xaml.AppendFormat(@"          <CheckBox");
xaml.AppendFormat(@" IsChecked=""{{Binding [{0}],  Mode=TwoWay}}""", header.ID);
xaml.AppendFormat(@" Loaded=""FrameworkElement_OnLoaded""");
StringBuilder xaml=new StringBuilder();
xaml.AppendFormat(@)
模板用于在列表框中创建一行,控件的资源中声明了转换器:

<UserControl.Resources>
        <converters:StringValueToBoolConverter x:Key="stringValueToBoolConverter" />
</UserControl.Resources>

为什么不将数据加载到预加载的列表框中,并使用ItemsSource绑定到从您的文件加载的集合?并使用可见性转换器显示/隐藏日期。您所做的工作与实际工作相比似乎不必要地复杂。

您能详细说明一下吗?我正在尝试使用一个列表框创建一个类似datagrid的UI,其中每个列表框ox项是一个单行网格。在编译时,列数和数据类型未知。任何列都可以是bool或字符串类型,如果是bool,我需要管理数据的转换(复选框)stringbuilder实际上是在重建xaml并将其注入控件中。将其解析为ienumerable并将其用作ItemsSource={binding BoolienumerablewhateverThisPropertyCalled}在datatemplate组件内,访问所述属性也是有效的。然后,可以通过调用相应的Visibly容器在屏幕上绘制数据模板来激活该数据模板。有关datagrids的更多信息,请参见[链接]和[链接]中的datatemplate字符串生成器正在创建由一些模式数据确定的复选框或文本框元素-实际的原始数据都是字符串,因此我都需要创建正确的ui元素,然后绑定到数据(如果字符串数据被认为是布尔值,则使用转换器)但这在设计时是未知的,我得到的行数据是