Binding a wpf combobox to an element - wpf

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?

Related

Bind multiply Comboboxes to a TextBox

I've bound a ComboBox to my TextBox
<TextBlock Grid.Row="1" Name="DescriptionText" Text="{Binding ElementName=ScreenLocations, Path=SelectedItem.Description}" />
I have 4 ComboBoxes in my grid. What I want to do is, every time I select an item from any ComboBox, update the TextBox with the selected objects Description property.
Is it possible to bind multiple ComboBoxes to one TextBox, or would I need to use an event of some sort?
Create a property in your ViewModel and bind all your comboboxes' 'selectedItem' property to it (Use Mode="OneWayToSource", this will prevent changes on selectedItem of one ComboBox to affect the other), then bind your TextBox to the same property created in the VM with Mode="OneWay". Don't forget to implement INotifyPropertyChanged in your VM.

ComboBox has extra space at the bottom

I have a ComboBox that shows empty space below its values. See picture below.
The data in the view model is set in a button click handler. When I set the values in the initialization of the view model the ComboBox is fine. When I try to create a small example the ComboBox also has the expected size. It seems it depends somehow on the context where I set the values in the view model but I cannot figure out. I hope someone can give me a hint.
Code in the view model
Repositories.Clear();
Repositories.Add("One");
Repositories.Add("Two");
Repositories.Add("Three");
SelectedRepository = "One";
Code in XMAL
<ComboBox MinWidth="150"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
VerticalContentAlignment="Center"
IsEnabled="{Binding CT.Connected}"
ItemsSource="{Binding CT.Repositories,
UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding CT.SelectedRepository}"/>
The ItemsSource you are binding to needs to be an ObservableCollection. The ComboBox will display the initial blank space if you bind the ItemsSource to any enumerable type that doesn't raise property changed when its items change.
When I try to create a small example the ComboBox has the expected size.
By that logic it maybe not the combobox, but the data which is in the list. Can you verify that there are not 8 items where the textual value to display is empty for the last four items or so?
Or
Maybe a style is causing the extra space. Try removing the style from the combobox such as this
<ComboBox Style="{x:Null}"/>
and see if it has any effect to the visual result.
Or
Also how about not setting the data and see if the drop down has the same size?

WPF MVVM Textbox and Datagrid binding

Hi I have a WPF MVVM application. I have a DatagridView and I have binded a list Items . I also have currentItem which I have set as Selected Item in Xaml
Now I have Binded another textbox to the CurrentItem.Name and I want Textbox's content to change whenever I select another item in the grid .
I have ViewModel which has INotifyProperty change implemented so don't post those as answers
i would think you can directly bind to the selected item of your datagrid, but its not tested :)
<TextBlock DataContext="{Binding ElementName=mygrid, Path=SelectedItem, Mode=OneWay}"
Text="{Binding Name, Mode=OneWay}"/>
I had missed calling OnProperty in setter of CurrentItem Object . So the UI was never informed about this .

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

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=...}">

Making use of DataGrid SelectedItem property to control look of TemplateColumn

When creating a custom column design for a Silverlight DataGrid, is there any way to bind or make use of the DataGrid's SelectedItem property?
I wish to display a static element, but have it only visible for the row that is selected.
A simple example of what I'm after:
<data:DataGrid>
<data:DataGrid.Columns>
...
<data:DataGridTemplateColumn>
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="Selected" Visibility="{IsSelected ? Visible : Collapsed}">
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
...
</data:DataGrid.Columns>
</data:DataGrid>
The column does not need to contain any other elements or bindings.
It does not need to control the Visibility property specifically - any means that hides the element on all rows except the selected row will do.
Can this be done with styles? (Note that there is already one style applied to the DataGrid).
Does RowDetails meet your needs?
Ultimately I made do with a work around - the class used as the ItemSource had a Selected property added which was automatically updated to sync with changes to the list. Then I added a Visiblity property (I could have used a converter as well) to converted the boolean selected to a visibility value, which was used to control the visual appearance of controls in a column of the selected item in the list.

Resources