wpf 获取 RadAutoCompleteBox 的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15894646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get text of RadAutoCompleteBox
提问by davidweitzenfeld
How can I get the text of a RadAutoCompleteBox using RadControls Q1 2013 in C#?
如何在 C# 中使用 RadControls Q1 2013 获取 RadAutoCompleteBox 的文本?
autoCompleteBox.SelectedItemreturns "ServerCrafterTelerikWPF.Command".
autoCompleteBox.SelectedItem返回"ServerCrafterTelerikWPF.Command"。
Edit 1:Here's my XAML:
编辑 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"/>
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.
而且我没有任何 C# 代码。我只想在按下按钮时获取 RadAutoCompleteBox 中的文本。
Edit 2:And here's my collection:
编辑 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...
// ...
// ...
};
}
}
采纳答案by Xelom
You should take it from the SelectedItemproperty. Cast it to your class and then get it from MyClass.ACommand
你应该把它从SelectedItem财产上拿走。将其投射到您的班级,然后从MyClass.ACommand
And I suggest binding SelectedItemwith Mode=TwoWayin your ViewModel can help a lot.
我建议结合SelectedItem与Mode=TwoWay您的视图模型可以有很大的帮助。
Just add a Member to ViewModel which is implementing Command like:
只需向 ViewModel 添加一个成员,该成员正在实现如下命令:
private Command _SelectedItem;
public Command SelectedItem
{
//get set with INotifyPropertyChanged
}
Then from the xaml: Bind RadAutoCompleteBox's SelectedItem Property like:
然后从 xaml:绑定 RadAutoCompleteBox 的 SelectedItem 属性,如:
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
回答by Haritha
I have reproduced the problem.
我已经重现了这个问题。
Yes. I had the same problem. And I foundthe problem and the answer too.
是的。我有same problem。我found也是问题和答案。
I got the problem because of using of type stringfor the selected item in my view model.
我遇到了问题,因为在我的视图模型中为所选项目使用了类型字符串。
private string selectedCommand;
public string SelectedCommand
{
get
{
return selectedCommand;
}
set
{
selectedCommand = value;
NotifyPropertyChanged("SelectedCommand");
}
}
Use the type as Command classand your problem will be solved.
使用类型作为 Command 类,您的问题将得到解决。
private Command selectedCommand;
public Command SelectedCommand
{
get
{
return selectedCommand;
}
set
{
selectedCommand = value;
NotifyPropertyChanged("SelectedCommand");
}
}
Bindthe SelectedItemproperty of the RadAutoCompleteBoxin the XAML
Bind该SelectedItem财产RadAutoCompleteBox在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"
SelectedItem="{Binding SelectedCommand, Mode=TwoWay}"/>
If you wanna get the selected item by the code-behind, convert the selected item to the Command class type.
如果您想通过代码隐藏获取所选项目,请将所选项目转换为 Command 类类型。
var selectedItem = autoCompleteBox.SelectedItem as Command;
And actually there can be multiple selected items. In that case you have to define a collection of Command objects.
实际上可以有multiple selected items。在这种情况下,您必须定义一个collection of Command objects.
private ObservableCollection<Command> selectedCommands;
public ObservableCollection<Command> SelectedCommands
{
get
{
return selectedCommands;
}
set
{
selectedCommands = value;
NotifyPropertyChanged("SelectedCommands");
}
}
And bind it to the SelectedItemsproperty (plural of SelectedItem) of the RadAutoCompleteBox control.
并将其绑定到SelectedItemsRadAutoCompleteBox 控件的属性(SelectedItem 的复数)。
SelectedItems="{Binding SelectedCommands, Mode=TwoWay}"
And make sure you have initiated the SelectedItems.
并确保您已启动 SelectedItems。
this.SelectedCommands = new ObservableCollection<Command>();
回答by Jehof
The SearchTextproperty of the RadAutoCompleteBoxshould provide you the value.
的SearchText属性RadAutoCompleteBox应该为您提供价值。
According to the documentationit gets or sets the string that is into the TextBox part of the RadAutoCompleteBox. The SearchText value is used to filter the RadAutoCompleteBox' ItemsSource.
根据文档,它获取或设置 RadAutoCompleteBox 的 TextBox 部分中的字符串。SearchText 值用于过滤 RadAutoCompleteBox 的 ItemsSource。
If you want to get the "Text" of the selected item of the AutocompleteBox, then you need to cast it to the specified type. In your case it is of type ServerCrafterTelerikWPF.Command.
如果要获取AutocompleteBox的选中项的“Text”,则需要将其强制转换为指定类型。在您的情况下,它的类型为ServerCrafterTelerikWPF.Command。
var selectedItem = autoCompleteBox.SelectedItem;
if (selectedItem is ServerCrafterTelerikWPF.Command) {
var selectedCommand = selectedItem as ServerCrafterTelerikWPF.Command;
string textOfAutoCompleteBox = selectedCommand.ACommand;
}

