c# - 获取 RadAutoCompleteBox 的文本

标签 c# wpf xaml telerik rad-controls

如何在 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: 这是我的收藏:

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 属性中获取它。将它转换到您的类(class),然后从 MyClass.ACommand

获取它

我建议在您的 ViewModel 中将 SelectedItemMode=TwoWay 绑定(bind)会有很大帮助。

只需将一个成员添加到 ViewModel,它正在执行如下命令:

private Command _SelectedItem;

public Command SelectedItem 
{ 
   //get set with INotifyPropertyChanged 
}

然后从 xaml 中:像这样绑定(bind) RadAutoCompleteBox 的 SelectedItem 属性:

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

关于c# - 获取 RadAutoCompleteBox 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15894646/

相关文章:

c# - 发布到 MVC6 Controller 的值为空

c# - 使用 WPF KeyDown 事件了解多个键

c# - 如何在 DataTrigger 中匹配 "newline"?

c# - 无法在 WPF 应用程序中创建自定义命令

c# - 从 UWP XAML 中的 HyperlinkBut​​ton 中删除下划线

c# - 为什么不能同时使用 2 个 NetworkManager?

c# - Math ML 的 .NET 组件,可以编辑公式,将它们呈现为图像

c# - 开始使用 WPF 而不是 WinForms 的最佳读物是什么?

c# - IValueConverter 和模拟数据

c# - 当控件变得不可见时,如何将焦点移至下一个同级而不是父级?