none
如何正确使用XamlReader通过字符串来构造一个DataGridTextColumn或者一个DataGridTemplateColumn? RRS feed

  • 问题


  • <DataGridTextColumn 
    
    Binding="{Binding name}" Header="追加的列——文本" >
         <DataGridTextColumn.EditingElementStyle>
                      <Style TargetType="{x:Type TextBox}">
                              <Setter Property="Tag" Value="name"></Setter>
                               <EventSetter Event="LostFocus" Handler="DataGridTextColumn_LostFocus" />
                        </Style>
         </DataGridTextColumn.EditingElementStyle>
    </DataGridTextColumn>
    
    //构造的DataGridTextColumn对象的xaml代码模板

          private void Button_Click_22(object sender, RoutedEventArgs e)
            {
               var i= Create();
    
            }
            public DataGridTextColumn Create()
            {
                StringReader stringReader =new StringReader(TestResource.addDataGridCol);
                XmlReader xmlReader = XmlReader.Create(stringReader);
                return XamlReader.Load(xmlReader) as DataGridTextColumn;
            }

    如题,依据上列代码,加载资源文件addDataGridCol字段里面的xaml代码,

    点击按钮试图构造一个DataGridTextColumn,然后出错,错误提示为”XamlObjectWriterException: “XamlObjectWriterException: “无法创建未知类型“DataGridTextColumn”。”,行号为“1”,行位置为“2,这些是什么原因导致,如何解决?

    2019年4月12日 7:18

全部回复

  • Hi,

    你需要加上wpf xaml的命名空间声明,https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/advanced/xaml-namespaces-and-namespace-mapping-for-wpf-xaml

    <DataGridTextColumn 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Binding="{Binding name}" Header="追加的列——文本" >
         <DataGridTextColumn.EditingElementStyle>
                      <Style TargetType="{x:Type TextBox}">
                              <Setter Property="Tag" Value="name"></Setter>
                               <EventSetter Event="LostFocus" Handler="DataGridTextColumn_LostFocus" />
                        </Style>
         </DataGridTextColumn.EditingElementStyle>
    </DataGridTextColumn>
    
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年4月15日 9:53
    版主