-->

获取RadAutoCompleteBox的文本(Get text of RadAutoComplet

2019-08-20 14:37发布

我怎样才能使用的RadControls 2013年第一季度在C#中RadAutoCompleteBox的文字?

autoCompleteBox.SelectedItem返回"ServerCrafterTelerikWPF.Command"

编辑1:这是我的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"/>

而且我没有任何的C#代码。 我只是想,当按下一个按钮,来获取在RadAutoCompleteBox文本。

编辑2:这是我的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 1:

你应该把它从SelectedItem属性。 它投射到你的类,然后得到它MyClass.ACommand

我建议结合SelectedItemMode=TwoWay您的视图模型可以有很大的帮助。

只需添加一个成员到视图模型,其正在实施类似命令:

private Command _SelectedItem;

public Command SelectedItem 
{ 
   //get set with INotifyPropertyChanged 
}

然后从XAML:绑定RadAutoCompleteBox的SelectedItem属性,如:

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


Answer 2:

我已复制了问题。

是。 我有same problem 。 而且我found这个问题,答案了。

因为使用字符串类型的在我的视图模型所选项目问题。

private string selectedCommand;

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

使用类型为Command类和您的问题将得到解决。

private Command selectedCommand;

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

BindSelectedItem的财产RadAutoCompleteBoxXAML

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

如果你想获得的代码隐藏所选择的项目,所选项目转换为命令类类型。

var selectedItem = autoCompleteBox.SelectedItem as Command;

而实际上可以有multiple selected items 。 在这种情况下,你必须定义一个collection of Command objects

private ObservableCollection<Command> selectedCommands;

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

并将其绑定到SelectedItems的RadAutoCompleteBox控制的属性(的SelectedItem的复数)。

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

并确保您发起SelectedItems。

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


Answer 3:

SearchText的财产RadAutoCompleteBox应该为您的值。

根据该文件它获取或设置是进入RadAutoCompleteBox的文本框部分的字符串。 所述SEARCHTEXT值用于过滤RadAutoCompleteBox”的ItemsSource。

如果你想获得的AutocompleteBox的所选项目的“文本”,那么你需要将它转换为指定的类型。 你的情况是类型的ServerCrafterTelerikWPF.Command

var selectedItem = autoCompleteBox.SelectedItem;

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

  string textOfAutoCompleteBox = selectedCommand.ACommand;
}


文章来源: Get text of RadAutoCompleteBox