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;
}
}