Enable/disable ui element usercontrol in wpf c# code behind - c#

For a project, I create a Numpad and a keyboard.
Both are user controls and they are shown in a Window.
I want to use a button/toggle in xaml to enable the one or the other.
What is the best way to approach this in code- behind C#?
Thank you in advance!

Make a Button on the GUI and bind the Click event to a EventHandler in the Code Behind. This EventHandler basically just flip-flops the IsEnabled Property of the control Elements. Here is how something like this could look
private void BtnToggle_Click(object sender, RoutedEventArgs e)
{
//Do this with every button
BtnToFlip.IsEnabled = !Btn.ToFlip.IsEnabled
}
This flip-flops between IsEnabled from true to false
Note: This is in no way shape or form the best way to do this, and I am aware this is not even remotely MVVM. This is just a simple answer to OP's question as he seems to be very new.

Related

Click on dynamically added UserControls

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))

Isn't Xaml breaking my encapsulation?

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.

Using a Picture Box as a button, how do I ignore/disable double-click events?

I'm having some difficulty using picture boxes as buttons. I'd like to have custom button graphics rather than using the plain-old windows forms Button control.
It works pretty well, except the buttons seem to lag when a single button is pressed rapidly. I'd like the OnClick method to be called immediately every time the Picture Box is clicked, but it seems to be registering a double-click event, even though I am not using that event.
The short version is I'd like to:
(Ideal) Disable the double-click event, since it appears this is what the Control is waiting for on every alternating click of the Picture Box and I never use it. Or,
(Acceptable) Write a generic DoubleClick event I can use for all Picture Boxes I plan to use as buttons.
I did write up a test program which shows that this is, in fact, what's happening. I wrote a MouseUp event that increments a counter by 1, and also added a OnClick even that increments a different counter by 1. When rapidly clicking the Picture Box linked to these events, the MouseUp counter is exactly double that of the OnClick counter.
I then added a DoubleClick event that calls the OnClick event. This, although not ideal, solves the problem perfectly well. If there is a way to write a generic DoubleClick event that all buttons could use, I think this would be acceptable.
Right now my DoubleClick event is just
private void pb_DoubleClick(object sender, EventArgs e)
{
pbOK_Click(sender, e); //This needs to be generic
}
I'd like to at least replace the DoubleClick line with something like
private void pb_DoubleClick(object sender, EventArgs e)
{
(Sender.ToString)_Click(sender, e); // Doesn't work quite like this.
}
I could write a special DoubleClick event for each Picture Box individually, but that is far from ideal and seems quite cumbersome, so I would prefer not to do that if it isn't necessary.
This is my first posted question, so thanks to anyone who takes the time to read this. Any advice would be greatly appreciated.
EDIT: This program is being developed for Windows CE, so the properties and events options for the objects is quite limited. I still may be missing something, but as far as I can tell the Properties list for buttons goes straight from GenerateMember to Location, with none of the Image options available. I apologize for not stating the target platform in the original post.
It is done with the Control.SetStyle() method, like this:
using System;
using System.Windows.Forms;
class MyButton : PictureBox {
public MyButton() {
this.SetStyle(ControlStyles.StandardDoubleClick, false);
}
}
Okay, my original answer was trash. My update:
Here's a link on how to get a controls Method name:
Link
In the link it has a Control c. To get your control (sender) use the following:
PictureBox picbox = (PictureBox) sender;
Next, call a method using a string(your class.Method.Name): Link
Now that should work. Hope it does.
Why don't you create your own control? This way, you don't have to do anything to get the behavior you want.
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class ButtonPictureBox : PictureBox
{
protected override void OnDoubleClick(System.EventArgs e)
{
OnClick(e);
}
}
}
I don't know if this is possible on Windows CE though.
If you also want to hide the DoubleClick event in the form designer, you can add this to the class:
[Browsable(false)]
public new event EventHandler DoubleClick;

Cycling through TabControl tabs while not having focus on it

I started with my first WinForms project, and bumped into a small problem on the way.
I have a Form with a TabControl and some buttons outside it.
The problem is when i have a button focused and press Ctrl-Tab - nothing happens. But if you open, for instance, properties window of a file in explorer, you can cycle through tabs using Ctrl-Tab no matter which element has the focus.
So what is the right way to make such behavior? I mean, i can do Form.KeyPreview = true and write handlers myself, but is there a better way to do this?
As far as I know, the correct way is the one you mentioned yourself i.e. set Form.KeyPreview = true and write private Form1_KeyDown(object sender, KeyEventArgs e) handler to switch tabs.

Best way to swap two .NET controls based on radio buttons

I've got a form where I have two radio buttons and two interchangeable controls (made up of a ListView and a handful of buttons). Based on which radio button is selected I want to display the proper control to the user.
The way I'm doing this now is just loading both controls and setting up an OnRadioButtonSelectionChanged() method which gets called at form load (to set the initial state) and at any time the selection is changed. This method just sets the visible property on each control to the proper value.
This seems to work well enough, but I was curious as to if there was a better or more common way of doing it?
Yep, that's pretty much how I do it. I would set the CheckedChanged event of both radio buttons to point at a single event handler and would place the following code to swap out the visible control.
private void OnRadioButtonCheckedChanged(object sender, EventArgs e)
{
Control1.Visible = RadioButton1.Checked;
Control2.Visible = RadioButton2.Checked;
}
Well you could also use databinding... seems a bit more elegant to me. Suppose you have two radiobuttons "rbA" and "rbB" and two textboxes "txtA" and "txtB". And you want to have txtA visible only when rbA is checked and txtB visible only when rbB is checked. You could do it like so :
private void Form1_Load(object sender, EventArgs e)
{
txtA.DataBindings.Add("Visible", rbA, "Checked");
txtB.DataBindings.Add("Visible", rbB, "Checked");
}
However... I observed that using UserControls instead of TextBoxes breaks the functionality and I should go read on the net why..
LATER EDIT :
The databinding works two-ways! : If you programatically set (from somewhere else) the visibility of the txtA to false the rbA will become unchecked. That's the beauty of Databinding.

Categories