2016-10-11 4 views
0

Zum Beispiel habe ich ListView, Element chang FontWeight wenn Doppelklick-Ereignis ausgelöst wurde. Wie speichern Sie den Status zum nächsten Start des Programms? Datenbank verwenden? Was es besser ist, in der Datenbank String-Variable wie Bold, ExtraBold, Normal zu speichern. Oder Werte?WPF ListView Store Element Zustand

Was gemeinsamer Weg Zustand zu speichern ist

<Window.Resources> 
    <Style x:Key="itemstyle" TargetType="{x:Type ListViewItem}"> 
     <EventSetter Event="MouseDoubleClick" Handler="HandleDoubleClick" /> 
    </Style> 
</Window.Resources> 
<Grid Background="#FFF0F0F0"> 
    <ListView ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedEmployee}" Margin="0,0,0,62" ItemContainerStyle="{StaticResource itemstyle}"> 
     <ListView.View> 
      <GridView> 
       <GridViewColumn Header="FirstName"> 
        <GridViewColumn.CellTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock Text="{Binding FirstName}" FontWeight="{Binding FontWeight}" /> 
          </StackPanel> 
         </DataTemplate> 
        </GridViewColumn.CellTemplate> 
       </GridViewColumn> 
       <GridViewColumn Header="SecondName"> 
        <GridViewColumn.CellTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock Text="{Binding FirstName}" FontWeight="{Binding FontWeight}" /> 
          </StackPanel> 
         </DataTemplate> 
        </GridViewColumn.CellTemplate> 
       </GridViewColumn> 
      </GridView> 
     </ListView.View> 
    </ListView> 
    <CheckBox Content="HideAllNormal" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" HorizontalAlignment="Left" Margin="69,290,0,0" VerticalAlignment="Top"/> 
</Grid> 

Code:

namespace WpfApplication27 
{ 
    public partial class MainWindow : Window 
    { 
     ListViewModel LVM = new ListViewModel(); 
     public MainWindow() 
     { 
      InitializeComponent(); 
      DataContext = LVM; 
     } 

     protected void HandleDoubleClick(object sender, MouseButtonEventArgs e) 
     { 
      LVM.SelectedEmployee.FontWeight = FontWeights.Normal; 
     } 

     private void CheckBox_Checked(object sender, RoutedEventArgs e) 
     { 
      LVM.HideAllBold(); 
     } 

     private void CheckBox_Unchecked(object sender, RoutedEventArgs e) 
     { 
      LVM.ShowAll(); 
     } 
    } 

    public class ListViewModel : INotifyPropertyChanged 
    { 
     private ObservableCollection<Employee> items; 

     public ObservableCollection<Employee> Items 
     { 
      get 
      { 
       return items; 
      } 

      set 
      { 
       items = value; 
       RaisePropertyChaged("Items"); 
      } 
     } 

     private ObservableCollection<Employee> WithBoldItems; 

     public Employee SelectedEmployee { get; set; } 

     public ListViewModel() 
     { 
      Items = new ObservableCollection<Employee>(); 
      for (int i = 0; i < 10; i++) 
      { 
       Employee emp = new Employee(); 
       emp.Number = i; 
       emp.FirstName = "Test"; 
       emp.FontWeight = (FontWeight)new FontWeightConverter().ConvertFromString("Bold"); 
       Items.Add(emp); 
      } 
     } 

     public void HideAllBold() 
     { 
      WithBoldItems = Items; 
      Items = new ObservableCollection<Employee>(Items.Where(key => key.FontWeight != FontWeights.Bold)); 
     } 

     public void ShowAll() 
     { 
      Items = WithBoldItems; 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void RaisePropertyChaged(string info) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(info)); 
      } 
     } 

    } 

    public class Employee : INotifyPropertyChanged 
    { 
     public int Number { get; set; } 

     public string FirstName { get; set; } 

     private FontWeight fontWeight = FontWeights.Normal; 

     public FontWeight FontWeight 
     { 
      get 
      { 
       return fontWeight; 
      } 

      set 
      { 
       fontWeight = value; 
       RaisePropertyChaged("FontWeight"); 
      } 
     } 

     private void RaisePropertyChaged(string info) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(info)); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
    } 
} 

Antwort

0

ich den Zustand in der Haupt-Anwendung gespeichert habe diese Hoffnung überprüfen, es hilft.

 /// <summary> 
     /// Initializes the singleton application object. This is the first line of authored code 
     /// executed, and as such is the logical equivalent of main() or WinMain(). 
     /// </summary> 
     public App() 
     { 
      Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
      Microsoft.ApplicationInsights.WindowsCollectors.Metadata | 
      Microsoft.ApplicationInsights.WindowsCollectors.Session); 

      this.InitializeComponent(); 
      this.Suspending += OnSuspending; 
     } 

    /// <summary> 
    /// Invoked when the application is launched normally by the end user. Other entry points 
    /// will be used such as when the application is launched to open a specific file. 
    /// </summary> 
    /// <param name="e">Details about the launch request and process.</param> 
    protected async override void OnLaunched(LaunchActivatedEventArgs e) 
    { 
     #if DEBUG 
        Debug.WriteLine($"Installed location: {Windows.ApplicationModel.Package.Current.InstalledLocation.Path}"); 

        if (System.Diagnostics.Debugger.IsAttached) 
        { 
         this.DebugSettings.EnableFrameRateCounter = false; 
        } 

    #endif 




     RootFrame = Window.Current.Content as Frame; 

     // Do not repeat app initialization when the Window already has content, 
     // just ensure that the window is active 
     if (RootFrame == null) 
     { 
      // Create a Frame to act as the navigation context and navigate to the first page 
      RootFrame = new Frame(); 

      RootFrame.NavigationFailed += OnNavigationFailed; 

      Debug.WriteLine(e.PreviousExecutionState); 


      if (e.PreviousExecutionState == ApplicationExecutionState.Terminated || e.PreviousExecutionState == ApplicationExecutionState.ClosedByUser) 
      { 

       await xxxxx.Common.SuspensionManager.RestoreAsync(); 



      } 


      if (true) 
      { 
       try 
       { 
         await xxxx(await Utils.DeserializeXML<xxx>(
         await ApplicationData.Current.LocalFolder.GetFileAsync("xxx.xml"))); 
       } 
       catch 
       { 
        Debug.WriteLine("No in xxx"); 
       } 
      } 



      // Place the frame in the current Window 
      Window.Current.Content = RootFrame; 
      await AssetManager.InitAssets(); 
      // Place the frame in the current Window 


      await AssetManager.FetchAssets(); 

     } 

     if (RootFrame.Content == null) 
     { 
      // When the navigation stack isn't restored navigate to the first page, 
      // configuring the new page by passing required information as a navigation 
      // parameter 

      if (App.xxxx == null || App.xxxx.SessionId == null) 
       RootFrame.Navigate(typeof(xxxPage), e.Arguments); 
      else 
       RootFrame.Navigate(typeof(MainPage), e.Arguments); 
     } 

     // Ensure the current window is active 
     Window.Current.Activate(); 





    } 


    void OnNavigationFailed(object sender, NavigationFailedEventArgs e) 
    { 
     throw new Exception("xxxxxx " + e.SourcePageType.FullName); 
    } 


    private async void OnSuspending(object sender, SuspendingEventArgs e) 
    { 
     // it would be best to have a SUSPEND state in the statechart for every possible 
     // state, but it's ok for now 
     Utils.ReleaseDisplay(); 

     var deferral = e.SuspendingOperation.GetDeferral(); 

     try { 
      if (App.LoggedUser != null) 
      { 
       await Utils.SerializeXML(await ApplicationData.Current.LocalFolder.CreateFileAsync(
       "xxx.xml", CreationCollisionOption.ReplaceExisting), xxxxx); 
       await Utils.SerializeXML(await App.xxxFolder.CreateFileAsync(
       "xxx.xml", CreationCollisionOption.ReplaceExisting), App.State); 
      } 
     } 
     catch (Exception e2) 
     { 
      Debug.WriteLine(e2); 
     } 

     await xxxxx.Common.SuspensionManager.SaveAsync(); 
     deferral.Complete(); 
    } 

}