creating textbox when button click in WPF? - c#

i already searched few stackoverflow threads related thisbut i have problem in code plz review my code and tell me where is the problem i am stuck and do not where is the problem i am new here please help me out
public partial class MainWindow : Window
{
Grid MainGrid = new Grid();
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
TextBox dynamicTextBox = new TextBox();
dynamicTextBox.Text = "Type Partnumber";
Grid.SetRow(dynamicTextBox, 1);
Grid.SetColumn(dynamicTextBox, 0);
this.MainGrid.Children.Add(dynamicTextBox);
}
}

By using the button_Click event you are adding the dynamic TextBox to the MainGrid and that code looks fine. But the problem is that this.MainGrid is not in the Present UI, its similar to the dynamically created TextBox, since you ware defined it in the code-behind(see the definition above the constructor Grid MainGrid = new Grid();).
Consider that canContainer is a canvas defined in the xaml as like the following,
<Canvas Height="319" Margin="0"
Width="517"
Name="canContainer"/>
To overcome this you can choose any of the following method.
1.Add The MainGrid to the UI. it will add the grid to the canvas, keep in mind the dynamic textBox already added to the Grid.
which means the code should be like this -->
this.canContainer.Children.Add(MainGrid);
2.Add dynamicTextBox to any other parent which is present in the UI.
this.canContainer.Children.Add(dynamicTextBox);
You you are using this method then need not to define and adding MainGrid

Related

How can I display the ToolTip of several control elements of a UserControl in a TextBox of another UserControl?

I'm new to this forum and have been trying to teach myself WPF programming for a short time. I am currently trying to redirect several ToolTip texts from one UserControl to a TextBox from another UserControl. In my MainWindow I show the "Button_with_ToolTip_UserControl.xaml" in Grid.Row="0" and the "Textbox_For_Tooltip_UserControl.xaml" in Grid.Row="1" below. The ToolTip of the upper control elements of my UserControl "Button_with_ToolTip_UserControl.xaml" should be displayed in the TextBox below which was created in the UserControl "Textbox_For_Tooltip_UserControl.xaml". I tried the ToolTipOpening event. This event is also called, but I don't know how to get the text of the ToolTip.
In my "Button_with_ToolTip_UserControl.xaml.cs" I put the event and tried something like that:
public partial class Button_with_ToolTip_UserControl : UserControl
{
private string _toolTipText;
private Button_with_ToolTip_ViewModel currentToolTip = new Button_with_ToolTip_ViewModel();
public Button_with_ToolTip_UserControl()
{
InitializeComponent();
}
private void Button_ToolTipOpening(object sender, ToolTipEventArgs e)
{
_toolTipText = e.OriginalSource.ToolTip.Text;
currentToolTip.CurrentToolTipText=_toolTipText;
}
}
With "_toolTipText = e.OriginalSource.ToolTip.Text;" I tried to get the ToolTip text to set it to the CurrentToolTipText property. But that's not how it works in terms of syntax. But unfortunately I did not find out how to do it.
Can someone help me with my project or give me a tip. Or is that generally a bad practice?

Avalonia UI - How to get/set properties of UI controls from code

I have several TabControls defined in my XAML. I would like my ViewModel to be aware of the TabItem name or the index of the TabItem that is selected.
I also have a ScrollViewer that i would like to always scroll to the bottom when ever a button is pressed.
I should be able to solve both of the above issues if i could somehow get access to the elements in my code.
How can i acheve something like this:
var tabIndex = this.GetElement<TabControl>("NameOfSomeTabControl").SelectedIndex;
var scrollViewer = this.GetElement<ScrollViewer>("NameOfSomeScrollViewer");
scrollViewer.VerticalScrollBarValue = scrollViewer.VerticalScrollBarMaximum;
Edit: code for xaml, viewModel code
Edit 2:
Looks like i am able to get the instance of the element from the window class, however i'm still not sure how to pass the reference to the ViewModel.
Edit 3: I can achieve the scroll viewer going to the bottom automatically using the code below. however, once that method is invoked it seems like the scrolling gets disabled.
var tbRaw = this.Get<TextBlock>("tbRawOutput");
tbRaw.PropertyChanged += (s,e) => {
var svRaw = this.Get<ScrollViewer>("svRawOutput");
svRaw.Offset = new Vector(svRaw.Offset.X, svRaw.Extent.Height -svRaw.Viewport.Height);};
An easier way to do this might be to use the DataContextChanged event handler in your main Window class:
public MainWindow()
{
InitializeComponent();
DataContextChanged += (object sender, EventArgs wat) =>
{
// here, this.DataContext will be your MainWindowViewModel
};
}
Then you can attach more event handlers/use getters and setters on the view model from the Window

Implementing an options dialog

