IsEditable on a combobox is not having an effect - c#

I want to make my ComboBox editable. My Combobox displays a list of countries, and I would like my users to be able to type in the country to search for it. However, in its current state, it doesn't do that. What have I done wrong?
<ComboBox x:Name="CmbCountryList" Width="150"
IsEditable="True" IsTextSearchEnabled="True"
IsTextSearchCaseSensitive="False" StaysOpenOnEdit="True"
TextSearch.TextPath="CountryName"
ItemsSource="{Binding CountryMasterList, Mode=TwoWay}"
DisplayMemberPath="CountryName"
SelectedValuePath="CountryID"
SelectedItem="{Binding Path=CountryObj, Mode=TwoWay, ValidatesOnDataErrors=True}"
Text="{Binding Path=CountryName, Mode=TwoWay}"
IsSynchronizedWithCurrentItem="False" />

Check http://weblogs.asp.net/okloeten/archive/2007/11/12/5088649.aspx for an example of how you can implement this.

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.

Bind combobox property of selected item in combobox to field

I have an issue with binding in ComboBox. I have searched in Google and so, but I haven't been able to find the answer.
I have silverlight form with combobox like this:
<ComboBox x:Name="FirmBox"
Grid.Row="23"
Grid.Column="1"
Grid.ColumnSpan="2"
Margin="5,5,5,0"
SelectedValuePath="{Binding Path=Value, Mode=TwoWay}"
SelectedItem="{Binding Path=Firm, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Value}"/>
<TextBlock Text="{Binding Path=Key}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
And the ItemsSource is an
ObservableCollection<KeyValue<String, KeyValue<String, String>>>
So I've figured out how to display this in right way, but I don't know how to bind the selected item to my KeyValuePair<String, String>
field. That does not seem obvious to me. So I need to bind the value of selected item to my field and don't know how to do it.
Thank you.
The solution was simple as always:
SelectedValuePath="Value"
SelectedValue="{Binding Path=Firm, Mode=TwoWay}">

Autocomplete causes text to scroll to right

Autocomplete on my editable combobox works fine for short items.
However, if the selected string is too wide and does not fit in the combobox, it scrolls horizontally to the right, presumably as a result of the autocomplete feature selecting the remainder of the string.
This hides the current location of the point at which the user is typing.
How do I get it to keep the position of the caret visible to the user?
<ComboBox x:Name="comboBoxCustomer"
ItemsSource="{Binding Source={StaticResource customerViewSource}}"
TextSearch.TextPath="CustomerDisplay"
SelectedValue="{Binding CustomerID, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
SelectedValuePath="ID"
SelectionChanged="comboBoxCustomer_SelectionChanged"
StaysOpenOnEdit="True"
IsEditable="True" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding CustomerDisplay}" Foreground="{Binding ActiveColour}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Use ScrollToHome method on textbox of combobox.
private void comboBoxCustomer_KeyUp(object sender, KeyEventArgs e)
{
TextBox tb = (TextBox)comboBoxCustomer.Template.FindName("PART_EditableTextBox", comboBoxCustomer);
tb.ScrollToHome();
}
You should try this.
<ComboBox x:Name="comboBoxCustomer"
ItemsSource="{Binding Source={StaticResource customerViewSource}}"
TextSearch.TextPath="CustomerDisplay"
SelectedValue="{Binding CustomerID, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
SelectedValuePath="ID"
SelectionChanged="comboBoxCustomer_SelectionChanged"
StaysOpenOnEdit="True"
IsEditable="True" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding CustomerDisplay}" Foreground="{Binding ActiveColour}" **TextAlignment="Left"** />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

DataGrid ComboBox has no selected item when clicked the first time

Recently I asked similar questions here and here, but could not fix the issue properly.
I have a DataGrid with ComboBox that can contain either a selected item or not. But if it does then the ComboBox should select it when the dropdown is opening which is doesn't.
Currently I have this code which works except when opening the dropdown the first time. It is nothing selected.
<DataGridTemplateColumn Header="Company">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Companies}"
SelectedItem="{Binding Company, Converter={StaticResource NullValueConverter}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Company, Converter={StaticResource NullValueConverter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
The NullValueConverter prevents exceptions if the Company is null.
If your company was not a string as you mentioned in the comments then it can't display the Name unless you set
DisplayMemberPath="Name" or create an ItemTemplate.
You should use SelectedItem="{Binding
Company,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" to update
the value as soon as it changes because if you don't it will only
raise ProperyChanged when you focus another cell or row.
if your Property was immutable or a value type then you
should use SelectedValue="{Binding Path=Company,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" SelectedValuePath="Content"
.

WPF Combobox property IsTextSearchEnabled - how to not allow own text

I have a Combobox in a DataGrid which allows IsTextSearchEnabled. This works but the user is allowed to put their own text when the item is not found in the combobox. Is there a property that will stop this, or what can I do to stop the user adding their own text?
The xaml
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding DataContext.Types,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=Window}}"
x:Name="cmbDeploymentEditType"
SelectedItem="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValue="Type"
Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsTextSearchEnabled="True"
IsSynchronizedWithCurrentItem="False"
IsEditable="True">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
Thanks
There are multiple ways you can handle this, you can use a control that stops this like the XamMultiColumnComboEditor with CustomValueEnteredAction setting. Or you could do validation to enforce the rule you want.

Categories