How to add WPF page to tabcontrol? - c#

I have this main wpf window
and this WPF page
I need to add this page to tabcontrol in main window
This is my OnRender method
protected override void OnRender(DrawingContext drawingContext)
{
if (ISFirstRender)
{
TabItem tabitem = new TabItem();
tabitem.Header = "Tab 3";
pan1.Items.Add(tabitem);
Page1 page1 = new Page1();
tabitem.Content = new Page1();
ISFirstRender = false;
}
base.OnRender(drawingContext);
}
after the application running I faced this exception while selecting the new tab
I need to know how to add wpf page to existing tabcontroll

If you want to add a new Page, as opposed to a UserControl, you can create a new Frame object and place the page in there.
if (ISFirstRender)
{
TabItem tabitem = new TabItem();
tabitem.Header = "Tab 3";
Frame tabFrame = new Frame();
Page1 page1 = new Page1();
tabFrame.Content = page1;
tabitem.Content = tabFrame;
pan1.Items.Add(tabitem);
ISFirstRender = false;
}

You can add user controls to the TabControl. So go to the add new items and select the user control and make what you want (like what you have in the page). Then add an instance of that user control to the TabControl.
protected override void OnRender(DrawingContext drawingContext)
{
if (ISFirstRender)
{
TabItem tabitem = new TabItem();
tabitem.Header = "Tab 3";
pan1.Items.Add(tabitem);
MyUserControl userControl = new MyUserControl();
tabitem.Content = userControl;
ISFirstRender = false;
}
base.OnRender(drawingContext);
}

Related

Set Back Button Title on Xamarin Forms C#

im quite new to c# and Xamarin and I've just encountered a problem. I can't seem to be able to set the back button title on a navigation page. Ive tried using the static SetBackButtonTitle method but it does not seem to set the back button to the title I want which is
'Test' but instead its giving me the 'default' title which is "Back".
// The root page of your application
var content = new ContentPage();
var CompanyName = new Label();
CompanyName.HorizontalTextAlignment = TextAlignment.Center;
CompanyName.Text = "Test";
var NextPage = new Button();
NextPage.Text = "Next Page";
NextPage.Font = Font.SystemFontOfSize(NamedSize.Large);
NextPage.BorderWidth = 1;
NextPage.HorizontalOptions = LayoutOptions.Center;
NextPage.VerticalOptions = LayoutOptions.CenterAndExpand;
var layout = new StackLayout();
layout.VerticalOptions = LayoutOptions.CenterAndExpand;
layout.Children.Add(CompanyName);
layout.Children.Add(NextPage);
content.Content = layout;
var content2 = new ContentPage();
var navigation = new NavigationPage(content);
NavigationPage.SetHasBackButton(navigation,true);
NavigationPage.SetBackButtonTitle(navigation,"Test");
NextPage.Clicked += NextPageClicked;
async void NextPageClicked(object sender, EventArgs e)
{
await navigation.PushAsync(content2);
}
MainPage = navigation;
}
If you want to change the back button's title(at the left in the navigation bar) in the second page, you should call the method SetBackButtonTitle with first page as parameter. So please modify NavigationPage.SetBackButtonTitle(navigation,"Test"); to NavigationPage.SetBackButtonTitle(content,"Test");
For those who use Xamarin.Forms WPF if you want to change back button title you can do this:
protected override void OnDisappearing()
{
base.OnDisappearing();
Title = "";
}
protected override void OnAppearing()
{
base.OnAppearing();
Title = "MyTitle";
}

How to retain new controls after PostBack?

I am creating a web app that allows the user to create their own forms. but once a control is added (for example a new label with its control) it will be deleted upon me trying to add another object to the form.
this is the code for the button that creates the new item.
protected void createFormButton_Click(object sender, EventArgs e)
{
////titles is the div id of where i want to insert the title label
var titlelabel = new Label();
titlelabel.Text = textboxForTitle.Text;
titles.Controls.Add(titlelabel);
controls.Add(titlelabel);
if (optionsDropdown.SelectedValue == "Checkbox")
{
//elements is the div id of where i want to insert the control
var newControl = new CheckBox();
newControl.CssClass = "checkbox";
newControl.Checked = true;
elements.Controls.Add(newControl);
controls.Add(newControl);
}
else if (optionsDropdown.SelectedValue == "Textbox")
{
//elements is the div id of where i want to insert the control
var newControl = new TextBox();
newControl.Text = "this is some text on the new box";
newControl.CssClass = "form-control";
elements.Controls.Add(newControl);
}
else if (optionsDropdown.SelectedValue == "Dropdown")
{
//elements is the div id of where i want to insert the control
var newControl = new DropDownList();
newControl.Items.Add("one");
newControl.Items.Add("two");
newControl.Items.Add("three");
newControl.CssClass = "form-control";
elements.Controls.Add(newControl);
}
}
how can I save the new controls, so with each button click they previous control does not get deleted on postback?
When adding controls dynamically they are deleted on each postback. Now if you follow the lifecycle of an asp page you will notice that the viewstate ( the variable that holds all of your data from the client side objects) comes after the page.init. So when working with dynamically added controls in asp you need to recreate then on each postback in the page.init event. Then the viewstate is loaded into thoses controls. The way i do it is i keep everycontrol created in a list(of control) in session and add them in the placeholder at the page.init
You could create a class-level variable which is a List of controls, rather than adding an item to the built-in controls collection. In your case you would need two, since you are adding controls both to the page and the elements div. These lists will persist across postbacks, so each time you can repopulate the page controls accordingly at the end of your method via AddRange.
NOTE: code untested.
List<Control> pageControls = new List<Control>();
List<Control> elementControls = new List<Control>();
protected void createFormButton_Click(object sender, EventArgs e)
{
////titles is the div id of where i want to insert the title label
var titlelabel = new Label();
titlelabel.Text = textboxForTitle.Text;
titles.Controls.Add(titlelabel);
pageControls.Add(titlelabel);
if (optionsDropdown.SelectedValue == "Checkbox")
{
//elements is the div id of where i want to insert the control
var newControl = new CheckBox();
newControl.CssClass = "checkbox";
newControl.Checked = true;
elements.Controls.Add(newControl);
pageControls.Add(newControl);
}
else if (optionsDropdown.SelectedValue == "Textbox")
{
//elements is the div id of where i want to insert the control
var newControl = new TextBox();
newControl.Text = "this is some text on the new box";
newControl.CssClass = "form-control";
elementControls.Add(newControl);
}
else if (optionsDropdown.SelectedValue == "Dropdown")
{
//elements is the div id of where i want to insert the control
var newControl = new DropDownList();
newControl.Items.Add("one");
newControl.Items.Add("two");
newControl.Items.Add("three");
newControl.CssClass = "form-control";
elementControls.Add(newControl);
}
Controls.AddRange(pageControls);
elements.Controls.AddRange(elementControls);
}

