Replace (sender as Button) with myNewButton

Replace (sender as Button) with myNewButton



I have a dynamically created button and it has been clicked, so we are in the click event for a button that was created at runtime. Within the click event I created a new button and I want to replace the sender button with the new button.



The sender resides within a WrapPanel in a MahApps.Metro Flyout (likely irrelevant information, but just to be detailed.



So in a nutshell:



Replace myOldButton (which is the sender button) with myNewButton which is being dynamically created.



ALTERNATIVELY:



What I'm really trying to get at is the foreground property of iconPacks:PackIconMaterial icon which is within the button (see https://github.com/MahApps/MahApps.Metro.IconPacks). The x:Name of the dynamic icon can be derived from the name of the sender button if that is helpful, but I can't seem to get at the foreground property of an iconpacks icon which is why I thought about just replacing the control with a new control like just like the original button. Here is the dynamically created button:


// Create a stringBuilder
StringBuilder sb = new StringBuilder();

// use xaml to declare a button as string containing xaml
sb.Append(@"<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:iconPacks='http://metro.mahapps.com/winfx/xaml/iconpacks'
Width='85' Margin='4' Padding='3'>
<WrapPanel Margin='0'>
<iconPacks:PackIconMaterial Kind='BedEmpty' VerticalAlignment='Center' Foreground='Green'/>
<Label Content='dynamicBedName' FontSize='12'/>
<iconPacks:PackIconMaterial x:Name='dynamicIconName' Kind='RadioboxMarked' Height='12' Width='12'
Margin='0' Foreground='Green' VerticalAlignment='Center'/>
</WrapPanel >
</Button>");

//Replace the placeholder Button.Name
sb.Replace("dynamicBedName", thisBed);

//Replace the placeholder IconPacks:PackIconMaterial x:Name
string iconName = "icon" + thisBed;
sb.Replace("dynamicIconName", iconName);

// Create button using a XamlReader
Button myButton = (Button)XamlReader.Parse(sb.ToString());

//Replace placeholder Label on Button
myButton.Name = "_" + thisBed + "_btn";

//Add a ToolTip to each button specific to this button
myButton.ToolTip = thisBed;






Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)