WPF ComboBox: Text value doesn't affect SelectedItem - c#

If i load string value in dependency property "Text" of ComboBox. ComboBox does not pair string value with ItemsSource objects and i don't have SelectedItem filled.
Need SelectedItem for ToolTip!!
<ComboBox x:Name="FieldComboBox" IsEditable="True" IsTextSearchEnabled="True"
Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="DataCode"
ItemsSource="{Binding FieldAlternative.FieldParts[0].DataItems}"
ToolTip="{Binding Path=SelectedItem.Description, ElementName=FieldComboBox}"/>
If i've empty Text and write, it works fine. If i've opened form with value in Text i have SelectedItem null but value Text is displayed.
Any solution how to forced pairing Text with objects in ItemsSource to fill SelectedValue, or any better solution of my problem.
Thx

You can set a default value to your combobox by the SelectedItem property which will be binded to your Text Field in your ViewModel:
SelectedItem="{Binding Path=Text, Mode=TwoWay}"
So your combobox element becomes like this:
<ComboBox x:Name="FieldComboBox" IsEditable="True" IsTextSearchEnabled="True"
Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding Path=Text, Mode=TwoWay}"
DisplayMemberPath="DataCode"
ItemsSource="{Binding FieldAlternative.FieldParts[0].DataItems}"
ToolTip="{Binding Path=SelectedItem.Description, ElementName=FieldComboBox}"/>

Related

XAML how to bring an entire datatable into a popup window

In the previous window I have a table. I can access specific data entries.
for example the code below takes the Description from the selected row, and places it inside of the textbox.
<TextBlock Text="Description" Style="{StaticResource tabTextBlock}"/>
<ComboBox ItemsSource="{Binding Vwr.Table.Tbl, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Description" SelectedValuePath="Description"
SelectedIndex="{Binding Vwr.Table.SelectedRowIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Text="{Binding Vwr.Table.Vals[2].Val, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsEditable="True" Style="{StaticResource tabTextBox}" Height="54"/>
My question is:
How would I port the entire Table, so that it would be displayed in a DataGrid/Table format inside of an XAML Window?
Assuming your data context is similar to the example you gave, this should work:
<DataGrid ItemsSource="{Binding Vwr.Table.Tbl}" />
By default, the DataGrid will automatically generate columns for each property in the item type.

Two way data Binding issue with combo box - WPF

I have a View that contains a Combobox. The Combobox SelectedItem property is data bound to SelectedX property of View Model as two way data binding. When the viewModel is initialized, the SelectedX property is set correctly. But after that when the view renders, it resets the value of SelectedX(since the binding is two-way).
So the two way data binding for the Combobox is basically not working. Please advise.
This is the xaml for my view. I initialize the View model first with apprpriate values for Relationships and SelectedX. When the view renders, the combo box resets the value for SelectedX. (I figured that by adding breakpoints). Hope this helps
<ComboBox Grid.Row="1" Grid.Column="1" Margin="5" Background="White" BorderBrush="DarkGray"
SelectedItem="{Binding SelectedX, Mode=TwoWay}"
ItemsSource="{Binding Relationships}" DisplayMemberPath="Value"
SelectedValuePath="Value" SelectedValue="{Binding Key, Mode=TwoWay}"
IsEditable="False" IsReadOnly="True" />
SelectedValue="{Binding Key, Mode=TwoWay}"
This will change the SelectedItem to its SelectedValue.

AutoCompleteMode for combobox in c# not available

I want my ComboBox to be searchable and hence I found that setting the AutoCompleteMode property of ComboBox does this. But when I try to get that property, IntelliSense doesn't show that property. Can someone help me in getting this?
<ComboBox
Grid.Column="3"
ItemsSource="{Binding Path=Students, Mode=OneWay}"
SelectedValuePath="ID"
Name="Combobox"
DisplayMemberPath="Name"
SelectedValue="{Binding Path=SelectedStudentId, Mode=TwoWay}"
Visibility="{Binding Path=IssuesVisible,UpdateSourceTrigger=PropertyChanged}">
</ComboBox>
In the code-behind file, I tried Combobox.AutoCompleteMode but the IntelliSense is showing error as I started typing.
Looking at the Combobox MSDN Documentation , there is no such property as AutoCompleteMode. Try StaysOpenOnEdit and IsEditable
<ComboBox StaysOpenOnEdit="True" IsEditable = "True" />

How to get a value from an edited TextBox before a ListBox item is selected?

I have a Silverlight application with a formular. The formular has a TextBox and a ListBox.
<TextBox Text="{Binding Value, Mode=TwoWay}/>
<ListBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
</ListBox>
I focus the TextBox and write something into it. After this I select an item in the ListBox. The ViewModel seems to set "SelectedItem" BEFORE "Value". Why? How can I fix the order? I mean I need to process the TextBox before the ListBox.
Thanks
Unfortunately, that looks like a bug. Same example on WPF will cause the TextBox to be set before the ListBox. You would have to find another way of doing what you want by slightly changing the logic of how you app UI functions.
You can update your TextBox Text property binding on TextChanged event, not LostFocus how it is now. Here is a sample.
The TextboxBinding is by default updating on LostFocus, try changing your binding to this:
<TextBox Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}/>
<ListBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
</ListBox>

Issue with ComboBox and ObservableCollection<string>

Here's the Xaml for my combobox:
<ComboBox Grid.Column="4"
Grid.Row="3"
ItemsSource="{Binding Path=Users, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:AdvancedSettingEditor}}}"
Margin="5"
x:Name="UserPicker"
SelectedValue="{Binding Path=StandAloneUserName, Mode=TwoWay}"
TabIndex="1"
Visibility="{Binding Converter={StaticResource InvertedBoolToVisibility}, Path=LoginRequired}" />
In the code behind, I have a simple ObservableCollection of strings:
public ObservableCollection<string> Users { get; set; }
The Users collection is loaded with string data from the database.
There is also a DependencyProperty that is bound to the window's DataContext that has a property called StandAloneUserName. The object stored in this property implements INotifyPropertyChanged.
When I run the program, the combo box has all of the names in the Users collection in the drop down list, but the box is blank. The control is not losing the value of the field, so it just doesn't know what to display.
How do I get my ComboBox to display the name that is in the StandAloneUserName property?
Tony
Set SelectedItem instead of SelectedValue
<ComboBox SelectedItem="{Binding Path=StandAloneUserName, Mode=TwoWay}" ... />
I'm assuming it's evaluating as "doesn't exist" since you don't have SelectedValuePath set to anything.

Categories