有時,我們在WPF中,需要將用戶輸入的四個數字字符串值(比如:"10,10,300,300")轉為Rect,比如用來畫矩形,或者是設置成畫刷的VIEwPort屬性等。怎麼做呢?
答案是:使用RectConverter,比如:
RectConverter rectConverter = new RectConverter();
string parseString = vIEwportEntry.Text;
if (parseString != null && parseString != string.Empty)
{
imageBrush.VIEwport = (Rect)rectConverter.ConvertFromString(parseString);
}
else
{
imageBrush.VIEwport = Rect.Empty;
vIEwportEntry.Text = "Empty";
}