-2

I have following grid inside WPF XML file

<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 >

above RadAutoCompleteBox SelectionChanged="val_SelectionChanged" code behind method exist in its code-behind file like follows

private void val_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ...
}

Then I have requirement populate above same grid in another window using resource dictionary. so I copied that grid to that ResourceDictionary like follows

<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>

then once I compile this I'm getting following error

'ResourceDictionary' root element requires a x:Class attribute to support event handlers in the XAML file. Either remove the event handler for the SelectionChanged event, or add a x:Class attribute to the root element.

mm8
  • 163,881
  • 10
  • 57
  • 88
Kelum
  • 1,745
  • 5
  • 24
  • 45

1 Answers1

0

You need to manually add a code-behind file for the ResourceDictionary and define the val_SelectionChanged event handler in this file.

Please refer to the following link for more information and an example of how to do this:

Is it possible to set code behind a resource dictionary in WPF for event handling?

You basically just create a new class and name it "Dictionary1.xaml.cs" where "Dictionary1" is the name of your resource dictionary XAML file and then you set the x:Class attribute in the XAML file:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                x:Class="WpfApplication1.Dictionary1"
...
mm8
  • 163,881
  • 10
  • 57
  • 88
  • can I give the same code behind file, without create new class file ? – Kelum May 22 '17 at 14:08
  • No. You should create a new code-behind class and copy the event handler over to this one. – mm8 May 22 '17 at 14:09
  • for this purpose can I use user-control and embed grid inside of it and call that user control inside resource-dictionary ? – Kelum May 22 '17 at 14:20
  • 1
    Please ask a new question if you have another issue. Don't ask additional questions in the comments field. – mm8 May 22 '17 at 14:24
  • no I 'm asking whether new approach suitable for this purpose or not , objective - populate grid with same dropdowns in a diffrent window . approach - can I use user-control and embed grid inside of it and call that user control inside resource-dictionary ? – Kelum May 22 '17 at 14:28
  • If I understand you correctly, you could define the Grid in a UserControl and use this UserControl wherever you want. No reason to add the UC to a ResourceDictionary. – mm8 May 22 '17 at 14:30