gpt4 book ai didi

.net - XamlReader.Load找不到静态资源

转载 作者:行者123 更新时间:2023-12-03 10:49:17 26 4
gpt4 key购买 nike

在运行时使用DataTemplateSelector创建列。 XamlReader.Load处有异常:System.Windows.StaticResourceExtension。如何加载资源?

var column = new DataGridTemplateColumn();
column.Header = (i.Hour + 1).ToString();
column.Width = 30;
var parserContext = new ParserContext();
parserContext.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
parserContext.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
parserContext.XmlnsDictionary.Add("local", "clr-namespace:ССПИ;assembly=ССПИ");
column.CellTemplateSelector = (DataTemplateSelector)XamlReader.Load(
new MemoryStream(Encoding.Default.GetBytes(
@"<local:ExtendedTemplateSelector TextBlockTemplate='{StaticResource TextBlockTemplate}' CheckBoxTemplate='{StaticResource CheckBoxTemplate}'/>"
)),parserContext);
dataGrid1.Columns.Add(column);

<DataGrid DataGridCell.Selected="DataGrid_GotFocus" EnableColumnVirtualization="true" EnableRowVirtualization="true" Name="dataGrid1" DockPanel.Dock="Top" AutoGenerateColumns="False"
Height="120" Width="Auto" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserAddRows="false" CanUserResizeRows="False" CanUserSortColumns="False" CellEditEnding="dataGrid1_CellEditEnding" ContextMenuOpening="dataGrid1_ContextMenuOpening">
<DataGrid.Resources>
<DataTemplate x:Key="TextBlockTemplate">
<TextBlock/>
</DataTemplate>
<DataTemplate x:Key="CheckBoxTemplate">
<CheckBox IsChecked="False"/>
</DataTemplate>
</DataGrid.Resources>

最佳答案

在您的App.xaml中:

<App.Resources>
<local:ExtendedTemplateSelector x:Key="MyExtendedTemplateSelector" TextBlockTemplate='{StaticResource TextBlockTemplate}' CheckBoxTemplate='{StaticResource CheckBoxTemplate}'/>
</App.Resources>

在您的.cs中:
column.CellTemplateSelector = (DataTemplateSelector)((App)Application.Current)).Resources["MyExtendedTemplateSelector"];

据我所知,这里不需要解析器:它只会使事情变得复杂而缓慢。因此,只需将您的资源放入您的APP资源(或resourceDictionary)中,然后像这样调用它

关于.net - XamlReader.Load找不到静态资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11465039/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com