【发布时间】:2013-06-12 10:17:20
【问题描述】:
我有一个如下所示的属性:
private int _wertungvalue;
public int WertungValue
{
get { return _wertungvalue; }
set
{
_wertungvalue = value;
RaisePropertyChanged(() => WertungValue);
}
}
它绑定到一个文本框
<TextBox Text="{Binding WertungValue, Mode=TwoWay}"/>
所以用户可以输入他想要的任何内容(我不想要数字文本框!)。用户输入'5',WertungValue的值为5。如果用户输入'Test',会弹出红色边框,WertungValue的值仍然是5!
现在我也有一个 RelayCommand
RelayCommand(DeleteExecute,CanDelete);
在 CanDelete 中,我检查属性是否为 int
private bool CanDelete()
{
int ot = 0;
if (int.TryParse(WertungValue.ToString(),out ot) == false)
return false;
else
return true;
}
以便 RelayCommand 仅在值为整数时才有效。所以这意味着当用户输入“Test”时,RelayCommand 应该返回 false。问题是它永远不会返回 false,因为属性的值总是一个整数,但在 View 中它是一个字符串。
我不想让属性输入字符串或在我的 TextBox 中使用 UpdateSourceTrigger=PropertyChanged。我也不想只制作一个数字文本框...应该允许用户输入他想要的任何内容,但 RelayCommand 应该只在他输入整数时才起作用。
【问题讨论】:
-
你不能为所欲为。您将收到绑定错误,并且您的“设置”将永远不会在您的财产上被调用。将
UpdateSourceTrigger设置为“PropertyChanged”并不能解决问题。您不需要将单个属性设为字符串......但您应该添加第二个属性,将您的文本框绑定到。
标签: c# mvvm int relaycommand