Listbox selecting first item again wpf mvvm not working - c#

Consider a listbox in WPF (MVVM) having a list of items(items are file names).If First item in listbox selected then the first file will be opened correctly. Suppose, if 'New' button(to open new file)is clicked and new file is opened. Now, if the first item(first file) is selected then fist file is not opening insetead new is only opened because list box selection is not changed. Instead if any other item is selected then it is working fine.How to make the first item to get selected again.

You can set your selectedItem to null then set it back to your value that you need.

Add an extra item as "SelectFile" in databound collection of ListBox.
Whenenver Newbutton get clicked, through button command execution, set ListBox selected item as "SelectFile".
This will allow you to reselect last file.
If this default entry "SelectFile" get selected, do not proceed with file opening from view model.

Related

list view selection mode none in windows form applications

i am working on windows form application ..i have a list view.
in the list view some time i will select some row then i will click add button .that time selected rows will add to list view.
after that i want to make my listview selection mode none so i wrote code like this
List_Item.Select = Nothing ..
but this code throwing error? how i can make my list view selection mode none?
any help is very appreciable.thanks in advance
loop every item and set the selected field to false
For Each item As ListViewItem In Me.List_Item.Items
item.Selected = false
Next

Update Textbox from DataGrid in Silverlight

I have a silverlight application.I am inserting values in a list and binding that List to datagrid with an edit and delete button before every item in datagrid.
When I click on the edit button, the values present in the item should get displayed in textbox outside datagrid. Now, when I click on edit button, the values get inserted in textbox at double click of edit button(not on single click) .I am getting the value in the property in code behind in ViewModel but that value is not populated in textbox.It only populates it at second click.
Why so? I want the value to be populated at first click on edit button.
current binding of textbox text property is :
Text="{Binding Value,Mode=TwoWay}"

Combobox displays duplicate items when any item is selected overwiting another item

I am doing a winforms project with C# as code behind in VS 2010.
I have a combobox which is populated from a db table using tableadaptor.fill
Each time I select any value in this combobox, the selected value overwrites another existing item in the combobox and therefore appears twice. As shown below:
Here, I click combobox and select LEP 2013
Now when I click again on the combobox, LEP 2013 overwrites SFT 2013 and appears twice.
Also, there is no code written on the selection of the item in the combobox, only the Fetch button does the next action. This behavior occurs even before the Fetch button can be clicked.
Another observation is that when I select the first item in the combobox, in this case SFT 2013, it will not duplicate the item.
This question is similar to
Combobox displaying duplicate items
and
Combobox displays duplicate items when an item is selected
But their solutions don't work for me. The column of the table that this combobox is bound to, is the primary key of that table, so it will not contain duplicate values.
Any suggestions ?
I removed the existing data binding for the combobox and rebound using a new adapter and the issue went away.
However I still don;t know what the earlier issue was. Somehow, the first item of the combobox's Text was getting replaced by the SelectedItem's text.
I had the same problem except I was using a datatable to populate my combobox. I put dataTable.Clear(); at the beginning of my procedure to clear the table and it solved the problem. You should check your data sources to make sure they are cleared before reloading data into them.

c# wpf checkbox content from one list to another

I have a list where i dynamically store checkboxes(with grid content in it). Next to the list i have a button who,when clicked, must put the selected checkboxes with content in the right list. When delete from right list is clicked, the selected item(s) on the right side needs to be deleted.
How is this possible? Suppose i have this code:
CheckBox cbox = new CheckBox();
Grid panel= new Grid();
panel.Width = 260;
cbox.Content = panel;
You can use:
Listbox.ItemTemplate to represent the data you want (so that it looks like CheckBox:Label)
Use 2 ObservableCollections to store 2 lists that you have
In your buttons you can bind to Current Item in the List it is associated with
On button click, it will get current select Item and Add/Remove from ObservableCollections as needed
Let me know if you need specific details. The good thing about ObservableCollections is that whenever collection changes, anything bound to it (like ListBox) will get updated.
so that any manupilation with the data is outside View so to speak and done on ViewModel.

Dropdown list selected item always set to default value

I wish to pass values of the selected items into a database and however noticed that the selected item is not what is sent into the database you can see in the snap shot below.
during run time the following value is recorded.
Where did it all go wrong with the dropdown lists selected item?
Counting on your intelligence...thanks.
Put your call to load the dropdownlist in
if (!Page.IsPostBack)
{
//LoadDropdownListHere();
}
Try DBNull.Value instead of null.
Edit:
I think you need to specify the type using this overload: http://msdn.microsoft.com/en-us/library/cc491445.aspx
CSMDataSource.InsertParameters.Add("CaseID", DBType.Int32, CaseIDDropDownList.SelectedItem.Text);
I found the problem for the dropdownlist always selecting the first value. It appears the dropdown list is always rebinded anytime i click a button that requires a post pack as such the data tha was binded at page load re binds a gain before the selected item gets picked as such the default first value gets picked all the time. to solve my problem i first disabled enable auto postback on the dropdownlist and in the code behind that is my .cs file during the page load, where i first binded the data to the dropdown list, i used a condition like if(!ispostback) to bind my data. what this does is when the page loads first time the dropdownlist is binded and if i should select an item from the drop down list, the "auto post back" that i disabled earlier on keeps the selected item selected and when i click and button that requires a post to the server, the (!ispostback) prevents the dropdownlist to be binded again before selection. so in effect, the selection is done first and afterwards if the page should load anew, the drop down list is binded again.
i was in a bit of a rush while typing so please bare with my typo errors. do call in anytime for more clarification...peace

Categories

Resources