Make ComboBox Edit Active When View Is Opened - wpf

I have a view that opens up on a button click. The view has a combo box that the user is requesting that when the view opens, the combo box is already selected so they don't have to click on it. Is there anyway to achieve this?

Are you saying that you would like to have an initial value selected in the ComboBox when you load the view? If you defined your ComboBox with static content, meaning the ComboBox's ItemSource property isn't bound to an object that was constructed using the results of a database query, you could set the IsSelected property of the ComboBoxItem you want selected to true. For example:
If I want to have the apples initially selected in the example below, I could do the following:
<ComboBox Name="fruits" >
<ComboBoxItem Content="apples" IsSelected="true"/>
<ComboBoxItem Content="pears"/>
<ComboBoxItem Content="oranges"/>
</ComboBox>

Related

ComboBox with Checkboxes on selective ComboBox Items

I have a ComboBox which takes it's values from ObservableCollection which gets it's data from a XML file.
I am able to display the data from XML to the ComboBox whose code is :
<ComboBox x:Name="ComboBox1" ItemSource="{Binding Path = <Property Name>}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedItem, UpdateSourceTrigger=PropertyChanged}">
Now I want to make modifications such that only some Items in the ComboBox have a checkbox which will be based on the text of the ComboBox Item.
If I use Data Template in ComboBox.ItemTemplate, then all the items of combobox will have the checkbox which I don't want.
Is there any way I can achieve this?
In the ItemTemplate, use the DataTemplate in which there will be triggers that change the Visibility of the flag according to the conditions you need.

How to load Combobox-items on dropdown

I need to work with an excel workbook, with some worsheets.
To load the available worksheets into a combobox, I need to open the workbook and iterate through all sheets and add them to the combobox. This cost a lot of time, the user needs to wait for. If the user doesn't want to change the preloaded property, this time is waisted. So I only want to fill in the listitems when needed but show the preselected property, that was loaded from a settingsfile on startup of the app.
So is there a way to load the items on dropdown? Or better, how can I bind a command that is triggerd on dropdown, to load the items?
<ComboBox SelectedItem="{Binding Worksheet}" ItemsSource="{Binding WorkSheets}" x:Name="cbWorkSheet" Padding="4">
... oh, I know how to bind a command to a button so I need a special tip for the combobox, not for commands in general :)
UPDATE
I solved to trigger the "loading" and the List is filled but I need to show the last selected value (that is loaded on startup as the bound property Worksheet)
<ComboBox
x:Name="cbWorkSheet"
SelectedItem="{Binding Worksheet}"
ItemsSource="{Binding WorkSheets}"
IsEditable="True"
Padding="4">
<i:Interaction.Triggers>
<i:EventTrigger EventName="DropDownOpened" SourceObject="{Binding ElementName=cbWorkSheet}">
<i:InvokeCommandAction Command="{Binding LoadBACnetWsCombobox}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
With that Binding, the Combobox remains empty till I dropdown it :(
Original Answer: Incorrect, Posted 2020/06/02:
If you need SelectedItem populated before you open the dropdown, then you should probably change your EventTrigger's EventName from DropDownOpened to be Loaded.
That way, before the combobox is shown to the user, LoadBACnetWsComboBox will run, and hopefully your Worksheet will be set. Then, the combobox will be shown to the user.
Modified Answer, Posted 2020/06/03:
If you want to just populate the initial displayed text, you can always set the Text property of the combobox. It will get overridden once you do selections from the drop down.
<ComboBox
...
Text="{Binding MyInitialValue}">

is it possible the binding to two properties?

I have a view model that has two properties. One of them is myDataGridSelectedItems, that is update in the selection changed event of the datagrid (I am using MVVM light to convert an event to command).
The second property is myText, that is the text that has a textbox in the view.
In my view I have a textBox which text depends on the selection of the dataGrid, if the selection is one item then I put the information of a column of the dataGrid in the textBox, if the selection is 0 or more than 1, then I clear the textBox.
To do that, I use the following code:
<TextBox Height="23" HorizontalAlignment="Stretch" Margin="5,26,0,0" Name="mytextBox" VerticalAlignment="Top"
Text="{Binding ElementName=Principal, Path=DataContext.MyDatagridSelectedItems, Converter={StaticResource TextBoxValueConverter}}">
This works fine because when I select one row in the data grid the textBox has text (the text that the convert returns) and is empty when I select more that one or unselect all rows.
However, in this way the property myText is not update because I don't set the binding, because the binding of the Text property in the axml use the converter, not the property myText of the view model.
So I was wondering if it possible to set two bindings in the Text property of the textBox, or if exists some way to update the myText property in the view model when the text in the TextBox changes.
Thanks.
You are doing it the wrong way around:
Right now, you have view logic encoded in a converter in the view. But view logic is precisly what the view model is there for.
You should have a property for the text of that text box in the view model and bind the text box only to that property.
In the view model you change its value according to the selection.

Silverlight GridView and Combobox

I have a combobox in a gridview in my silverlight application.
<Controls1:GridViewComboBoxColumn Header="Accomplishment Category"
ItemsSource="{Binding AccomplishmentCategoryList}"
DataMemberBinding="{Binding AccomplishmentCategoryValue}"
SelectedValueMemberPath="{Binding AccomplishmentCategoryValue}">
</Controls1:GridViewComboBoxColumn>
I am able to get my grid to display the results for its own itemSource.
<Controls1:RadGridView x:Name="Accomplishments" Grid.Row="1" CanUserInsertRows="True"
ShowInsertRow="True" CanUserDeleteRows="True" RowIndicatorVisibility="Visible"
IsReadOnly="False" ItemsSource="{Binding AccomplishmentResults, Mode=TwoWay}">
The AccomplishmentResults collection binds perfectly. my issue is the combobox does not display anything until you actually click on the column that contains the combobox control. so that column looks empty, when you click on the column the results are displayed, when you click again you get the combobox that will show the items in that collection..if you select anything other than that column those values disappear and are empty again. if you select the row they remain empty..it is only when the column is selected that the values appear.
By default ListView and DataGrid only displays text till the item is clicked and item becomes editable.
You will have to create Templated Column, that will always display combobox as declared in template.

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

Resources