How show contents of wpf window in stack panel - wpf

i want to show contents of window in wpf when click on button
i think will use container controls like stake panel but doesn't work
private void RibbonButton_Click(object sender, RoutedEventArgs e)
{
Window1 w1 = new Window1();
stkShow.Children.Add(w1);
}

You need to use the content of the window you want to use as child.
This worked for me.
private void RibbonButton_Click(object sender, RoutedEventArgs e)
{
Window1 Child = new Window1();
StkPanelContent.Children.Clear();
object content = Child.Content;
Child.Content = null;
Child.Close();
this.stkShow.Children.Add(content as UIElement);
}
I hope it helps.

Related

Adding tabpage to a tabcontrol which is on another form on click of button

I have a winform called form1 which has tabcontrol with some tabpages and one button on clicking of which I open another form called form2. I want to add tabpage to form1 tabcontrol on click of button which is present on form2.
Assumed that you have not created a tabcontrol at runtime in the Firstform.
In FirstForm.cs
private void button1_Click(object sender, EventArgs e)
{
SecondForm obj = new SecondForm(this);
obj.Show();
}
public void addTabpage()
{
TabPage tbpg = new TabPage();
tbpg.Text = "New tabpage";
tabControl1.TabPages.Add(tbpg);
this.BringToFront();
}
In SecondForm.cs, change the constructor as follows
static FirstForm objpub;
public SecondForm(FirstForm obj)
{
InitializeComponent();
objpub = obj;
}
private void button1_Click(object sender, EventArgs e)
{
if (objpub != null)
{
objpub.addTabpage();
}
}
If this answer does not meet your problem, then provide your code.
Cheers !

WPF contextMenu click issue

A ListBox and a ContextMenu are created dynamicaly. The ListBox has some items.
How do I know the ListBoxItem Text that right mouse button clicked on?
private void Init2()
{
ContextMenu contextMenu = new ContextMenu();
MenuItem menuItemOpen = new MenuItem();
menuItemOpen.Click += new RoutedEventHandler(menuItemOpen_Click);
contextMenu.Items.Add(menuItemOpen);
listBox1.ContextMenu = contextMenu;
}
void menuItemOpen_Click(object sender, RoutedEventArgs e)
{
//How do I know the listItem text that right mouse button clicked on?
}
When you right click, you actually also select. So that means you can just do:
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
string selectedListBoxItemText = ((ListBoxItem)listBox1.SelectedItem).Content.ToString());
// do your thing
}

Silverlight: Closing a ChildWindow when the Overlay is clicked

If the user clicks on the overlay, I want the ChildWindow to automatically close and return the user to the main screen.
Is there a property that controls this? If not, is there a way to attach a click handler to the overlay?
Turns out you can get a reference to the overlay right after it is created. After that it is a simple matter of attaching the event handler.
private void Overlay_MouseButtonDown(object sender, MouseButtonEventArgs e)
{
this.Close();
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var overlay = (Grid)GetTemplateChild("Overlay");
overlay.MouseLeftButtonDown += Overlay_MouseButtonDown;
overlay.MouseRightButtonDown += Overlay_MouseButtonDown;
}

Wpf, I need to hide and show a groupbox?

How I can show or hide a groupbox from xaml.cs. I try to do this in the checkbox's event:
private void cbDaily_Checked(object sender, RoutedEventArgs e)
{
gbCalendar.Visibility = Visibility.Visible;
}
But this don't work.
It must be work on checked/unchecked events of checkbox like this way :
private void chkTest_Checked(object sender, RoutedEventArgs e)
{
grpTest.Visibility = System.Windows.Visibility.Visible;
}
private void chkTest_Unchecked(object sender, RoutedEventArgs e)
{
grpTest.Visibility = System.Windows.Visibility.Hidden;
}
It is working fine in my sample application. Can you please give more details of your problem, so I can have better idea. Is event fired properly ? Make sure name of groupbox in code behind is correct or not ?

Focus the controls in a Tabcontrol

How can focus the controls when select a tabitem in a tabcontrol?
You should capture the Selection changed event of the TabControl and inside that you focus the control you need. Just as
private void TabControl1_SelectionChanged(object sender, EventArgs e)
{
control1.Focus();
}
Not sure I understand what you're asking completely, but you probably want to capture the SelectionChanged event for the TabControl:
public Window1()
{
InitializeComponent();
TabControl1.SelectionChanged += TabControl1_SelectionChanged;
}
private void TabControl1_SelectionChanged(object sender, EventArgs e)
{
TabItem item = (TabItem)TabControl1.SelectedItem;
// Find the first control in the TabItem content and focus it?
}

Resources