Autocompletebox SelectedText Bug

时间:2012-12-28 05:47:46

标签: silverlight-4.0 silverlight-3.0 silverlight-5.0 silverlight-toolkit silverlight-2.0

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

MainPage有一个DataGrid。当我从网格中选择一条记录(即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");
    }
}

1 个答案:

答案 0 :(得分:3)

您不应在自动填充中同时使用 SelectedText SelectedItem 功能。这是 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();
    }