Tag: WPF/設定を保存する
名前 | 型 | スコープ |
Message | string | ユーザー |
<Window x:Class="SettingsDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="200" Width="200"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <TextBox Margin="10" Text="{Binding Message}"></TextBox> <Button Grid.Row="1" Click="button_Clicked">保存</Button> </Grid> </Window>
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); DataContext = new MainWindowViewModel(); } private void button_Clicked(object sender, RoutedEventArgs e) { Properties.Settings.Default.Save(); } }
public class MainWindowViewModel :INotifyPropertyChanged { public string Message { get { return Properties.Settings.Default.Message; } set { Properties.Settings.Default.Message = value; NotifyPropertyChanged("Message"); } } #region INotifyPropertyChanged メンバー public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #endregion