在动态构建UserControl中,我format string以TextBox这种方式设置了:TextBox newTextBox = new TextBox();TempViewModel viewModel = new TempViewModel();var binding = new System.Windows.Data.Binding{ Source = viewModel, Path = new PropertyPath("DecimalValue"), ConverterCulture = new System.Globalization.CultureInfo("de-DE"), StringFormat = "{0:#,##0.00}"};newTextBox.SetBinding(TextBox.TextProperty, binding);public class TempViewModel{ public decimal DecimalValue { get; set; }}到目前为止效果很好。但在添加DependencyProperty后,格式将被忽略。Dependencyproperty是这样定义的:public class UserControl_CBOGridQuotePositions : UserControl{ private static readonly DependencyProperty Amount_QuotePos_Base_DependencyProperty = DependencyProperty.Register("Amount_QuotePos_Base", typeof(System.Decimal), typeof(UserControl_CBOGridQuotePositions), new PropertyMetadata(0m)); public System.Decimal Amount_QuotePos_Base { get { return (System.Decimal)GetValue(UserControl_CBOGridQuotePositions.Amount_QuotePos_Base_DependencyProperty); } set { SetValue(UserControl_CBOGridQuotePositions.Amount_QuotePos_Base_DependencyProperty, value); } } private void MakeTheBindings(DependencyProperty dependencyProperty) { var binding = new Binding("Amount_QuotePos_Base"); binding.Source = this; // which is the UserControl_CBOGridQuotePositions newTextBox.SetBinding(dependencyProperty, binding); }}有没有办法在 TextBox 绑定到属性时使格式工作?
1 回答
心有法竹
TA贡献1866条经验 获得超5个赞
因为在 MakeTheBindings() 中,您要用新的 Binding 替换 Binding 。确保在执行此操作时 var binding = new Binding("Amount_QuotePos_Base"); 您还设置了所有属性,例如 ConverterCulture 和 StringFormat
- 1 回答
- 0 关注
- 101 浏览
添加回答
举报
0/150
提交
取消