Is there a non-klunky way to perform special-case formatting with bound data?

淺唱寂寞╮ 提交于 2019-12-11 06:51:13

问题


I am binding many controls on my form to an object returned from a method call like so:

textBoxMoonBeam.DataBindings.Add(new Binding("Text", pi, "MoonBeam"));

Note: "pi" is the name of an instantiation of the PlatypusInfo class.

...but when I am grabbing a dateTime value, which by definition includes the time appended to the date, but I only want to show the date in the control, I've got to eschew the type of binding above and instead do this:

textBoxDateAztecsFirstSawElvis.Text = pi.DateAztecsFirstSawElvis.ToString("d");

Is there a way to bind my data like the first example, and still truncate the date?


回答1:


Use the binding source's Format event.

Something like this:

Binding binding = new Binding("Text", pi, "DateAztecsFirstSawElvis", true);
binding.Format += binding_Format;
textBoxDateAztecsFirstSawElvis.DataBindings.Add(binding);

void binding_Format(object sender, ConvertEventArgs e) {
  e.Value = ((DateTime)e.Value).ToShortDateString();
}


来源:https://stackoverflow.com/questions/10639477/is-there-a-non-klunky-way-to-perform-special-case-formatting-with-bound-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!