我想绑定AutocompleteBox的SelectedText
和SelectedItem
属性,因为我的客户端希望能够输入文本并从列表中进行选择。它运作正常但是......
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");
}
}
答案 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();
}