Created
September 21, 2017 18:28
-
-
Save name2name2/86bf81be8e291a1a0e951f1b84e65b44 to your computer and use it in GitHub Desktop.
20170922
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[xaml.cs] | |
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
DataContext = this; | |
#region emp init | |
//初始化這邊可以加到UI上 | |
Employees = new ObservableCollection<Employee> | |
{ | |
new Employee { FirstName = "FirstName1", LastName = "LastName1", HireDate = new DateTime(1970, 1, 1) }, | |
new Employee { FirstName = "FirstName2", LastName = "LastName2", HireDate = new DateTime(1970, 1, 1) }, | |
new Employee { FirstName = "FirstName3", LastName = "LastName3", HireDate = new DateTime(1980, 1, 1) }, | |
new Employee { FirstName = "FirstName4", LastName = "LastName4", HireDate = new DateTime(1980, 1, 1) }, | |
new Employee { FirstName = "FirstName5", LastName = "LastName5", HireDate = new DateTime(1980, 1, 1) }, | |
new Employee { FirstName = "FirstName6", LastName = "LastName6", HireDate = new DateTime(1990, 1, 1) }, | |
new Employee { FirstName = "FirstName7", LastName = "LastName7", HireDate = new DateTime(1990, 1, 1) }, | |
new Employee { FirstName = "FirstName8", LastName = "LastName8", HireDate = new DateTime(1990, 1, 1) }, | |
new Employee { FirstName = "FirstName9", LastName = "LastName9", HireDate = new DateTime(1990, 1, 1) }, | |
}; | |
#endregion | |
/// 把Resource改到這裡 | |
EmployeesSource = | |
CollectionViewSource.GetDefaultView(Employees) ; | |
EmployeesSource.GroupDescriptions.Add(new PropertyGroupDescription("HireDate")); | |
} | |
public ObservableCollection<Employee> Employees { get; set; } | |
/// <summary> | |
/// 把Resource改到這裡 | |
/// </summary> | |
private ICollectionView EmployeesSource | |
{ | |
get { return _employeesSource; } | |
set { _employeesSource = value; } | |
} | |
private ICollectionView _employeesSource; | |
private void addItem_Click(object sender, RoutedEventArgs e) | |
{ | |
//有增加進去, 但是UI不會新增這一筆1983的資料 | |
Employees.Add(new Employee | |
{ | |
FirstName = "FirstName10", | |
LastName = "LastName10", | |
HireDate = new DateTime(1983, 1, 1) | |
}); | |
Employees.Add(new Employee | |
{ | |
FirstName = "FirstName10", | |
LastName = "LastName10", | |
HireDate = new DateTime(1990, 1, 1) | |
}); | |
} | |
} | |
[ cs 測試留Employee即可 ] | |
public class Employee : INotifyPropertyChanged | |
{ | |
private string _firstName; | |
public string FirstName | |
{ | |
get { return _firstName; } | |
set | |
{ | |
_firstName = value; | |
NotifyPropertyChanged("FirstName"); | |
} | |
} | |
private string _lastName; | |
public string LastName | |
{ | |
get { return _lastName; } | |
set | |
{ | |
_lastName = value; | |
NotifyPropertyChanged("LastName"); | |
} | |
} | |
private DateTime _hireDate; | |
public DateTime HireDate | |
{ | |
get { return _hireDate; } | |
set | |
{ | |
_hireDate = value; | |
NotifyPropertyChanged("HireDate"); | |
} | |
} | |
public event PropertyChangedEventHandler PropertyChanged; | |
protected void NotifyPropertyChanged(string sProp) | |
{ | |
if (PropertyChanged != null) | |
{ | |
PropertyChanged(this, new PropertyChangedEventArgs(sProp)); | |
} | |
} | |
} | |
[xaml resource不須宣告collection resource 改在xaml.cs完成] | |
<Window.Resources> | |
<DataTemplate x:Key="EmployeeTemplate"> | |
<StackPanel Orientation="Horizontal"> | |
<TextBlock Text="{Binding FirstName}" /> | |
<TextBlock Text="{Binding LastName}" /> | |
</StackPanel> | |
</DataTemplate> | |
</Window.Resources> | |
<Grid> | |
<Grid.RowDefinitions> | |
<RowDefinition Height="50"/> | |
<RowDefinition Height="*"/> | |
</Grid.RowDefinitions> | |
<StackPanel Grid.Row="0"> | |
<Button Name="addItem" Width="50" Height="50" Content="AddItem" HorizontalAlignment="Left" Click="addItem_Click"/> | |
</StackPanel> | |
<ListBox Grid.Row="1" ItemsSource="{Binding Employees}" | |
ItemTemplate="{StaticResource EmployeeTemplate}" | |
SelectionMode="Extended"> | |
<ListBox.GroupStyle> | |
<GroupStyle/> | |
</ListBox.GroupStyle> | |
</ListBox> | |
</Grid> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment