How to use update source trigger on Wpf Combobox which is editable? - wpf

I have a combo box (in my wpf-mvvm app). I have set IsEditable = true. But the "property changed event" is getting fired when I start typing.
How can I set UpdateSourceTrigger = Propertychanged here ?
Also..I need to call a validation function if user has entered new value ( i mean other than those available in list ..using edit functionality).
Any help will be appreciated.
<ComboBox ItemsSource="{Binding Path = PlanTypeBasedContractNumberList }" Width="90" IsEditable="True"
SelectedValue="{Binding GeneralCharacteristicsDataContext.ContractNumber.Value}">
</ComboBox>

In an editable ComboBox, the SelectedItem and SelectedValue properties refer to the Popup items, not the editable item. Once you start typing, the SelectedItem becomes "unselected" and that's why the event fires.
To bind to the value of the TextBox of the ComboBox, use the Text property:
<ComboBox IsEditable="True" Text="{Binding Path=..., UpdateSourceTrigger=...}">

Related

WPF Binding textbox to listview stops after textbox value is updated

I have a listview that when selected, will populate data from the selected lineitem into separate textboxes.
I used databinding to accomplish the task, which seems to work fine:
<TextBox x:Name="SKU_TxtBox" HorizontalAlignment="Left" Height="23" Margin="10,21,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{Binding SelectedItem.SKU, ElementName=Inventory_ListView, Mode=OneWay}" />
The above code works correctly. The problem starts if in the codebehind I have to change the textbox value; afterwards the databinding stops.
SKU_TxtBox.text = ""
After the above line runs, the textbox will remain blank no matter what is selected in the listview.
When working with bindings, you should always manipulate only the binding source.
Inventory_ListView.SelectedItem.SKU = ""
Or a better approach is to have a View Model bound to the view. in which you define a Dependency Property (currentSKU). Then bind it to both Inventory_ListView.SelectedItem and SKU_TxtBox.text. Then it will be:
CurrentSKU = ""

Combobox: Get text and selected item in mvvm way

The combobox is editable so user can also write. I have two usecases:
Get the text from combobox in a Lostfocus way, when user writes
something in the box and when he presses "Tab" then I want the text
from the combobox and I add the value in the itemsSource list.
When the users makes the selection from the combobox dropdown, I want that
selected item as soon he selects it and this time I dont
want to have it in Lostfocus manner but somewhat like
PropertyChanged way.
I tried the code which is given below:
<ComboBox Margin="3" x:Name="Combobox" SelectedItem="{Binding SelectedPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Text="{Binding PathLocation, UpdateSourceTrigger=LostFocus, ValidatesOnNotifyDataErrors=True}" IsTextSearchEnabled="True" VerticalContentAlignment="Center" ItemsSource="{Binding SelectedPaths}" IsEditable="True" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" HorizontalAlignment="Stretch"/>
Things worked fine for the first time when the application starts but after some interactions the problem arises. When the user starts typing in the combobox the SelectedItem property of combobox triggers which is contrary to what I want in the first use case.
In short: when the user writes something in the combobox I want to have it in a Lostfocus manner and when he makes the selection from the dropdown of combobox I want to have it in a PropertyChanged manner.
Let me know if more details are required.
I removed the "IsTextSearchEnabled" property but it also didnt work then I came to know that "IsTextSearchEnabled" property of Comobobox is by default true, which is causing some values suggested by the combobox are setting in my properties. As soon as I made the "IsTextSearchEnabled" to false, it is working fine.

WPF Combobox SelectedIndex Not Working

I have a WPF user control (FileSelectionView.xaml) with a combo box that displays data. My WPF looks like:
<ComboBox Width="250"
HorizontalAlignment="Left"
ItemsSource="{Binding Path=FileTypes}"
SelectedItem="{Binding Path=FileType, Mode=TwoWay}" />
In my View Model file (FileSelectionViewModel.cs), I have a List that binds to this control that successfully works. The data looks like:
<Please select a file>
File Type 1
File Type 2
I have tried to set the SelectedIndex property to 0 so that "<Please select a file>" shows up when the user control renders, but it is not working. It doesn't show anything, but when I click on the combo box, I do see all my items.
Is there something I'm missing?
Instead of using SelectedIndex, After updating the ItemsSource, update the selected item with the following code from viewmodel
FileType = "Please select a value";
IT works just fine, if you do it in XAML, I don't see it in your XAML, did you forget?
<ComboBox Width="250"
HorizontalAlignment="Left"
ItemsSource="{Binding Path=FileTypes}"
SelectedItem="{Binding Path=FileType, Mode=TwoWay}"
SelectedIndex="0"/>
Note that only will work initially, then you'll have to reset it again when you need it.. via trigger, or code behind.

Binding properties of selected object in combobox to TextBox in WPF

I'm using MVVM to load text files and to show their content.
Model
MyFile.cs has a Name and Text // Implements the INotifyPropertyChanged
MyFileRepository.cs // collection of my loaded files
ViewModel
OpenFileCommand to load a file and add it to the _filerepository object
FileCollection that's bound to the View
View
Button to fire the OpenCommand
ComboBox to show the names of the loaded files
TextBox to show the content of selected file in combobx
<Button Name="OpenFile" Command="{Binding OpenFileCommand}">
<ComboBox Name="FilesList" ItemsSource="{Binding Path=FileCollection}" DisplayMemberPath="Name" />
<TextBox Name="FileContent" Text="{Binding the Text of selected file in combobx "/>
How to bind the Text property of MyFile selected in combobx to the TextBox?
The easiest approach would be element binding:
<TextBox Name="FileContent"
Text="{Binding SelectedItem.Text,ElementName=FilesList} />
So that's binding to the Text property of the SelectedItem in your FilesList ComboBox, which (if everything's wired up the way I think it is) is of type MyFile.
Without element binding you could add the property "SelectedItem" (Type: MyFile) to your VM and bind it to the SelectedItem property of your combobox (mode=twoway).
Now your TextBox.Text-Property should look like:
<TextBox Name="FileContent"
Text="{Binding SelectedItem.Text} />

Binding a wpf combobox to an element

A very basic question : I want to bind a wpf ComboBox to a selected element, so it displays the same value as the TextBox:
<StackPanel DataContext="{Binding ElementName=comboBox1, Path=SelectedItem}">
<TextBox Text="{Binding Path=Id}"></TextBox>
<ComboBox ....
When changing the value of combobox1, the id of the selected item shows in the TextBox. How do I bind a second ComboBox to the selected item in the first ComboBox to show the same value?
UPDATE : WPF ComboBox…how to set the .Text property? solved it for me : I needed the possibility to add items other than known beforehand, so the IsEditable property should have been true.
WPF ComboBox…how to set the .Text property? solved it for me : I needed the possibility to add items other than known beforehand, so the IsEditable property should have been true.
Looks ok to me. Maybe you should post a more complete examole. Do you have Selector.IsSynchronizedWithCurrentItem set on the combobox?

Resources