Get text of RadAutoCompleteBox

蓝咒 提交于 2019-12-04 14:47:14

You should take it from the SelectedItem property. Cast it to your class and then get it from MyClass.ACommand

And I suggest binding SelectedItem with Mode=TwoWay in your ViewModel can help a lot.

Just add a Member to ViewModel which is implementing Command like:

private Command _SelectedItem;

public Command SelectedItem 
{ 
   //get set with INotifyPropertyChanged 
}

Then from the xaml: Bind RadAutoCompleteBox's SelectedItem Property like:

SelectedItem="{Binding SelectedItem, Mode=TwoWay}"

I have reproduced the problem.

Yes. I had the same problem. And I found the problem and the answer too.

I got the problem because of using of type string for the selected item in my view model.

private string selectedCommand;

public string SelectedCommand
{
    get
    {
        return selectedCommand;
    }
    set
    {
        selectedCommand = value;
        NotifyPropertyChanged("SelectedCommand");
    }
}

Use the type as Command class and your problem will be solved.

private Command selectedCommand;

public Command SelectedCommand
{
    get
    {
        return selectedCommand;
    }
    set
    {
        selectedCommand = value;
        NotifyPropertyChanged("SelectedCommand");
    }
}

Bind the SelectedItem property of the RadAutoCompleteBox in the XAML

<telerik:RadAutoCompleteBox 
            x:Name="txtboxCommand" 
            ItemsSource="{Binding Commands, Source={StaticResource ViewModel}}" 
            DisplayMemberPath="ACommand"  
            AutoCompleteMode="Append" 
            HorizontalAlignment="Left" 
            telerik:StyleManager.Theme="Modern" 
            Margin="280,405,0,0" 
            VerticalAlignment="Top" 
            Width="330" 
            Height="30" 
            KeyDown="txtboxCommand_KeyDown"
            SelectedItem="{Binding SelectedCommand, Mode=TwoWay}"/>

If you wanna get the selected item by the code-behind, convert the selected item to the Command class type.

var selectedItem = autoCompleteBox.SelectedItem as Command;

And actually there can be multiple selected items. In that case you have to define a collection of Command objects.

private ObservableCollection<Command> selectedCommands;

public ObservableCollection<Command> SelectedCommands
{
    get
    {
        return selectedCommands;
    }
    set
    {
        selectedCommands = value;
        NotifyPropertyChanged("SelectedCommands");
    }
}

And bind it to the SelectedItems property (plural of SelectedItem) of the RadAutoCompleteBox control.

SelectedItems="{Binding SelectedCommands, Mode=TwoWay}"

And make sure you have initiated the SelectedItems.

this.SelectedCommands = new ObservableCollection<Command>();

The SearchText property of the RadAutoCompleteBox should provide you the value.

According to the documentation it gets or sets the string that is into the TextBox part of the RadAutoCompleteBox. The SearchText value is used to filter the RadAutoCompleteBox' ItemsSource.

If you want to get the "Text" of the selected item of the AutocompleteBox, then you need to cast it to the specified type. In your case it is of type ServerCrafterTelerikWPF.Command.

var selectedItem = autoCompleteBox.SelectedItem;

if (selectedItem is ServerCrafterTelerikWPF.Command) {
  var selectedCommand = selectedItem as ServerCrafterTelerikWPF.Command;

  string textOfAutoCompleteBox = selectedCommand.ACommand;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!