Reputation: 5401
I managed to create a Command
, like so:
Code-behind
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;
}
XAML
<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>
When I clicked the button, CommandParameter
always return null, even when I put something in the text box, and I'm sure that the command is working because Custom Command Executed
shows.
What I want to achieve here is to get the value of the TextBox
that has the same Tag
value as the Button
's Tag
(same barcode), because there will be multiple instances of both the TextBox
and the Button
, and the Tag
is the only one that can pair them.
Upvotes: 1
Views: 67
Reputation: 2363
As we spoke in the comments, you should be looking for ExecutedRoutedEventArgs.Parameter
instead of checking the sender
in your event handler.
Upvotes: 1