Get text of RadAutoCompleteBox

davidweitzenfeld picture davidweitzenfeld · Apr 9, 2013 · Viewed 9.4k times · Source

How can I get the text of a RadAutoCompleteBox using RadControls Q1 2013 in C#?

autoCompleteBox.SelectedItem returns "ServerCrafterTelerikWPF.Command".

Edit 1: Here's my 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"/>

And I don't have any C# code. I just want, when a button is pressed, to get the text that is in the RadAutoCompleteBox.

Edit 2: And here's my collection:

public class Command
{
    public string ACommand { get; set; }
}

/// <summary>
/// A view model for MainWindow.xaml
/// </summary>
public class ViewModel
{
    public ObservableCollection<Command> Commands { get; set; }

    public ViewModel()
    {
        Commands = new ObservableCollection<Command>()
            {
                new Command() {ACommand = "stop "},
                // Other commands...
                // ...
                // ...
            };
    }
}

Answer

Xelom picture Xelom · Apr 9, 2013

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}"