public DataBrowserControl(IEntityController controller) { if (controller == null) { throw new ArgumentNullException("IEntityController<T> cannot be null"); } this.Controller = controller; //Fill the properties with "fresh" data Items = controller.GetDataSource(); //Listen when the data changes so that we can update the properties holding the data controller.Changed += Controller_Changed; InitializeComponent(); //Setting the background is required => when using DataBrowserControl as a popup //and it flows out of the window its background shouldn't be transparent dockPanel.Background = App.Current.MainWindow.Background; #region Mapping with column names foreach (KeyValuePair <string, string> item in controller.Mapping) { GridViewColumn column = new GridViewColumn { Header = item.Key, DisplayMemberBinding = new Binding(item.Value) { StringFormat = item.Value.Contains("PRICE") ? "C" : "", ConverterCulture = Thread.CurrentThread.CurrentCulture } }; gridView.Columns.Add(column); } #endregion //Create binding to the current controller Binding controllerBinding = new Binding() { Source = this, Path = new PropertyPath("Controller"), Mode = BindingMode.OneWay }; //Create binding to the selected item (reversed binding used for the sake of simplicity) Binding selecteditemBinding = new Binding() { Source = this.Controller, Path = new PropertyPath("SelectedItem"), Mode = BindingMode.OneWayToSource }; EntitiesListView.SetBinding(ListView.SelectedItemProperty, selecteditemBinding); #region Context menu Style style = new Style(typeof(ListViewItem)); ContextMenu contextMenu = new ContextMenu(); MenuItem newMenuItem = new MenuItem() { Command = InvoiceManagerCommands.New, Icon = new Image() { Source = new BitmapImage(new Uri("/Resources/new.png", UriKind.Relative)), Width = 20, Height = 20 } }; newMenuItem.SetBinding(MenuItem.CommandParameterProperty, controllerBinding); contextMenu.Items.Add(newMenuItem); MenuItem editMenuItem = new MenuItem() { Command = InvoiceManagerCommands.Edit, Icon = new Image() { Source = new BitmapImage(new Uri("/Resources/Edit.png", UriKind.Relative)), Width = 20, Height = 20 } }; editMenuItem.SetBinding(MenuItem.CommandParameterProperty, controllerBinding); contextMenu.Items.Add(editMenuItem); MenuItem deleteMenuItem = new MenuItem() { Command = InvoiceManagerCommands.Delete, Icon = new Image() { Source = new BitmapImage(new Uri("/Resources/Delete.png", UriKind.Relative)), Width = 20, Height = 20 } }; deleteMenuItem.SetBinding(MenuItem.CommandParameterProperty, controllerBinding); contextMenu.Items.Add(deleteMenuItem); style.Setters.Add(new Setter(ListViewItem.ContextMenuProperty, contextMenu)); Resources.Add(typeof(ListViewItem), style); #endregion }
public DataBrowserControl(IEntityController controller) { if (controller == null) throw new ArgumentNullException("IEntityController<T> cannot be null"); this.Controller = controller; //Fill the properties with "fresh" data Items = controller.GetDataSource(); //Listen when the data changes so that we can update the properties holding the data controller.Changed += Controller_Changed; InitializeComponent(); //Setting the background is required => when using DataBrowserControl as a popup //and it flows out of the window its background shouldn't be transparent dockPanel.Background = App.Current.MainWindow.Background; #region Mapping with column names foreach(KeyValuePair<string, string> item in controller.Mapping) { GridViewColumn column = new GridViewColumn { Header = item.Key, DisplayMemberBinding = new Binding(item.Value) { StringFormat = item.Value.Contains("PRICE") ? "C" : "", ConverterCulture = Thread.CurrentThread.CurrentCulture } }; gridView.Columns.Add(column); } #endregion //Create binding to the current controller Binding controllerBinding = new Binding() { Source = this, Path = new PropertyPath("Controller"), Mode = BindingMode.OneWay }; //Create binding to the selected item (reversed binding used for the sake of simplicity) Binding selecteditemBinding = new Binding() { Source = this.Controller, Path = new PropertyPath("SelectedItem"), Mode = BindingMode.OneWayToSource }; EntitiesListView.SetBinding(ListView.SelectedItemProperty, selecteditemBinding); #region Context menu Style style = new Style(typeof (ListViewItem)); ContextMenu contextMenu = new ContextMenu(); MenuItem newMenuItem = new MenuItem() { Command = InvoiceManagerCommands.New, Icon = new Image() { Source = new BitmapImage(new Uri("/Resources/new.png", UriKind.Relative)), Width = 20, Height = 20 } }; newMenuItem.SetBinding(MenuItem.CommandParameterProperty, controllerBinding); contextMenu.Items.Add(newMenuItem); MenuItem editMenuItem = new MenuItem() { Command = InvoiceManagerCommands.Edit, Icon = new Image() { Source = new BitmapImage(new Uri("/Resources/Edit.png", UriKind.Relative)), Width = 20, Height = 20 } }; editMenuItem.SetBinding(MenuItem.CommandParameterProperty, controllerBinding); contextMenu.Items.Add(editMenuItem); MenuItem deleteMenuItem = new MenuItem() { Command = InvoiceManagerCommands.Delete, Icon = new Image() { Source = new BitmapImage(new Uri("/Resources/Delete.png", UriKind.Relative)), Width = 20, Height = 20 } }; deleteMenuItem.SetBinding(MenuItem.CommandParameterProperty, controllerBinding); contextMenu.Items.Add(deleteMenuItem); style.Setters.Add(new Setter(ListViewItem.ContextMenuProperty, contextMenu)); Resources.Add(typeof (ListViewItem), style); #endregion }