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>
Related
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}">
I am using the following code to use a ComboBox inside my DataGridTemplateColumn:
<DataGridTemplateColumn Header="MyHeader" Width="50">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="Auto" Text="{Binding Path=MyVal}" ToolTip="{Binding MyDisplayName}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Width="50" Height="17" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}},Path=DataContext.MyList}"
SelectedValuePath="MyValPath" SelectedValue="{Binding MySelectedVal}" SelectedItem="{Binding MyObject}" DisplayMemberPath="MyDisplayName"
FontSize="12" >
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
CellTemplate is for showing my text (custom text for selected value) and CellEditingTemplate contains my ComboBox which has the actual list.
When I select a value from my drop-down, I have to click on another part of the data grid to get DataGridDiagnosticCellEditEnding fired.
I want to get it fired as soon as I select a value or change a value from my ComboBox.
I tried adding the following code, but it didn't work:
<ComboBox Width="50" Height="17" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}},Path=DataContext.MyList}"
SelectedValuePath="MyValPath" SelectedValue="{Binding MySelectedVal}" SelectedItem="{Binding MyObject, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="MyDisplayName" FontSize="12" >
Also I tried adding IsSynchronizedWithCurrentItem="True".
I gave a try to your code, and ve got a fix :
On the ComboBox of the DataGrid Column, subscribe to the closing event
Implement the event Handler and raise a routed Event.
CommitEditCommand belongs to the DataGrid class :
private void ComboBoxDropDownClosed(object sender, EventArgs e)
{
DataGrid.CommitEditCommand.Execute(datagrid1,datagrid1);
}
From there you fall in the DataGrid.CellEditting breakpoint
Here is the working solution : http://1drv.ms/1LFB5Gc
Regards
I have a View that displays a Part. All parts contain a list of identifiers. In my View I display Part Properties and a DataGrid with all the Identifiers of that part.
Now if I change a value of an identifier, I want another value update to the default. But if I change my identifier value and set the default of the other property - my DataGrid does not update. Only if I click on the cell, then it gets updated after losing focus.
How can I update the View automatically?
I guess the problem is that I do not want to update a direct property of the Part, but a Property in a List that is a property of the Part.
View
<DataGrid>
<DataGridTemplateColumn Header="Company">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox x:Name="CompanyEditComboBox"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Companies}"
SelectedItem="{Binding Company, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectionChanged = "CompanyEditComboBox_SelectionChanged" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Company}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="CompanyType">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox x:Name="CompanyTypeEditComboBox"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.CompanyTypes}"
SelectedItem="{Binding IdentificationCompanyType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding IdentificationCompanyType, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid>
View Code-Behind
private void CompanyEditComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var vm = (PartViewModel)DataContext;
var box = (ComboBox) sender;
var c = (Company) box.SelectedItem;
vm.SetDefaultCompanyType(c);
}
ViewModel
public void SetDefaultCompanyType(Company c)
{
SelectedIdentification.IdentificationCompanyType = c.DefaultCompanyType;
OnPropertyChanged("IdentificationCompanyType");
}
Solved it. I had to add a
OnPropertyChanged("IdentificationCompanyType");
into the setter of the IdentificationCompanyType in the Identification class. After that it got automatically updated in the DataGrid.
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.
How do you find the index of a ListBoxItem if it is set within a DataTemplate as a Textbox control? Here is the WPF:
<ListBox Name="ScriptEditor" Margin="10" Height="291" ItemsSource="{Binding Path=Script}" SelectionChanged="ScriptEditor_SelectionChanged_1" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Command}"PreviewMouseDoubleClick="Command_DoubleClick" GotFocus="ScriptEditor_GotFocus" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When I gain focus of the textbox (text is bound to an observableCollection), I cannot simply use the SelectionChanged Event on the ListBox. I would like to know how I can determine the index of the textbox I have gained focus in.
Thanks
You could bind the AlternationCount to the Script.Count then add the AlternationIndex from the ItemsControl(ListBox) to the Textbox Tag property so you can access from your GotFocus event handler.
Example:
<ListBox Name="ScriptEditor" Margin="10" Height="291" ItemsSource="{Binding Script}" AlternationCount="{Binding Script.Count}" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding ., Mode=OneWay}" GotFocus="ScriptEditor_GotFocus"
Tag="{Binding Path=(ItemsControl.AlternationIndex), Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
private void ScriptEditor_GotFocus(object sender, RoutedEventArgs e)
{
int index = (int)(sender as TextBox).Tag;
}