Isn't Xaml breaking my encapsulation? - c#

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.

Related

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

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.

Access a ListView located in a UserControl from the MainForm in Winforms

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!

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

Click to new window.xaml

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.

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