create popup using silverlight and add a data grid to the popup

I need to add a grid to a popup in Silverlight project. I have added this code in the mainpage.xaml.cs file I have created a popup.
but I need to add a grid showing a table from the database.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
popup p = new Popup();Border border = new Border();
border.BorderBrush = new SolidColorBrush(Colors.Black);
border.BorderThickness = new Thickness(5.0);
StackPanel panel1 = new StackPanel();
panel1.Background = new SolidColorBrush(Colors.LightGray);
Button button1 = new Button();
button1.Content = "Close";
button1.Margin = new Thickness(2.0);
button1.Click += new RoutedEventHandler(button1_Click);
TextBlock textblock1 = new TextBlock();
textblock1.Text = "The popup control";
textblock1.Margin = new Thickness(5.0);
panel1.Children.Add(textblock1);
panel1.Children.Add(button1);
border.Child = panel1;
Grid grid1 = new Grid();
// i need to bind this grid to data from the sql database and attach this grid to the popup
// Set the Child property of Popup to the border
// which contains a stackpanel, textblock and button.
p.Child = border;
p.DataContext =
// Set where the popup will show up on the screen.
p.VerticalOffset = 200;
p.HorizontalOffset = 300;
// Open the popup.
p.IsOpen = true;
}
The Grid is a layout control, and not for displaying tables. For tabular data, you'll need a DataGrid control.
List<Customer> customerList = new List<Customer>();
var dataGrid = new DataGrid();
dataGrid.ItemsSource = customerList;
Popup popup = new Popup();
popup.Child = dataGrid;
popup.IsOpen = true;

SystemTray for Pivot Item content

I have a Pivot Control in MainPage.xaml page. I add in the PageOne.xaml.cs as pivot item using
private void OnLoadingPivotItem(object sender, PivotItemEventArgs e)
{
if (e.Item.Content != null)
{
// Content loaded already
return;
}
Pivot pivot = (Pivot)sender;
if (e.Item == pivot.Items[0])
{
e.Item.Content = new PageOne();
}
}
Thing is not working so fine, but I have tried to fix them, for example,
// NavigationService.Navigation(new Uri());
// need to change to
(App.Current as App).RootFrame.Navigate( new Uri());
// ApplicationBar for PivotItem
appBar = ((PhoneApplicationPage)((PhoneApplicationFrame)Application.Current.RootVisual).Content).ApplicationBar;
Now, I need to show ProgressIndicator on my PivotControl MainPage.xaml from my PageOne.xaml.cs code.
_progressIndicator = new ProgressIndicator
{
IsIndeterminate = true,
IsVisible = false,
};
SystemTray.SetProgressIndicator(this, _progressIndicator);
But progressIndicator doesn't show, what should I do? My guess is to call the SystemTray of RootFrame but I am not sure how to.
Try change the property IsVisible to true.
Wish this can help you.
I need to pass MainPage as reference to PageOne, and put it as parameter when using SystemTray.SetProgressIndicator.
In MainPage.xaml.cs
e.Item.Content = new PageOne(this);
Here is the constructor code from PageOne.xaml.cs
public PageOne(MainPage page) {
_progressIndicator = new ProgressIndicator
{
IsIndeterminate = true,
IsVisible = false,
};
SystemTray.SetProgressIndicator(page, _progressIndicator);
}

Create dynamic dragpanel in asp.net with c#

I want to create ajax dragpanel dynamically and add the controls and panel dynamically.I used the below code for create the control dynamically.But I can`t able to drag it.
enter code here
protected void Page_PreInit(object sender, EventArgs e)
{
DragPanelExtender drg = new DragPanelExtender();
drg.ID = "drg_1";
drg.DragHandleID = "pnl_1";
drg.TargetControlID = "pnl_2";
Panel p1 = new Panel();
p1.ID = "pnl_1";
Panel p2 = new Panel();
p2.ID = "pnl_2";
Label l1 = new Label();
l1.Text = "First";
TextBox t = new TextBox();
t.Text = "Enter text";
p2.Controls.Add(l1);
p2.Controls.Add(t);
p1.Controls.Add(p2);
Page.Controls.Add(p1);
}
How to I create drag controls inside the panel

Categories