in my application i want to implement an options dialog like you have in VisualStudios if you go to Tools->Options in the menubar. How can i do this? My first idea was to use pages and navigation but maybe there's an easier approach?
It's probably not the easiest way but I wrote this snippet that match your goal and it's a good exercise.
In an empty Windows Forms project add a ListBox (listBox1) and a Panel (panel1). Then create 2 UserControls (UserControl1 and UserControl2), these will be the content that is shown when you click the list.
In your Form1 class we create a ListItem class that will contain your menu options as such:
public partial class Form1 : Form
{
public class ListItem
{
public string Text { get; set; }
public UserControl Value { get; set; }
public ListItem(string text, UserControl value)
{
Text = text;
Value = value;
}
};
...
}
After that you add items to the ListBox right after InitializeComponent() in Form1:
public Form1()
{
InitializeComponent();
listBox1.DisplayMember = "Text";
listBox1.ValueMember = "Value";
listBox1.Items.Add(new ListItem("Item1", new UserControl1()));
listBox1.Items.Add(new ListItem("Item2", new UserControl2()));
}
This will make it so when you use listBox1.SelectedItem it will return an object that you can cast to a ListItem and access the associated UserControl.
To make use of this behaviour, go to designmode and double-click the ListBox, this'll add code for the SelectedIndexChanged event. We use this event to display the UserControl in the Panel panel1. This will clear any old Panel content and add a selected UserControl:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
panel1.Controls.Clear();
UserControl control = (listBox1.SelectedItem as ListItem).Value;
if(control != null)
{
panel1.Controls.Add(control);
control.Dock = DockStyle.Fill;
}
}
I suggest you try adding a button or something to differentiate the UserControls and play around. Have fun! :)
You should create a new Window and show that as opposed to create a page and navigate to it. Then you would call .show() on the new window for it to show.
Then you would change the look of the new window to however you want, the same as editing pages.
If you build your options into a full object model that matches the structure of the options window, then the best way is to use whatever navigation-aware UI binding that your MVVM toolkit uses. The options window would start off as a new root level window to which you would bind the root of your options data model.
So, in short think of the options dialog as a mini-application that uses the same structure as your main MVVM application, but with a different data model root.
If you plan to allow the user to cancel the changes to the options, then you would want your options data model to be clonable so that you can populate the options window with the clone and then swap out the real options with the new data if the user presses OK on the options window. If they select cancel you can just throw the cloned object away and destroy the window.

How to bind Windows Form Control to a "Grid" Panel using MVVM in WPF

I am attempting to use MVVM to Bind a Windows Form Control to a panel in WPF. My overall objective is to be able to dynamically change which specific Windows Form Control I will use as I plan on having potentially several available.
Right now, I have been able to get this to work by having the application launch a callback on initialization which accesses the grid object by name. Here is how XAML currently looks:
<Grid Name="WindowsControlObject"></Grid>
The Callback looks like the following:
private void WindowLoaded(object sender, RoutedEventArgs e)
{
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
System.Windows.Forms.Control activeXControl = new SomeWindowsControl();
host.Child = activeXControl;
this.WindowsControlObject.Children.Add(host);
}
While this works, I am trying to fully utilize the MVVM pattern, as such is there a way I can do something like the following in the XAML/ModelView:
XAML:
<Grid Content="{Binding WindowsControl"></Grid>
In my ModelView:
public class MyModelView
{
public Grid WindowsControl;
public MyModelView{
WindowsControl = new Grid;
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
System.Windows.Forms.Control activeXControl = new SomeWindowsControl();
host.Child = activeXControl;
WindowsControl.WindowsControlObject.Children.Add(host);
}
}
Am I even right in my exploration/possible approach? It has occurred to me that I might need to use some other type of panel (other than grid), but haven't found anything obvious yet. If it can't be done, I have a solution, just not a very clean one.
Doing more digging, it turns out that I really wanted to bind this to a "ContentControl" tag, as follows:
XAML:
<ContentControl Content="{Binding WindowsControl}"/>
ViewModel:
private System.Windows.Forms.Control _myControl;
public WindowsFormsHost STKObject
{
get
{
return new WindowsFormsHost() { Child = _myControl};
}
}

How to load content page into mainPage from another xaml page?

I have 3 separate pages MainPage.xaml, Content.xaml and Nav.xaml. I need to load a Content.xaml in 'ucMainContent' userControl that sits inside MainPage.xaml by clicking a button from Nav.xaml. I am wondering what I am doing wrong. Any advice is highly appreciated.
MainPage.xaml where I set content container with this code:
<UserControl x:Name="ucMainContent" />
I am trying to load Content.xaml into mainPage.xaml from Nav.xaml. The code from Nav.xaml page where is the button:
private void LoadContent_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var contentPage = new Content();
var ucMainPage = new MainPage();
ucMainPage.ucMainContent.Content = contentPage;
}
If I understand you correctly you need to find the parent Control of Nav.xaml in this case Main.
Check out http://forums.silverlight.net/forums/t/55369.aspx - there is an excellent generic utility which allows you to find the parent of any usercontrol

Categories