从WPF中的资源字典访问codebehind方法

从WPF中的资源字典访问codebehind方法,wpf,telerik,code-behind,telerik-grid,resourcedictionary,Wpf,Telerik,Code Behind,Telerik Grid,Resourcedictionary,我在WPF XML文件中有以下网格 <Grid Grid.RowSpan="2" > <Grid.ColumnDefinitions> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height = "Auto" /> </Grid.R

我在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 >
然后,我使用资源字典在另一个窗口中的相同网格上填充需求。所以我将网格复制到ResourceDictionary中,如下所示

private void val_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ...
}
<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>
一旦我编译了这个,我就会得到以下错误

“ResourceDictionary”根元素需要一个x:Class属性来 支持XAML文件中的事件处理程序。或者删除该事件 SelectionChanged事件的处理程序,或向 根元素


您需要手动为ResourceDictionary添加代码隐藏文件,并在此文件中定义val_SelectionChanged事件处理程序

请参阅以下链接以了解更多信息和如何执行此操作的示例:

基本上只需创建一个新类并将其命名为Dictionary1.xaml.cs,其中Dictionary1是资源字典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"
...

我是否可以提供相同的代码隐藏文件,而不创建新的类文件?否。您应该创建一个新的代码隐藏类,并将事件处理程序复制到此类。为此,我是否可以使用用户控件并将网格嵌入其中,并将该用户控件称为资源字典中的用户控件?如果您有其他问题,请提出新问题。不要在评论栏中问其他问题。不,我是在问新方法是否适合这个目的,客观-在不同的窗口中用相同的下拉列表填充网格。方法-我可以使用用户控件并将网格嵌入其中,然后在资源字典中调用该用户控件吗?