【发布时间】:2010-11-04 06:01:41
【问题描述】:
我正在使用 MVVM 模式,并且我在父窗口中有一个文本框,并希望将一些文本发送到将出现在 Textchanged 上的弹出窗口。
我尝试使用命令参数,但它不适合我。
请帮忙..
谢谢 沙拉特
【问题讨论】:
我正在使用 MVVM 模式,并且我在父窗口中有一个文本框,并希望将一些文本发送到将出现在 Textchanged 上的弹出窗口。
我尝试使用命令参数,但它不适合我。
请帮忙..
谢谢 沙拉特
【问题讨论】:
你试过什么?这段代码对我有用:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.CommandBindings>
<CommandBinding Command="Cut" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<StackPanel>
<TextBox x:Name="textBox1" />
<Button Command="Cut"
CommandParameter="{Binding Text,ElementName=textBox1}"
Content="Cut" />
</StackPanel>
</Window>
使用此事件处理程序:
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show(e.Parameter.ToString());
}
【讨论】:
如果我希望在用户按下回车时执行命令,我喜欢使用它。注意 IsDefault 绑定的巧妙使用 :-)
<TextBox x:Name="inputBox"/>
<Button Command="{Binding CutCommand}"
CommandParameter="{Binding Text, ElementName=inputBox}"
Content="Cut"
IsDefault="{Binding IsFocused, ElementName=inputBox}" />
如果您不希望按钮可见,当然可以将其可见性设置为折叠状态。我认为如果你按回车它仍然会执行命令。
【讨论】:
此代码适用于我
<UserControl x:Class="Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Height="Auto" Width="Auto">
<UserControl.InputBindings>
<KeyBinding Key="Enter" Command="{Binding ScanCommand}" CommandParameter="{Binding Text, ElementName=tbBarcode}"/>
</UserControl.InputBindings>
<Grid Name="LayoutRoot">
<TextBox x:Name="tbBarcode" Height="23"/>
</Grid>
</UserControl>
【讨论】: