获取 RadAutoCompleteBox 的文本



如何在 C# 中使用 RadControls Q1 2013 获取 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...
                // ...
                // ...
            };
    }
}
你应该

SelectedItem属性中获取它。将其投射到您的班级,然后从MyClass.ACommand获取

我建议将SelectedItem与 ViewModel 中的Mode=TwoWay绑定会有很大帮助。

只需将一个成员添加到 ViewModel,该成员正在实现如下命令:

private Command _SelectedItem;
public Command SelectedItem 
{ 
   //get set with INotifyPropertyChanged 
}

然后从 xaml:绑定 RadAutoCompleteBox 的 SelectedItem 属性,如下所示:

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

我已经重现了这个问题。

是的。我有same problem.我也found问题和答案。

由于对视图模型中的选定项目使用字符串类型,我遇到了问题。

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

使用类型作为命令类,您的问题将得到解决。

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

Bind RadAutoCompleteBoxXAML中的SelectedItem属性

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

如果要通过代码隐藏获取所选项,请将所选项转换为 Command 类类型。

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

并将其绑定到 RadAutoCompleteBox 控件的 SelectedItems 属性(SelectedItem 的复数形式(。

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

并确保您已启动选定项。

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

RadAutoCompleteBoxSearchText属性应提供值。

根据文档,它获取或设置进入RadAutoCompleteBox的TextBox部分的字符串。SearchText 值用于筛选 RadAutoCompleteBox 的 ItemsSource。

如果要获取自动完成框的选定项的"文本",则需要将其转换为指定的类型。在您的情况下,它是类型 ServerCrafterTelerikWPF.Command .

var selectedItem = autoCompleteBox.SelectedItem;
if (selectedItem is ServerCrafterTelerikWPF.Command) {
  var selectedCommand = selectedItem as ServerCrafterTelerikWPF.Command;
  string textOfAutoCompleteBox = selectedCommand.ACommand;
}

最新更新