XP上XamlReader读取XAML失败的问题

2022-09-29 08:06:06 浏览数 (1)

项目中DataGrid需要合并列,开始还以为XP不支持WPF动态模版创建,后来发现是XP上不支持绑定数据的格式化(加¥),把StringFormat={}{0:C}, ConverterCulture=zh-CN去掉,就可以正常绑定数据了

代码语言:javascript复制
 StringBuilder cellTemp = new StringBuilder();
            cellTemp.Append("<DataTemplate ");
            cellTemp.Append(" xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'");
            cellTemp.Append(" xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
            cellTemp.Append(" xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit" ");
            cellTemp.Append("   >");
            cellTemp.Append("<Grid Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=toolkit:DataGridCell},Path=ActualWidth}" ");
            cellTemp.Append("     Height="{Binding RelativeSource={RelativeSource AncestorType=toolkit:DataGridColumnHeader},Path=ActualHeight}"  >");
            cellTemp.Append("		<Grid.ColumnDefinitions>");
            cellTemp.Append("			<ColumnDefinition Width="0.16*"></ColumnDefinition>");
            cellTemp.Append("			<ColumnDefinition Width="0.34*"></ColumnDefinition>");
            cellTemp.Append("			<ColumnDefinition Width="0.16*"></ColumnDefinition>");
            cellTemp.Append("			<ColumnDefinition Width="0.34*"></ColumnDefinition>");
            cellTemp.Append("		</Grid.ColumnDefinitions>");
            cellTemp.Append("		<TextBlock Text="¥" HorizontalAlignment="Right" VerticalAlignment="Center"/>");
            cellTemp.Append("		<TextBlock Grid.Column="1"  Text="{Binding "   bindFieldSale   "}" HorizontalAlignment="Left" VerticalAlignment="Center">");
            cellTemp.Append("           <TextBlock.ToolTip >");
            cellTemp.Append("               <ToolTip DataContext="{Binding Path=PlacementTarget,RelativeSource={x:Static RelativeSource.Self}}">");
            cellTemp.Append("                   <WrapPanel>");
            cellTemp.Append("                       <TextBlock Text="¥"/>");
            cellTemp.Append("                       <TextBlock Text="{Binding  Path=Text}"/>");
            cellTemp.Append("                   </WrapPanel>");
            cellTemp.Append("               </ToolTip>");
            cellTemp.Append("         </TextBlock.ToolTip >");
            cellTemp.Append("       </TextBlock>");
            cellTemp.Append("		<TextBlock Grid.Column="2"  Text="¥" HorizontalAlignment="Right" VerticalAlignment="Center"/>");
            cellTemp.Append("		<TextBlock Grid.Column="3" Text="{Binding " bindFieldPercentage "}" HorizontalAlignment="Left" VerticalAlignment="Center">");
            cellTemp.Append("           <TextBlock.ToolTip>");
            cellTemp.Append("               <ToolTip DataContext="{Binding Path=PlacementTarget,RelativeSource={x:Static RelativeSource.Self}}">");
            cellTemp.Append("                   <WrapPanel>");
            cellTemp.Append("                       <TextBlock Text="¥"/>");
            cellTemp.Append("                       <TextBlock Text="{Binding  Path=Text}"/>");
            cellTemp.Append("                   </WrapPanel>");
            cellTemp.Append("               </ToolTip>");
            cellTemp.Append("           </TextBlock.ToolTip>");
            cellTemp.Append("       </TextBlock>");
            cellTemp.Append("</Grid>");
            cellTemp.Append("</DataTemplate>");
            DataTemplate dt = null;
dt = (DataTemplate)System.Windows.Markup.XamlReader.Parse(cellTemp.ToString());//最好加异常处理

然后加TextBlock显示“¥”,并当鼠标悬浮时,显示数字

0 人点赞