1

I have a custom user control which has some textboxes and some buttons. In my main application (MainWindow.xaml) I use this control like this :

<ScrollViewer Grid.Column="1" 
              VerticalScrollBarVisibility="Auto" 
              HorizontalScrollBarVisibility="Auto" 
              MaxHeight="300" Width="Auto" 
              HorizontalAlignment="Left">
    <StackPanel x:Name="MyStackPanel" 
                Margin="10,0,10,0" 
                MaxHeight="300" Width="Auto">
        <ItemsControl ItemsSource="{Binding}" 
                      x:Name="MyItemsControl" 
                      BorderThickness="0" 
                      Background="Transparent">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <local:MyCustomControl/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</ScrollViewer>

The DataContext of the ItemsControl is a table from the DataSet. Each textbox in the custom control is binded to some field in the table . I want to implement a click event of some button in the custom control in the MainWindow.xaml.cs , not in the custom control behind code. How can I do it? thanks.

1 Answer 1

2

You need to create a new event on the custom control and fire this event in the click event on the button in the code behind of the custom control.

Sign up to request clarification or add additional context in comments.

2 Comments

in the code behind of the custom control I can't implement the click event, because I need to do some operations in the MainWindow.xaml.cs . But in the MainEindow.xaml I can't get to the button.click event of the custom control because it is in the ItemTemlate .....
@N.D: You can declare new events on custom controls and you can fire this event in the code behind of your custom control. You can then subscribe to this new event in your main window.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.