C# WPF onclick change location of listbox and textbox - c#

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.

Related

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

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;
}

WPF Show Data in Grid

I have a C# CLI program that scans for missing Windows updates and writes them to command line or serializes them to XML depending on the flag passed in. I'm trying to build a WPF component to this but am unsure of a few things. Specifically I'd like to write all missing updates to a grid in the center of my WPF main window. The appearance would be something like this (with gridlines between the fields):
NAME SEVERITY DETECTED
Security Update for Windows 7 (KB1234567) Important 3/9/2014
Security Update for Windows 7 (KB7654321) Critical 3/9/2014
My specific questions:
What type of control would I need to add to the window to house this data?
How do I send the data (detected missing update names and properties) to the grid for display?
How can I set the control so that it is collapsed (or invisible) when no missing updates are detected?
Will I need to add a scrollbar to the grid or will one display automatically?
Apologies for the simple questions. I'm really just looking for some examples to get started, and I haven't been able to find anything thus far that meets my needs.
What type of control would I need to add to the window to house this
data?
DataGrid control is what you are looking for.
How do I send the data (detected missing update names and properties)
to the grid for display?
Bind ItemsSourceof DataGrid to ObservableCollection<T> where T will be class containing data with properties Name, Severity and Detected.
How can I set the control so that it is collapsed (or invisible) when
no missing updates are detected?
Add a DataTrigger to check if ItemsSource collection contains no data, collapse the visibility.
Will I need to add a scrollbar to the grid or will one display
automatically?
DataGrid internally use ScrollViewer. No need to add explicitly.
Refer to the dataGrid samples here and here.
As an alternative DataGrid can offer ListView control, it will be little "easier" than the DataGrid, he also supports the ability to sort columns. For him also need to bind a ItemsSource collection to display:
The ListView control provides the infrastructure to display a set of data items in different layouts or views. For example, a user may want to display data items in a table and also to sort its columns.
Example in MSDN.
Little add some notes to the wonderful answer of #RohitVats, all that has been said about DataGrid also applies to ListView:
How can I set the control so that it is collapsed (or invisible) when no missing updates are detected?
In this situation, I advise you to adhere to the principle of MVVM. Use Binding and Commands to create an independent application. You want to create property (for example IsEnabled) in Model / ViewModel and use bindings to set them in the View, in order to avoid apply directly to the Control. WinForms style app or "regular" applications creates a strong connection between logic and UI, which subsequently impedes further change and application maintenance.

Conditionally validate Dynamically generated Controls in Silverlight

I am having a form with different type of controls like Text Box, Drop downs, Check box, Radio buttons etc. All these controls are loaded dynamically from database at run time.
I want to perform validation on Text box on conditional basis. For example, If we have selected any value in drop down, then you must have to fill details in Text box. Otherwise text box details are not required.
I am open to use database to perform this task and I am using MVVM pattern in my project.
Any help on this is highly appreciated.
Thanks.
(I started this as a comment, but it ended up being too long).
In theory you have access to all these controls and their values in your ViewModel.
Without knowing the specifics of your program, it's difficult to suggest anything useful, but in essence you need to expose some more properties from your ViewModel (probably boolean) which will be calculated based on the values in your controls. Then you need to bind IsEnabled properties on your controls to these new properties.
Sounds simple, but I think you have some architectural problems which will make it difficult to implement what I suggested above. In order for this to work and automatically update your controls whenever other controls' content change, your ViewModel needs to implement INotifyPropertyChanged and raise PropertyChanged event every time you update one of those boolean properties.
I think what you're trying to do could be achieved with ItemsControl and DataTemplates (and maybe DataTemplateSelectors). This will allow you to store "data" in your ViewModel (say List or something more specific) without referencing the actual Controls and the relevant DataTemplates will add the right controls for different data types you have in your ViewModel.

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.

WP7 Complex Layout

I want to make a complex layout, and would like advice on the best way to go about this.
Here is the behaviour I'm looking for: Layout of some text blocks and some images, such that a tap anywhere on the layout will go to another related page to that item. I want a long list of this item in a scroll viewer.
First, I tried to make a grid, add the items in it, then add this grid in the outer grid, the main one for pressing, I made an event handler for mouse click, but a problem appeared; that when I try to scroll "on the item to view the all list" the event handler fired!, I tried the mouseButton up and down, and the same happen
Second, I'm not sure if it's an efficient way to make it.
I want a good way to design this complex layout, and of course it will be a programmatic way, and a way to recognize the item I press on.
It sounds like you're trying to create your own ListBox control. Using this will give you the scrolling functionality for free, and you can use the ListBox's SelectionChanged event to determine when an item has been clicked.
The best way to design the layout will depend on how complex it actually is. If it merely contains an image and some text, you should have no problem hand-coding that in XAML. If there's a lot more to it than that, I'd recommend looking into Microsoft's Blend tool. Whichever route you choose to produce the XAML, you will place the code in the ListBox's ItemTemplate, which determines how each item in the ListBox is displayed.
A good walkthrough of something similar to what you're doing can be found at http://weblogs.asp.net/psheriff/archive/2010/10/27/windows-phone-list-box-with-images.aspx

Categories