简体   繁体   中英

Cannot save TimeSpan in user settings wpf

I have time to respawn which is saved as timespan in file mysettings.settings. I am trying to save new timespan in user.config by changing text in textbox. The code:

    public NewWindow()
    {
        InitializeComponent();

        tbInt.Text = Properties.MySettings.Default.SomeInt.ToString();
        tbRespawnTime.Text = Properties.MySettings.Default.TimeToRespawn.TotalSeconds.ToString();
    }


    private void tbRespawnTime_TextChanged(object sender, TextChangedEventArgs e)
    {
         Properties.MySettings.Default.TimeToRespawn = TimeSpan.FromSeconds(Convert.ToDouble(tbRespawnTime.Text));

    }

    private void btSave_Click(object sender, RoutedEventArgs e)
    {
        Properties.MySettings.Default.Save();
    }

The problem is that new timespan is saving in user.config, but when i'm trying to show it in textbox in new instance of my application it's again default value from mysettings.settings

What am I doing wrong?

prefix "tb" means textbox "bt" means button

Bind directly in xaml like this,

Text="{Binding Source={x:Static prop:Settings.Default}, Path=TimeToRespawn}"

xmlns:prop="clr-namespace:yourapplication.Properties"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM