wpf 如何以编程方式设置文本框与字符串格式的绑定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16557887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:47:54  来源:igfitidea点击:

How to programatically set the binding of a textbox with stringformat?

c#wpftextbox

提问by JSchwartz

How do I programmatically do the following (from the XAML):

我如何以编程方式执行以下操作(来自 XAML):

<TextBox Name="OrderDateText"
         Text="{Binding Path=OrderDate, StringFormat=dd-MM-yyyy}"

public DateTime OrderDate

Right now I have the following

现在我有以下内容

TextBox txtboxOrderdDate = new TextBox();

And I know I need to do something like

我知道我需要做类似的事情

  Binding bindingOrderDate = new Binding();
  bindingOrderDate.Source = "OrderDate";

But I am stuck here ... not sure how to proceed to apply the StringFormat nor am I sure that SOURCE is the correct way (should I be using ElementName?)

但我被困在这里......不确定如何继续应用 StringFormat 也不确定 SOURCE 是正确的方法(我应该使用 ElementName 吗?)

回答by Ramesh Durai

Let MainWindowbe the Class Name. Change MainWindowin the below code to your class name.

MainWindow成为类名。MainWindow将下面的代码更改为您的班级名称。

public DateTime OrderDate
{
    get { return (DateTime) GetValue(OrderDateProperty); }
    set { SetValue(OrderDateProperty, value); }
}

public static readonly DependencyProperty OrderDateProperty =
    DependencyProperty.Register("OrderDate",
                                typeof (DateTime),  
                                typeof (MainWindow),
                                new PropertyMetadata(DateTime.Now, // Default value for the property
                                                     new PropertyChangedCallback(OnOrderDateChanged)));

private static void OnOrderDateChanged(object sender, DependencyPropertyChangedEventArgs args)
{
    MainWindow source = (MainWindow) sender;

    // Add Handling Code
    DateTime newValue = (DateTime) args.NewValue;
}

public MainWindow()
{
    InitializeComponent();

    OrderDateText.DataContext = this;
    var binding = new Binding("OrderDate")
        {
            StringFormat = "dd-MM-yyyy"
        };
    OrderDateText.SetBinding(TextBox.TextProperty, binding);

    //Testing
    OrderDate = DateTime.Now.AddDays(2);


}

回答by It'sNotALie.

Have you tried setting the bindingOrderDate's StringFormatproperty to the proper format? That's how it should work, according to MSDN.

您是否尝试将bindingOrderDateStringFormat属性设置为正确的格式?根据 MSDN,它应该是这样工作的。

回答by Haritha

Define a property of type DateTimein your code behind and then bind.

DateTime在后面的代码中定义一个 type 属性然后绑定。

Please refer this link.

请参考此链接

回答by Chamath Jeevan

?Object data = new Object();

 ? ? ? ? ? ?TextBox txtboxOrderdDate = new TextBox();
 ? ? ? ? ? ?Binding bindingOrderDate = new Binding("Order Date", data, "OrderDate");
 ? ? ? ? ? ?bindingOrderDate.Format += new ConvertEventHandler(DecimalToCurrencyString);
 ? ? ? ? ? ?txtboxOrderdDate.DataBindings.Add(bindingOrderDate);

? ?private void DecimalToCurrencyString(object sender, ConvertEventArgs cevent)
 ? ? ? ?{

 ? ? ? ? ? ?if (cevent.DesiredType != typeof(string)) return;

 ? ? ? ? ? ?cevent.Value = ((decimal)cevent.Value).ToString("dd-MM-yyyy");
 ? ? ? ?}


//[For more information check MSDN][1]