In this case, how enable the event handler in a user control - c#

I have a user control that got an image, two textblock, button & integer (in the code class non UI). I have bind this control to the DB and add it in a list with no issues.
But the only thing that is left is. I would like whenever I press the button it pulls the number from the integer in the control and compare that number with the DB and return a result.
Please advise me since I don't know how to do this.

A good place to start will be http://wpftutorial.net/ where you can lear a lot about WPF and MVVM.
Some good key words for you to search and learn in google will be:
MVVM
WPF Binding
WPF Command
MVVM light
These keywords will give you a good start on learning more WPF
In relation to your question above you need to:
1. handle the button click
2. do something
3. place the result in the required textblock
<Button click="some_method_name"/>
public void some_method_name()
{
// do something
textBlock.Text = result;
}

Related

How do I display the value in the Textbox

How do I display Price value($) in textbox next to a label when user checks the radio button, select one of item from listBox, and then clicks the button?
Is there way to do it on the form1.cs[Design] using the properties? Or do I need to set them up in code level?
listBox item example
EDIT: Solved this in Code-level.
I don't think you can do it without any code.
Also I'd recommend you to check out this page - just a few simple rules can make your chances to get a good answer on this site much better.
And there is not much of coding needed to solve your problem. Take a look on the Events tab in Property view in form designer. A few event handlers to process user input and some fields inside your class to store the data - I assume it is not some serious business app you're dealing with, so all the code you need for this to work would be like 20 lines tops -)

Accessing a button control of a page from another class Windows phone?

The problem I'm facing is that, I'm unable to access a control(button) which is in the MenuPanoramaPage.xaml from another page which is MyProfile.xaml.
I wanted to change the content within the button when I select the radio button which is in the MyProfile.xaml. How could I make this?
I tried using this within the button tag in xaml:
x:FieldModifier="public"
But it didn't work out. How can I work on this?
Thanks in advance.
I suggest you to use MVVM Pattern in which use can handle these kind of problems like handling data of one page from other page.
I give you a simple scenario :-
Suppose you want to change the content of a button that is on another page then what you have done basically is.
Some Points :-
1- > Define a string property in viewmodel of the page that contain button. and bind this property to the content property of the button.
2 -> So suppose you want to change it from other page then all you have to do is send a message from another page to change this property. And it will be automatically updated on button(you will get it after some example setup).
3 -> It is not related to some simple string contents. You handle whole data that will be displayed on that page and other View related properties too.(Basically MVVM is much more powerful than you can thought of).
4 -> Last point If you want to make a quality Windows Phone app then MVVM pattern is a good fit.
Links :-
How to implement MVVM in Windows Phone
Basic one from Channel9
What actually MVVM is ?
Explore those. You will not regret. Cheers :)
Sorry, but this is not possible. You only have direct access to the controls on the page you are on. In this case you can only change the controls in MyProfile.xaml.
But what you could do is sending an event or message (if you are using mvvm) to the MenuPanoramaPage.xaml / ViewModel.

FocusManager.FocusedElement and MVVM

I'd like to control which button is focused in my view. User must be able to perform a job without need to use mouse. And job is going through all elements. Depending on element, some buttons will appears, some disappears.
I could do it with dependency properties. To example, if there are buttons Previous and Next, then I can provide IsPreviousFocused and IsNextFocused to set focus to them after certain event. Events can be: showing window for the first time (something should have focus already), reaching 1 element (hiding Previous button, setting IsNextFocused), reaching last element (opposite), etc.
This looks reasonable more or less.
But, if I have, to example, 10 buttons (different operations), then all of them will have to have dependency property!
So I was thinking about much easier approach: when I hide button, there will be no focus
if(FocusManager.FocusedElement == null) { ... }
If I can detect, when there are no focus, then I can try to set it to one of the buttons somehow. This way, I don't really need any complicated focus management
Question is: how to deal with FocusManager.FocusedElement in MVVM scenario? How to detect when there is no focus (when window is shown first time, when certain button is clicked and become invisible, etc)?
P.S.: I actually hate ms for making another technology without thinking fully into it; focus is very basic feature (and everybody care about it in their software), but there is no direct support for it (in xaml); looks like "oh, its too complicated, lets skip it" solution.
You could control your focus from your ViewModel by using the approach shown here:
Set focus on textbox in WPF from view model (C#)

C# WPF onclick change location of listbox and textbox

I am a total beginner in this field so I need some help.
In my application I have to different buttons which has to change to position or location of some elements on click.
I have one Textbox and one ListBox with 4 Listboxitems and on button click their position has to change.
Can you please tell me how to implement this in code.
Thank you so much!
In WPF you normally use the MVVM pattern. If you haven't heard of it, there is a good explanation here. So basically, you listbox is just a presentation of an underlying list of data items. You don't change the presentation listboxitems, but instead change the items in the underlying collection and let the WPF UI handle the resulting update of the presentation layer.
You button should activate a command that switches the data in the viewmodel (or even in the model, depending on your needs). For an introduction to commands, you might find this interesting.

Keeping track of index in an editable listview populated by observable collection

As a WPF newbie, I'm kindof going round in circles trying to work with observable collections.
So here's what i'm trying to do using the MVVM pattern.
I have a form in which I am trying to enter, say, skill sets of a person.
The following is my UI layout and hopefully this will also give you an idea of what my datacontext for each view is
Main Window
SubScreen
TextBlk(not a part of the user Control below)
ListView
UserControl(added to listview Only upon a button click)
ComboBox (ItemSource = SkillsList eg: C,C++, C# etc)
TxtBox (Binding TotalExperience... a number)
Radion Buttons (Bound to skillLevel property eg: Beginner, Expert etc)
Button (Command = Add new skill set row => repeat the usercontrol in a new list view row)
The user Control is my observable collection.
How do I make sure the usercontrol is populated correctly? How do I keep track of which row of the listView I am working on (i.e: adding or editing)? Can I save the collection as a whole for a particular person? All this I am trying to do in the View model...no code behind file.
Apologies if the questions are naive. But the more I read up the more confused I get. Would be great if someone could help me out.
You question is pretty huge to answer. Read this article it shows a good example of a xml only set up
WPF/MVVM Quick Start Tutorial
After you run through the first link it would be worth looking here too
Implementing the MVVM Pattern
Pay particular attention to the INotifyPropertyChanged example. This is how you keep track of which record you are working on.
You will have a model which is passed around, it contains all fields you create in it including the ID if necessary.
Also if you are trying for no code behind then also pay attention to the Commands.
Hope this helps.

Categories