I've seen a number of suggestions for AutoComplete in WPF on a TextBox. However, they are all years old by now and many are expired without working websites.
Basically, I'm going to want to bind the TextBox to an ObservableCollection with a Model of a Person. Their Name will be displayed wen the user types as a suggestion, and will be displayed when the user selects said Person.
However I also want to be able to access the selected Persons ID as well. Is there a TextBox that is still current and can achieve what I would like it to do?
EDIT: I've attempted to use WPF TextBox AutoComplete like so:
<TextBox Text="{Binding SelectedItem.ContactMadeBy, ElementName=contactsDataGrid, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding EmployeeCollection}"/>
but nothing happens when I type. I've looped through EmployeeCollection and it is definitely populated, so the issue lies with the TextBox. An additional problem to this is that it needs to be bound to an IEnumerable<string> where I can't have the Persons ID as well.
Related
Currently I'm doing a registration to create a new user to login, but to create an user I would like to have updatesourcetrigger on my fields just like I do for the name field for an example:
<TextBox Text="{Binding FirstName, UpdateSourceTrigger=PropertyChanged}"></TextBox>
The functions to create the user works but the updating information at the current time doesn't work really unless you press on another textbox for an example, if you do that then the information on the passwordbox will update to the new value. I am using MVVM so I don't want any code behind in the xaml.cs file. I want it to be updating its value like on the xaml code I mentioned.
Solved it by myself without any bindings or a updatesourcetrigger.
I did it with a commandparameter. With a button using a commandparameter, which means that it gets current information on the passwordboxes.
We have a controls that contains family member information (e.g. textbox for first name, last name, address, phone, etc).
This is done like the following bound to the FamilyMember object:
<TextBox x:Name="FirstNameTextBox" Text="{Binding FamilyMember.FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextWrapping="NoWrap" Margin="0,0,0,5" Width="363" Height="30" HorizontalAlignment="Right"/>
We have a button to create a new family member. It's purpose is to blank out the fields.
In the scenario where we click on a family member it shows the values in the textboxes. Now click on "create new" button and it should blank out the fields.
The problem i'm encountering is because the data is still bound to the previous family member ... the "blanking" out of the control values will also affect the previously selected family member object.
Question: Would I need to "unbind" the control and then do the reset on the fields? I thought there was a way to do that in XAML but it doesn't seem like it is possible in Windows 8.1 apps.
Thanks for the help.
#Nathan Cooper. you're right. that's what I thought and I did something a bit different before coming back here to see if anyone had any ideas.
Basically there is a dropdown of family members. I added a blank family member object into the dropdown. So when the "new" family member button was pressed it would select the blank family member object. So no wiping or anything that was out in left field when I first looked at this was done.
Thanks to all for taking a look and for your help.
Im trying to get data from first row in datagrid in c# wpf app but i cannot figure out the code.
Can somebody help me?
I tried WF app code but it doesn't work because there's not function of .Rows
return Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentRow.Index]
.Cells[0].Value.ToString());
Thanks
UPDATE:
Im trying to get current's logged in users id to display in textbox, so i can use it to make a reservation later and join two tables.
Also can somebody help write SQL statement that joins two different id's (user's ID and id of the book that user is buying into a new table called "reservations"
Can somebody help me?
Since others have already posted a potential workaround for your problem, I'll go ahead and explain the proper solution:
Delete all your code and start all over.
WPF is NOT winforms, and if you're working with WPF, you need to leave behind any and all notions you got from the traditional approach and understand and embrace The WPF Mentality.
Basically, you don't "get data from a DataGrid" in WPF, simply because UI is NOT Data.
This means that the data you're trying to obtain must not be stored by the UI to begin with. Instead, you need to create a proper Data Model and use DataBinding to populate the UI with such data from the DataModel.
The responsibility of the UI is to show data, not store it. This is an important mindshift from the traditional approach where you would manually populate the UI with data and then retrieve the data from the UI.
WPF Two-way DataBinding capabilities make it easier to implement such scenarios in a clean, decoupled way.
Say you have a User class which has a Name and an Id property:
public class User
{
public int Id {get;set;}
public string Name {get;set;}
}
first step to show this in a DataGrid is to create a proper ViewModel that contains a collection of this class:
public class ViewModel
{
public ObservableCollection<User> Users {get; private set;}
public ViewModel()
{
Users = new ObservableCollection<User>();
//... Populate the collection here.
}
}
then you will use this class as the DataContext of your UI:
//Window's constructor
public MainWindow()
{
InitializeComponent();
//Here we set the DataContext:
DataContext = new ViewModel();
}
Finally, you create the proper DataBindings in XAML:
<DataGrid ItemsSource="{Binding Users}"
x:Name="DataGrid"/>
<!-- ... -->
<TextBox Text="{Binding SelectedItem.Id, ElementName=DataGrid}"/>
Notice how I'm using an ElementName binding to bind the TextBox directly to the DataGrid.
This is the preferred, professional approach to WPF. I suggest that you read all the linked documentation and try to familiarize yourself more with this approach.
If you're populating your DataGrid with a List<SomeObject>, for example, try this:
var row = ((SomeObject)DataGrid1.Items[0]);
Just cast the item back to your class type, then access the properties on it. You may want to do a check to make sure there's something displayed in the grid before accessing Items[0].
You can sort different columns in your grid, and this will always grab the record displayed at the top.
Edit, after seeing the following comment:
When I click on first row in DATAGRID, this function should/must display user's ID in the textbox below data grid. That is all.
You can bind directly to your grid, to display the property you want from the currently selected row.
<DataGrid x:Name="DataGrid1" />
<TextBox Text="{Binding ElementName=DataGrid1, Path=SelectedItem.UserId}" />
I have a ComboBox whose ItemSource is bound to a list of strings (idealy i would use an Enum), this is done using the MVVM pattern.
Now i want to bind an object to the ComboBox, it's called SelectedUser and i want to bind its property: UserType, which is a string.
So i have got this:
<ComboBox ItemsSource="{Binding Path=Usertypes}" SelectedValue="{Binding Mode=TwoWay, Path=SelectedUser.UserType}" />
It works and it does change the value of the selected user if i play with it, but the problem is, that it does not display anything in the ComboBox unless i select a user, and then change the ComboBox selection, then it works, but only for that user.
I tried playing around with DisplayMemberPath, SelectedValuePath and SelectedItem,
when i added those the ComboBox did not show anything in it (there were still options to select from, but they were invisible or something).
So what should i do? Is this a bug?
I have to mention that i have got another ComboBox that has a list of ints, and it works fine.
Update:
I was informed that I'm getting this issue because the string I'm comparing to the string in the comboBox, are not actually the same.
My string comes from the Entity Framework via Ria Services. (User.UserType)
And when it compares it to the list of strings in the ComboBox ItemSource, they are not equal, for some strange reason.
And i also heard, i might have to override Equal method for that check.
but I'm not sure where and how to do so.
Is the view notified if the SelectedUser changes? I could imagine that this might be the problem; if there is no such notification the ComboBox will not reload the SelectedValue if another user is selected, it will only update the binding if you make changes yourself.
If that is not it, you also need to consider that no selected value will be displayed unless the current value exactly matches one of values in the source list.
I have a WPF combobox. It took me ages to figure out how to make it text editable so that some kind of resonable event fires when both the user enters text and when the user picks an option in the list.
I used this method here (very bottom of the page)
http://social.msdn.microsoft.com/Forums/en/wpf/thread/13c6dfad-4062-41b9-85d8-0d5f23dd349b
But now I have an issue - if I have an option in the comboBox with the text "Apple" for example and i wish to type "Apricot" into the field, as soon as I type the letter A it picks Apple from the list. It leaves the caret after the A so I can continue to write Apricot. However due to the way things listen to the comboBox its vital I can prevent this behaviour but have no idea how.
Any help appreciated as always.
ComboBox.IsTextSearchEnabled = false;
Couldn't find that anywhere. Phew
The above solution works in code, below is how I would do it in XAML.
<ComboBox x:Name="cbCountry" IsTextSearchEnabled="False"
LostFocus="cbCountryLostFocus" KeyUp="cboCountryKeyUp">