im learning wpf for the first time,
i have made this far
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
// TODO: Add event handler implementation here.
}
lets say its my click button 'home' some how i have made a new window store.xaml at the same product.
how can i connect them ?
heres a sc
Your question seems a bit vague to me, if you simply want to display the store inside the same window you should not implement the content of the window directly but only use the window as a shell for your content, if your store is a window as well you should refactor it into a UserControl which then can be added to the window.
You can also use Pages, see the Navigation Overview for more info on that.
Related
I have an app that uses a side menu, and for each button (there are 3) on the left side menu, it changes the pages shown.
I tried doing it with multiple panels, but it's a nightmare to maintain in designer, and it's probably not a very good programming habit, I expect.
So I search and found what seemed to be a great idea: UserControl.
But as usual, it's not that simple (for a badly self-taught guy like me)
The general flow of the program is as follows:
a Btn_uc1_Check button that gathers informations and displays them in a uc1_ListView,
a Btn_uc2_Seek button that gathers informations on the net based on the uc1_ListView , and displays them on uc2_ListView,
a Btn_uc3_compile that compiles the info from uc2_ListView into a file,
a Clear button that clears the ListView depending on the UserControl on screen.
Now to the problem:
How on earth do I gain access to a ListView located in a UserControl to be able to read, clear, and add items from the MainFrom or from another UserControl?
I searched and honestly found nothing corresponding to what I needed?
Quite many questions.
You can gain access to any controls in UC. Just change the property "Modifiers" of the ListView in your UC to "Public".
Set that method to public. Do not use keyword "static". Each control
in your form is an instance of a class, not a static class actually. In the main form, create a button and double click on it in VS designer. A method will automatically generated, something like private void button1_Click. When the button is clicked, all of the code lines in button1_Click will run.
Create a public event handler of your user control, then pass the method in main to the handler.
So the UC class will be similar to this:
public event EventHandler button_UC_Click_handler;
public UserControl1()
{
InitializeComponent();
}
private void button_UC_Click(object sender, EventArgs e)
{
button_UC_Click_handler.Invoke(sender, e);
}
In main form:
public MainForm()
{
InitializeComponent();
userControl11.button_UC_Click_handler += UserControl11_button_UC_Click_handler;
}
private void UserControl11_button_UC_Click_handler(object sender, EventArgs e)
{
MessageBox.Show("You have clicked it!");
}
Good luck!
I was a stubborn WinForms protectionist for years, but I changed my mind and try to get in touch with WPF. I like it so far.
My problem:
I've created a UserControl "SelectableRectangle" which Contains a Rectangle. Well - in one of my application I create hundreds of them, programmatically/dynamically.
And I want to be able to "click" on them and use different tools, for example changing the color. Therefore I need to know in my main window on which exact SelectableRectangle I clicked.
Any ideas?
Basically, if your usercontrol implements an event handler you can link an event-handler to an event like that:
mycontrol1.Click += new RoutedEventHandler(mycontrol_Click);
and then implement the method
private void mycontrol(object sender, RoutedEventArgs e)
{
// do things here
}
Further, if your usercontrol is derived from either UIElement, UIElement3D or ContentElement you can use the AddHandler method:
mycontrol1.AddHandler(Button.ClickEvent, new RoutedEventHandler(mycontrol_Click))
i am developing windows application using wpf. I want to do some functionality when i click outside of my control created. for example, if i have Message-box open in my window, i want to do some function if i click outside of my Message-box window.
I tried,
private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
.....
}
but its not working.. please any one tell me, what is the event fire when i click outside of my control?
you have two supposed solutions:
one of them is to get Mouse.X, Mouse.Y from System not from application, this article will help
http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C
second which is better is not using Dialog, but use PopUp window and this article will help
how to close a WPF Dialog Window when the user clicks outside it
I'm trying to make a card game using Windows Application Forms.
The thing is that I don't know how to do the following- for example if i'd have 3 buttons- one of them named, for example, "Play", if i'd click on it, it would open the actual game, but in the same window, it would only make the buttons dissapear, and when i'd click back, it would open the window with buttons again. I don't really know how to explain my problem better, hopefully someone can tell me how to do that.
You don't have to hide / show the buttons. What you can do instead is to make a new form with the cards on it. That Form will pop up after you click the play button.
private void PlayButton_Click(object sender, EventArgs e)
{
// You other functionality goes here
GameForm GF = new GameForm();
GF.Show();
//Or - try this and see the difference
GF.ShowDialog();
}
Good Luck!
In addition to Leez's answer, in your situation, you should think about using container controls rather than handling the visible states of individual controls.
You could put related controls in a Panel, GroupBox or TabControl and set the visible properties of those containers instead.
you can use Visible property of button to do that as follows.
private void button1_Click(object sender, EventArgs e)
{
// You other functionality goes here
button1.Visible = false;
}
Ok, i think i'm missing something here.
Lets say, in a Winforms application i have a Form and a UserControl. I then add a Button to the UserCntrol, and then add the UserControl to the Form. Now the Button is added to the UserControl as a private member and until the UserControl exposes it's private member through public property, the Form shouldn't have access to the Button.
private void Form1_Load(object sender, EventArgs e)
{
this.testUserControl1.
}
You won't find the UserControl's Button from the Form's code. From the encapsulation point of view, i think this is exactly what we want.
Now lets say, i'm doing the same thing in a WPF application with a Window and a UserControl. I add a Button to the UserControl through Xaml and then add the UserControl to the Window. But now i can have access to the Button from the Window's code.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.testUserControl1.button1.Content = "What the HELL!";
}
If i add the Button through code instead of Xaml, i still have the chance to make it private. But "building the UI declaratively, NOT through the code" - isn't that's why Xaml is there? Then isn't it breaking the concept of Encapsulation?
EDIT : I know most of us, including myself, use MVVM to develop WPF applications where you don't need to refer to your UI elements from your code-behind. But the context of the question still holds, right? I often build re-usable UserContolr (not as a View as in MVVM) to use them through out several Views as visual element, where i most often than not need to use the code-behind of the UserControl.
I'm not really sure what you're saying. You can make the Button in XAML and have it be private as well:
<Button x:FieldModifier="private" x:Name="Whatever" Content="This is a button" />
You don't have to do it through code. Just it defaults to (I think) internal rather than private. But like H.B. said, I rarely name my controls anyway, so I haven't run into this myself.