我在WPF XML文件中有以下网格
<Grid Grid.RowSpan="2" >
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height = "Auto" />
</Grid.RowDefinitions >
<telerik:RadAutoCompleteBox TextSearchMode = "Contains" Grid.Column= "0" Grid.Row= "0" Name="cmbStyleNo" Margin= "5"
DisplayMemberPath="SAMPLE_ID" ItemsSource="{Binding Styles}"
SelectionMode= "Single" AutoCompleteMode= "Suggest" NoResultsContent= "No Matches" SelectionChanged= "val_SelectionChanged" />
</Grid >
RadAutoCompleteBox SelectionChanged =&#34; val_SelectionChanged&#34;代码隐藏方法存在于其代码隐藏文件中,如下所示
private void val_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
...
}
然后我要求使用资源字典在另一个窗口中填充相同的网格。所以我将该网格复制到 ResourceDictionary ,如下所示
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:samplePrjkt"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
<ToolBar x:Key="MyToolbar" Height="120">
<!--Template-->
<GroupBox Header="Template" Style="{StaticResource ToolbarGroup}" Margin="3">
<StackPanel Grid.Row="1" Orientation="Horizontal">
<StackPanel Orientation="Vertical" Margin="0,2,0,2">
<Grid Grid.RowSpan="2" >
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<telerik:RadAutoCompleteBox TextSearchMode="Contains" Grid.Column="0" Grid.Row="0" Name="cmbStyleNo" Margin="5"
DisplayMemberPath="SAMPLE_ID" ItemsSource="{Binding Styles}"
SelectionMode="Single" AutoCompleteMode="Suggest" NoResultsContent="No Matches" SelectionChanged="val_SelectionChanged"/>
</Grid>
</StackPanel>
</StackPanel>
</GroupBox>
</ToolBar>
</ResourceDictionary>
然后,一旦我编译了这个,我就会得到以下错误
&#39;的ResourceDictionary&#39; root元素需要x:Class属性 支持XAML文件中的事件处理程序。删除事件 SelectionChanged事件的处理程序,或添加x:Class属性 根元素。
答案 0 :(得分:0)
您需要为ResourceDictionary
手动添加代码隐藏文件,并在此文件中定义val_SelectionChanged
事件处理程序。
有关更多信息,请参阅以下链接以及如何执行此操作的示例:
Is it possible to set code behind a resource dictionary in WPF for event handling?
你基本上只是创建一个新类并将其命名为&#34; Dictionary1.xaml.cs&#34;在哪里&#34; Dictionary1&#34;是资源字典XAML文件的名称,然后在XAML文件中设置x:Class
属性:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication1.Dictionary1"
...