Question

This is a WPF application. button is null and CommandParameter is not recognized. I'm not sure how to fix this. Any ideas? this is the code:

private void Button_Click(object sender, RoutedEventArgs e)

var button = (Button) sender;
var userName = button.CommandParameter;





<TabControl Name="ImTabControl"
                    Width="670"
                    Height="450"
                    Margin="0,10,5,0"
                    HorizontalAlignment="Right"
                    VerticalAlignment="Top"
                    ItemsSource="{Binding}">
            <TabControl.Resources>
                <DataTemplate x:Key="TabHeader" DataType="TabItem">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Margin="0,0,5,0"
                                   HorizontalAlignment="Left"
                                   VerticalAlignment="Center"
                                   Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}},
                                                  Path=Header}" />
                        <Button Name="ImButton"
                                Click="CloseTabButton_OnClick"
                                CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}},
                                                           Path=Name}">
                            <Image Width="7"
                                   Height="7"
                                   Source="../Resources/Images/CloseWindow.png" />
                        </Button>
                    </StackPanel>
                </DataTemplate>
            </TabControl.Resources>
        </TabControl>
Was it helpful?

Solution

I've changed the CommandParameter to Tag and sender as Button to (FrameworkElement)sender.

Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Name}">


var button = (FrameworkElement)sender;
var tabName = button.Tag as string;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top