亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 TextBox 綁定到屬性后忽略格式字符串

在 TextBox 綁定到屬性后忽略格式字符串

C#
米琪卡哇伊 2022-12-24 12:51:01
在動態構建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



查看完整回答
反對 回復 2022-12-24
  • 1 回答
  • 0 關注
  • 117 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號