我怎样才能使用的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...
// ...
// ...
};
}
}
你应该把它从SelectedItem
属性。 它投射到你的类,然后得到它MyClass.ACommand
我建议结合SelectedItem
与Mode=TwoWay
您的视图模型可以有很大的帮助。
只需添加一个成员到视图模型,其正在实施类似命令:
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");
}
}
使用类型为Command类和您的问题将得到解决。
private Command selectedCommand;
public Command SelectedCommand
{
get
{
return selectedCommand;
}
set
{
selectedCommand = value;
NotifyPropertyChanged("SelectedCommand");
}
}
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}"/>
如果你想获得的代码隐藏所选择的项目,所选项目转换为命令类类型。
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>();
该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;
}