Wpf 文本框';使用CommandParameter时,s Text属性返回null

Wpf 文本框';使用CommandParameter时,s Text属性返回null,wpf,data-binding,Wpf,Data Binding,我设法创建了一个命令,如下所示: 代码隐藏 XAML 当我单击按钮时,CommandParameter始终返回null,即使我在文本框中输入了一些内容,我确信该命令正在工作,因为执行的自定义命令显示 我想在这里实现的是获得文本框的值,该值与按钮的标签(相同的条形码)具有相同的标记值,因为文本框和按钮都有多个实例,而标记是唯一可以将它们配对的标记。正如我们在评论中所说的,您应该查找ExecutedRoutedEventArgs.Parameter,而不是在事件处理程序中检查发送方。输出窗口中是

我设法创建了一个
命令
,如下所示:

代码隐藏

XAML


当我单击按钮时,
CommandParameter
始终返回null,即使我在文本框中输入了一些内容,我确信该命令正在工作,因为执行的
自定义命令显示


我想在这里实现的是获得
文本框
的值,该值与
按钮
标签
(相同的条形码)具有相同的
标记
值,因为
文本框
按钮
都有多个实例,而
标记是唯一可以将它们配对的标记。

正如我们在评论中所说的,您应该查找
ExecutedRoutedEventArgs.Parameter
,而不是在事件处理程序中检查
发送方。

输出窗口中是否有错误?类似错误:40或41?@XAMlMAX我不太确定,让我检查一下。@XAMlMAX我只能看到
系统。NullReferenceException
加上堆栈跟踪您是否尝试检查
ExecutedRoutedEventArgs
上的
参数
?我看到@XAMlMAX没有检查发送者
CommandParameter
,至少工作正常。如果您愿意,您可以添加它作为答案,以便我可以接受它。
public static RoutedCommand GetValueCommand = new RoutedCommand();
private void ExecutedGetValueCommand(object sender, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Custom Command Executed");
    Button b = (sender) as Button;
    MessageBox.Show(b.CommandParameter.ToString());
}
private void CanExecuteGetValueCommand(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
}
<UserControl.CommandBindings>
    <CommandBinding Command="{x:Static local:ReturnsUserControl.GetValueCommand}"
                    Executed="ExecutedGetValueCommand"
                    CanExecute="CanExecuteGetValueCommand" />
</UserControl.CommandBindings>

<ListView>
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal"
                                    VerticalAlignment="Center"
                                    HorizontalAlignment="Center">
                            <TextBlock Text="{Binding ProductDescription}"/>
                         </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal"
                                    VerticalAlignment="Center"
                                    HorizontalAlignment="Center">
                            <TextBox x:Name="txtExchangeQuantity"
                                     Tag="{Binding ProductBarcode}"/>
                            <Button Content="Add"
                                    Tag="{Binding ProductBarcode}"
                                    Command="{x:Static local:ReturnsUserControl.GetValueCommand}"
                                     CommandParameter="{Binding Text,ElementName=txtExchangeQuantity}"/>
                         </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>