6

我想绑定 AutocompleteBox 的SelectedTextSelectedItem属性,因为我的客户也希望能够输入文本并从列表中进行选择。它工作正常,但...

MainPage 有一个DataGrid。当我从 Grid 中选择一条记录(即 SelectedItem)时,我想将它设置在弹出窗口的 AutocompleteBox 中。有时它会起作用,但有时却不起作用。

这个问题我该怎么办?

这是我的 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>

此属性在我的视图模型中并绑定到 DataGrid:

public InvoicesDTO SelectedInvoice
{
    get { return _selectedInvoice; }
    set
    {
        SelectedInvoice = value;
        SelectedDomoText = SelectedInvoice.DemoText.Trim();
        RaisePropertyChanged("SelectedInvoice");
    }
}
4

1 回答 1

3

您不应该在autocomplete中同时使用SelectedTextSelectedItem函数。这是AutoCompleteBox的一个错误.....更好的方法是在 GotFocus 和 LossFocus 上设置文本框AutoCompleteBox的可见性。这种方式你会挑衅地解决你的问题

 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();
    }
于 2013-02-04T06:03:26.457 回答