简体   繁体   中英

WPF Copied Button Click not firing

I'm quite new to c# wpf and have a problem.

I have used the answer from this post to duplicate a Grid control. The grid control contains a button. It looks like it is being duplicated correctly.

When the original control's button is pressed, the click event is handled which calls a method in the window's code.

When the copy of the control's button is pressed, the click event is not fired and the method is not called. This is confusing me as I want it to call that same method.

Maybe the event handling data is not being copied properly? Is there a way around this?

Both the origional grid and copied grid (containing the buttons) are children of another grid.

Edit:

This is the xaml for the origional grid which contains a button:

<Grid Name="TempTab" DockPanel.Dock="Left" HorizontalAlignment="Left" Margin="5,5,5,0">
            <Rectangle  Fill="Black" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stroke ="White" Margin="0,0,-2,0">
            </Rectangle>
            <Grid>
                <DockPanel LastChildFill="False">
                    <TextBlock Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="3,0,3,3">Some Text</TextBlock>
                    <Button Width="50" BorderBrush="{x:Null}" Foreground="{x:Null}" BorderThickness="0" Margin="3,0,0,0" Click="tabdowntest">
                        <Button.Background>
                            <ImageBrush ImageSource="TopMenuBar_Close.png" Stretch="Uniform"/>
                        </Button.Background>
                    </Button>
                </DockPanel>
            </Grid>
        </Grid>

This grid is a child of a DockPanel with name 'TabsDock'. It is being copied with the following code:

string gridXaml = XamlWriter.Save(TempTab);
StringReader stringReader = new StringReader(gridXaml);
XmlReader xmlReader = XmlReader.Create(stringReader);
Grid newTab = (Grid)XamlReader.Load(xmlReader);
TabsDock.Children.Add(newTab);

This is the code for the 'Click' event handler which should be called when the either the origional or the copied button's are pressed. But it is only called for the origional:

    private void tabdowntest(object sender, MouseButtonEventArgs e)
    {
        Console.WriteLine("Button Pressed");
    }

The bindigs are not set, you need to set them (comment in the orig post):

To be clear, this is only half the solution (as it stood back in 08). This will cause bindings to be evaluated and the results be serialized. If you wish to preserve bindings (as the question asked) you must either add a ExpressionConverter to the Binding type at runtime (see the second part of my question for the relevant link) or see my own answer below for how to do it in 4.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM