I want to bind both the SelectedText and SelectedItem properties of an AutocompleteBox because my client wants to be able to input text and select from the list also. It's working properly but ...

The MainPage has one DataGrid. When I select a record from the Grid (i.e. SelectedItem), I want to set it in a popup window's AutocompleteBox. Some times it works but some times it doesn't.

What should I do for this issue?

This is my XAML:

<Sdk:AutoCompleteBox Grid.Column="3" Grid.Row="3" Height="18" Width="150" 
     IsTextCompletionEnabled="True" TabIndex="9" HorizontalAlignment="Left"

     Text="{Binding ElementName=ResEdit,Path=DataContext.SelectedDemoText,Mode=TwoWay}"
     ItemsSource="{Binding ElementName=ResEdit,Path=DataContext.DemoList,Mode=OneWay}"
     ItemTemplate="{StaticResource DemoTemplate}"
     ValueMemberPath="DemoCode" 
     LostFocus="AutoCompleteBox_LostFocus"
     Margin="0,0,21,0" Padding="0">
  </Sdk:AutoCompleteBox>

This property is in my view-model and bound to the DataGrid:

public InvoicesDTO SelectedInvoice
{
    get { return _selectedInvoice; }
    set
    {
        SelectedInvoice = value;
        SelectedDomoText = SelectedInvoice.DemoText.Trim();
        RaisePropertyChanged("SelectedInvoice");
    }
}
有帮助吗?

解决方案

You should not use both function SelectedText and SelectedItem in autocomplete. it's a bug of AutoCompleteBox..... A better way is to set the visiblity of the textbox and AutoCompleteBox on GotFocus and LossFocus. This Way You Will Defiantly Solve You Problem

 private void DemoAutoComplete_LostFocus(object sender, RoutedEventArgs e)
            {
                DemoTextBox.Visibility = Visibility.Visible;
                DemoAutoComplete.Visibility = Visibility.Collapsed;
                DemoTextBox.Text = OCRAutoComplete.Text;

                ((DemoVM)this.DataContext).SelectedDemoText = DemoAutoComplete.Text;
            }



private void DemoTextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        DemoAutoComplete.Text = OctTextBox.Text;
        DemoTextBox.Visibility = Visibility.Collapsed;
        DemoAutoComplete.Visibility = Visibility.Visible;
        DemoAutoComplete.Focus();
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top