如果您可以修改 NumberPress 引用的命令,那么最简单的解决方案是在那里解析命令参数以获取数字。如果这不是一个选项,那么另一个解决方案是创建一个 IValueConverter 类并将其添加到 CommandParameter 绑定。
<Button Content="_9"
Focusable="False"
Command="{Binding NumberPress}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self},
Path=Content, Converter={StaticResource NumberConverter}}"
Margin="92,134,92,129" />
执行:
public class NumberConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is string)
{
string strVal = ((string)value).TrimStart('_');
int intVal;
if (int.TryParse(strVal, out intVal))
return intVal;
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
}