【发布时间】:2017-08-07 09:22:58
【问题描述】:
XAML
<UserControl.CommandBindings>
<CommandBinding Command="{x:Static local:ReturnsUserControl.GetValueCommand}"
Executed="ExecutedGetValueCommand"
CanExecute="CanExecuteGetValueCommand" />
</UserControl.CommandBindings>
<TextBox x:Name="txtExchangeQuantity" />
<Button Content="Add"
Tag="{Binding ProductBarcode}"
Command="{x:Static local:ReturnsUserControl.GetValueCommand}"
CommandParameter="{Binding Text, ElementName=txtExchangeQuantity}"/>
代码隐藏
public static RoutedCommand GetValueCommand = new RoutedCommand();
private void ExecutedGetValueCommand(object sender, ExecutedRoutedEventArgs e)
{
Button b = (sender) as Button;
MessageBox.Show(b.Tag.ToString());
}
private void CanExecuteGetValueCommand(object sender, CanExecuteRoutedEventArgs e)
e.CanExecute = true;
}
当我单击该按钮时,我收到一个NullException,因为显然Tag 的值为空,但我确信Tag 有一个值。那么如何使用Command 获取Tag 的值?
这是我用来检查Tag是否有东西的:
private void SampleClick(object sender, RoutedEventArgs e)
{
Button btnSelect = sender as Button;
string barcode = btnSelect.Tag.ToString();
MessageBox.Show(barcode);
}
【问题讨论】:
标签: c# wpf data-binding command