1

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");
    }
Community
  • 1
  • 1
WPFHelp
  • 35
  • 3
  • 1
    I don't see anything in that link related to a Grid control or a button. I even followed it through to the CodeProject source. Can you provide a little more help - specifically on what you are expecting to fire when you click, i.e. is it an event handler in code behind, or are you binding to an ICommand instance? – LordWilmore Aug 05 '16 at 12:12
  • Sounds like a wonderfully overcomplicated and error prone way to reimplement templates poorly. – 15ee8f99-57ff-4f92-890c-b56153 Aug 05 '16 at 13:06
  • Thank you!! I didn't know how to use templates in wpf. Just what I needed! – WPFHelp Aug 05 '16 at 16:34

1 Answers1

0

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.

Dexion
  • 1,101
  • 8
  • 14