【问题标题】:Get text of RadAutoCompleteBox获取 RadAutoCompleteBox 的文本
【发布时间】:2013-03-31 10:51:42
【问题描述】:

如何在 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...
                // ...
                // ...
            };
    }
}

【问题讨论】:

  • 请发布您的XAMLCode
  • @Haritha 编辑了问题。
  • 您尚未向我们展示如何获取 RadAutoCompleteBox 控件的选定项。请张贴代码。
  • 我想我got 你的问题。问题应该是你得到了选定的项目as a string。但它shouldCommand class 的类型。 (实际上应该是collection of Command 类的类型,因为RadAutoCompleteBox 可以有multiple selected items。)请看我的回答。

标签: c# wpf xaml telerik rad-controls


【解决方案1】:

RadAutoCompleteBoxSearchText 属性应该为您提供值。

根据documentation,它获取或设置进入RadAutoCompleteBox 的TextBox 部分的字符串。 SearchText 值用于过滤 RadAutoCompleteBox 的 ItemsSource。

如果要获取AutocompleteBox选中项的“Text”,则需要将其强制转换为指定类型。在您的情况下,它的类型为 ServerCrafterTelerikWPF.Command

var selectedItem = autoCompleteBox.SelectedItem;

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

  string textOfAutoCompleteBox = selectedCommand.ACommand;
}

【讨论】:

  • 这就是问题所在,没有Text属性。
  • SearchText 仅获取使用 search 的 ItemSource 字符串。如果已找到并选择一个项目,则该项目不再位于 SearchText 中。
  • 谢谢,很好很简单,但这仅在选择一项时有效。
【解决方案2】:

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

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

只需向 ViewModel 添加一个成员,该成员正在执行以下命令:

private Command _SelectedItem;

public Command SelectedItem 
{ 
   //get set with INotifyPropertyChanged 
}

然后从xaml中:绑定RadAutoCompleteBox的SelectedItem属性如:

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

【讨论】:

  • 感谢您的回答,但我是 C# 新手,不知道具体该怎么做。
  • 我已更改答案以更详细地解释。
  • 是的。这就是答案。 这里假设只有one selected item。但实际上 multiple items can be seleceted 在 RadAutoCompleteBox 控件中。所以不要使用Command 使用ObservableCollection&lt;Command&gt; 来绑定控件的SelectedItems(SelectedItem 的复数)属性。
【解决方案3】:

我已经重现了这个问题。

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

我遇到了问题因为在我的视图模型中对所选项目使用了字符串类型

private string selectedCommand;

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

使用type作为Command类,你的问题就迎刃而解了。

private Command selectedCommand;

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

XAMLRadAutoCompleteBoxSelectedItem属性Bind

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

并确保您已启动 SelectedItems。

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

【讨论】:

  • 非常感谢您的详细回答,但是当我绑定SelectedItems="{Binding SelectedCommands, Mode=TwoWay}" 时,我得到了System.NotSupportedException (Collection was of fixed size.) 类型的异常。
  • 我也收到错误Cannot resolve symbol 'NotifyPropertyChanged',所以我删除了这行,希望没有必要。
猜你喜欢
  • 1970-01-01
  • 2018-12-02
  • 2015-07-06
  • 1970-01-01
  • 2010-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多