仅通过按钮的文本内容
Text Content only via button
我正在尝试获取以下内容:在我的页面中,我有一个文本框和按钮。当用户在键盘上按下 "Enter" 时,它应该与单击按钮相同。
我的代码大致如下:
<Page x:Class="MyApp.Pages.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
.....
DataContext="{Binding Page1VM, Source={StaticResource Locator}}">
<Page.InputBindings>
<KeyBinding
Key="Enter"
Command="{Binding Btn_ConfirmCommand}" />
</Page.InputBindings>
<Grid>
<Grid >
<TextBox Text="{Binding SelectedID}" />
<Button Command="{Binding Btn_ConfirmCommand}"/>
</Grid>
</Grid>
内部视图模型:
public Page1VM()
{
Btn_ConfirmCommand = new RelayCommand(Btn_ConfirmMethod);
}
...
void Btn_ConfirmMethod()
{
MessageBox.Show(SelectedID);
}
public string SelectedID
{
get{return selectedID;}
set
{
Set(() => SelectedID, ref selectedID, value);
RaisePropertyChanged("SelectedID");
}
}
The problem: When I write some content inside the textbox and click the button the messagebox prints the content, but if I press enter key is prints an empty string
将Binding
的UpdateSourceTrigger
设为PropertyChanged
:
<TextBox Text="{Binding SelectedID, UpdateSourceTrigger=PropertyChanged}" />
这应该会导致来源 属性 在每次击键时得到更新。默认值为 LostFocus
.
我正在尝试获取以下内容:在我的页面中,我有一个文本框和按钮。当用户在键盘上按下 "Enter" 时,它应该与单击按钮相同。
我的代码大致如下:
<Page x:Class="MyApp.Pages.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
.....
DataContext="{Binding Page1VM, Source={StaticResource Locator}}">
<Page.InputBindings>
<KeyBinding
Key="Enter"
Command="{Binding Btn_ConfirmCommand}" />
</Page.InputBindings>
<Grid>
<Grid >
<TextBox Text="{Binding SelectedID}" />
<Button Command="{Binding Btn_ConfirmCommand}"/>
</Grid>
</Grid>
内部视图模型:
public Page1VM()
{
Btn_ConfirmCommand = new RelayCommand(Btn_ConfirmMethod);
}
...
void Btn_ConfirmMethod()
{
MessageBox.Show(SelectedID);
}
public string SelectedID
{
get{return selectedID;}
set
{
Set(() => SelectedID, ref selectedID, value);
RaisePropertyChanged("SelectedID");
}
}
The problem: When I write some content inside the textbox and click the button the messagebox prints the content, but if I press enter key is prints an empty string
将Binding
的UpdateSourceTrigger
设为PropertyChanged
:
<TextBox Text="{Binding SelectedID, UpdateSourceTrigger=PropertyChanged}" />
这应该会导致来源 属性 在每次击键时得到更新。默认值为 LostFocus
.