I want to property grid open on Treeview item selection using c# - c#

I have been trying to open a property grid on the selection of a tree node and then bind the values of the property grid to the selected tree node in C#. This way if I create a new tree node it has its own values in the property grid.
Can anyone give an example of how that can be done or probably suggest me on the code below,
public partial class WPFpropertygriddemo : Window
{
public WPFpropertygriddemo()
{
InitializeComponent();
_propertyGrid.Visibility = Visibility.Hidden;
}
private void myTreeView_Selected(object sender, RoutedEventArgs e)
{
TreeViewItem tvi = e.OriginalSource as TreeViewItem;
Item itemsub = (Item)tvi.Header;
if (itemsub != null)
{
_propertyGrid.Visibility = Visibility.Visible;
// System.Windows.MessageBox.Show(itemsub.DisplayValue);
_propertyGrid.SelectedObject = itemsub;
}
}
}
public class Item : MyModelINotifyProperty
{
public string DisplayValue { get; set; }
private bool _isSelected = false;
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
OnPropertyChanged("IsSelected");
}
}
private string _sample;
public string Sample
{
get
{
return _sample;
}
set
{
_sample = value;
OnPropertyChanged("Sample");
}
}
}
public class MyViewModel : MyModelINotifyProperty
{
public ObservableCollection<Item> Items
{
get
{
return new ObservableCollection<Item>()
{
new Item() {DisplayValue = "Item1", IsSelected = false, Sample = "Sample: I am Item1"},
new Item() {DisplayValue = "Item2", IsSelected = true, Sample = "Sample: I am Item2"},
new Item() {DisplayValue = "Item3", IsSelected = false, Sample = "Sample: I am Item3"}
};
}
}
}
public abstract class MyModelINotifyProperty : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
What I don't understand is where is TreeViewItem defined and what is the code line
"TreeViewItem tvi = e.OriginalSource as TreeViewItem;
doing? I have an error at the TreeViewItem and Original source. Am I supposed to add a reference to use the e.original?
Thanks
Sree.

Related

OnPropertyChanged not updating UI for xzing barcode scanner for xamarin

I'm using mvvm thoughout my app and data binding and property changes are working everywhere else in the application but for some reason, on my view using XZing Barcode Scanner, my view is not updating after I scan an item.
I'm used to working with XAML ViewModel and Codebehind but they way I set up the barcode scanner is only ViewModel and Codebehind/alone class. So maybe I'm missing something simple that I am just not used to doing.
I've posted some code below that is an example of just one of the properties not updating, the PartName for productsLable.Text. It is bound when the page first loads and the dummy PartName shows correctly but after scanning a new item, I update the ScannedPart in my ViewModel but the "get" is never called after I set and call OnPropertyChanged();
This is working well in other parts of my application but maybe I am missing something silly here, like I said I am more comfortable working with bindings in XAML. Do you see anything I might be doing wrong or missing?
Thanks in advance.
public class TimsCustomScanPage : ContentPage
{
ZXingScannerView zxing;
TimsCustomOverlay overlay;
RequestPartsViewModel viewModel;
public TimsCustomScanPage(RequestPartsViewModel viewModel) : base()
{
//Attaching ViewModel
this.viewModel = viewModel;
this.BindingContext = viewModel;
//Scanner found barcode
zxing.OnScanResult += (result) =>
{
// Stop analysis until we navigate away so we don't keep reading barcodes
zxing.IsAnalyzing = false;
//Setting new scanned part
viewModel.ScannedPart = viewModel.Parts.FirstOrDefault();
};
--Some more code--
var productsLabel = new Label()
{
//Text = "Find New Part",
TextColor = Color.Black,
WidthRequest = 150,
HorizontalOptions = LayoutOptions.Start,
VerticalTextAlignment = TextAlignment.Center,
Padding = new Thickness(0, 0, 0, 0),
};
//Binding text property
productsLabel.SetBinding(Label.TextProperty, "PartName");
productsLabel.BindingContext = viewModel.ScannedPart;
}
ViewModel
public class RequestPartsViewModel : BaseViewModel
{
public ObservableCollection<TimsCategory> Categories { get; set; }
public ObservableCollection<TimsCategory> FavoriteCategories { get; set; }
public ObservableCollection<TimsPart> Parts { get; set; }
public TimsCategory CurrentSelection { get; set; }
public Command LoadItemsCommand { get; set; }
TimsPart scannedPart;
string partName;
int totalRequestedParts;
public RequestPartsViewModel()
{
Title = "Request Parts";
Categories = new ObservableCollection<TimsCategory>(db.Table<TimsCategory>().ToList());
Parts = new ObservableCollection<TimsPart>(db.Table<TimsPart>().ToList());
TotalRequestedParts = 0;
ScannedPart = new TimsPart();
ScannedPart.PartName = "Scan New Part";
ScannedPart.AvailableQuantity = 0;
//Attach parts to categories
foreach (var item in Categories)
{
int newId = item.CategoryId;
var partsForCategories = Parts.Where(p => p.CategoryId == newId);
var partsForCategoriesCollection = new ObservableCollection<TimsPart>(partsForCategories);
item.Parts = partsForCategoriesCollection;
}
FavoriteCategories = new ObservableCollection<TimsCategory>(Categories.Take(6).ToList());
LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand());
}
public TimsPart ScannedPart
{
get
{
return scannedPart;
}
set
{
if (scannedPart != value)
{
scannedPart = value;
OnPropertyChanged("PartName");
OnPropertyChanged("RequestedQuantity");
OnPropertyChanged("AvailableQuantity");
OnPropertyChanged("ScannedPart");
}
}
}
Base ViewModel
public class BaseViewModel : INotifyPropertyChanged
{
public SQLiteConnection db = TimsData.database;
bool isBusy = false;
public bool IsBusy
{
get { return isBusy; }
set { SetProperty(ref isBusy, value); }
}
string title = string.Empty;
public string Title
{
get { return title; }
set { SetProperty(ref title, value); }
}
protected bool SetProperty<T>(ref T backingStore, T value,
[CallerMemberName]string propertyName = "",
Action onChanged = null)
{
if (EqualityComparer<T>.Default.Equals(backingStore, value))
return false;
backingStore = value;
onChanged?.Invoke();
OnPropertyChanged(propertyName);
return true;
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
var changed = PropertyChanged;
if (changed == null)
return;
changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
I think the problem is that you are binding directly to a property, but then changing the base object, not just the property. Try this instead
productsLabel.SetBinding(Label.TextProperty, "ScannedPart.PartName");
// you already have a BindingContext set for your page that the Label will inherit
// productsLabel.BindingContext = viewModel.ScannedPart;

WPF DataGrid DataBinding is not displayed

I have a DataGrid in my current WPF Application which I would like to bind to a ViewModel that holds a ObservableCollection. The user can enter search values in some TextBoxes and after enter has been hit I am performing an query to our database that retunrs a table of records. From these records I am populate the data for the ObservableCollection. I am now struggeling now that the datagrid is not displaying the data.
I have read a howl bunch of posts about the binding but I am still missing something I think.
Product.cs
public class Product : InotifyPropertyChanged, IEditableObject
{
public string Title { get; set; } = "";
//public Product()
//{
//}
private ProductViewModel _productViewModel = new ProductViewModel();
public ProductViewModel productViewModel { get { return _productViewModel; } set { _productViewModel = value; } }
public DataTable ProductsTable { get; set; }
public void GetProducts(string filter)
{
//< --doing some stuff to fill the table-->
foreach (DataRow row in ProductsTable.Rows)
{
productViewModel.Products.Add(new Product
{
Title = (string)row["TITLE"],
});
}
}
}
ProductViewModel.cs
public class ProductViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private Product _SelectedProduct;
private ObservableCollection<Product> _Products = new ObservableCollection<Product>();
public ObservableCollection<Product> Products { get { return _Products; } set { _Products = value; } }
public ProductViewModel()
{
}
public void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
ProductWindow.xaml
<DataGrid
Name="ProductsGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding Products, Mode=TwoWay, NotifyOnSourceUpdated=True}"
SelectedItem="{Binding SelectedProduct, Mode=TwoWay}"
CanUserAddRows="False" SelectionUnit="FullRow"
VerticalAlignment="Stretch"
Grid.Row="0"
Margin="10,10,10,10"
>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Title}" Header="Title"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
ProductWindow.xaml.cs
public partial class ProductWindow : Page
{
public object DialogResult { get; private set; }
//public ProductViewModel ProductViewModel;
public ProductWindow()
{
InitializeComponent();
DataContext = new ProductViewModel();//stackflow
//var ProductViewModel = products.ProductViewModel;
//ProductsGrid.DataContext = new ProductViewModel();
}
public ProductViewModel ViewModel => DataContext as ProductViewModel;
private void OnKeydownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
var tb = sender as TextBox;
Product products = new Product();
string filter = "";//performing some ifelse to create filter
products.GetProducts(filter);
//ProductsGrid.DataContext = products.ProductsTable;
//ProductsGrid.DataContext = products.productViewModel;
}
else if (e.Key == Key.Escape)
{
ProductsGrid.DataContext = null;
foreach (TextBox tb in FindVisualChildren<TextBox>(this))
{
// do something with tb here
tb.Text = "";
}
}
}
}
If DataContext is a ProductViewModel, and the Products collection of that ProductViewModel is populated, you will see rows in your DataGrid. I've tested that. It appears that the viewmodel you're giving it may not have any rows.
That said, there's a problem with your design:
Product creates a ProductViewModel. ProductViewModel creates a collection of Product. Each Product, as I just said, creates a ProductViewModel. Which creates a collection of Product. They keep creating each other until you get a StackOverflowException. If you're not seeing that, you must be calling GetProducts() from somewhere else.
But there's no need for Product to own a copy of ProductViewModel. That's like adding a car to each wheel on your car.
So let's do this instead: ProductViewModel owns a collection of Product. Just that. And we'll call GetProducts() to make sure we get some items in the grid. Your binding is fine. You just weren't populating the collection.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ProductViewModel();
}
// Now you can call ViewModel.GetProducts(filterString) from an event handler.
// It would be more "correct" to use a Command, but let's take one step at a time.
public ProductViewModel ViewModel => DataContext as ProductViewModel;
}
Viewmodels
// You didn't include any implementation of IEditableObject. I presume
// you can add that back in to this version of the class.
public class Product : INotifyPropertyChanged, IEditableObject
{
// You weren't raising PropertyChanged here, or anywhere at all.
// In every setter on a viewmodel, you need to do that.
private string _title = "";
public string Title {
get => _title;
set
{
if (_title != value)
{
_title = value;
NotifyPropertyChanged(nameof(Title));
}
}
}
public Product()
{
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ProductViewModel : INotifyPropertyChanged
{
public ProductViewModel()
{
GetProducts("");
}
public event PropertyChangedEventHandler PropertyChanged;
private Product _SelectedProduct;
public Product SelectedProduct
{
get { return _SelectedProduct; }
set
{
if (value != _SelectedProduct)
{
_SelectedProduct = value;
NotifyPropertyChanged(nameof(SelectedProduct));
}
}
}
public DataTable ProductsTable { get; set; }
public void GetProducts(string filter)
{
//< --doing some stuff to fill the table-->
Products.Clear();
foreach (DataRow row in ProductsTable.Rows)
{
Products.Add(new Product
{
Title = (string)row["TITLE"],
});
}
}
private ObservableCollection<Product> _Products = new ObservableCollection<Product>();
// This setter MUST raise PropertyChanged. See the Title property above for example.
public ObservableCollection<Product> Products { get { return _Products; } private set { _Products = value; } }
public void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Update
Here's the problem: You create a new Product, which creates its own ProductsViewModel. Nothing is bound to any property of that viewmodel. You fill its collection and the DataGrid doesn't know or care, because you bound its ItemsSource to a property of a different object.
So use my suggestions above, particularly the ViewModel property of the window. I just made a change in ProductsViewModel.GetProducts() that you need to copy: Now it calls Products.Clear() before populating the collection.
if (e.Key == Key.Enter)
{
var tb = sender as TextBox;
// Don't create this
//Product products = new Product();
string filter = "";//performing some ifelse to create filter
ViewModel.GetProducts(filter);
}
else if (e.Key == Key.Escape)
{
// Setting the DataContext to null breaks everything. Never do that.
//ProductsGrid.DataContext = null;
// Instead, just clear the collection. It's an ObservableCollection so it will
// notify the DataGrid that it was cleared.
ViewModel.Products.Clear();
foreach (TextBox tb in FindVisualChildren<TextBox>(this))
{
// do something with tb here
tb.Text = "";
}
}

Collection changed event for child property

I have use the following code snippet for Creating ObservableCollection binded to the DataGrid.
public class Test:INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value;OnpropertyChanged("Name"); }
}
private string _city;
public string City
{
get { return _city; }
set
{
_city = value;OnpropertyChanged("City");}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void OnpropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
#endregion
}
class Data:INotifyPropertyChanged
{
private int customerID;
public int CustomerID
{
get { return customerID; }
set { customerID = value; OnpropertyChanged("CustomerID"); }
}
private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set { isSelected = value; OnpropertyChanged("IsSelected"); }
}
private ObservableCollection<Test> _collection;
public ObservableCollection<Test> Collection
{
get { return _collection; }
set { _collection = value;OnpropertyChanged("Collection" +
""); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnpropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
}
class ViewModel:NotificationObject
{
public ViewModel()
{
this.GDCSource = Getsource();
}
private ObservableCollection<Data> _gdcsource;
public ObservableCollection<Data> GDCSource
{
get { return _gdcsource; }
set { _gdcsource = value; RaisePropertyChanged("GDCSource");}
}
private ObservableCollection<Data> Getsource()
{
ObservableCollection<Data> items = new ObservableCollection<Data>();
if (items != null)
{
items.Add(new Data()
{
IsSelected = true,
CustomerID = 1,
});
items.Add(new Data()
{
IsSelected = true,
CustomerID = 2,
});
}
return items;
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ViewModel vmModel = new ViewModel();
this.datagrid.ItemsSource = vmModel.GDCSource;
vmModel.GDCSource.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(GDCSource_CollectionChanged);
}
void GDCSource_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
//Listen the collection changed event for underlying source
}
// add the object to the Collection property
private void Test_OnClick(object sender, RoutedEventArgs e)
{
(this.DataContext as ViewModel).GDCSource[0].Collection.Add(new Test() { Name = "Name1", City = "City1" });
(this.DataContext as ViewModel).GDCSource[0].Collection.Add(new Test() { Name = "Name1", City = "City1" });
(this.DataContext as ViewModel).GDCSource[0].Collection.Add(new Test() { Name = "Name1", City = "City1" });
}
}
It is possible to listen while adding Collection property in any event.
Thanks in advance
Regards,
Rajasekar
If you mean you want to register for event that is raised when item is added/deleted in observable collection you should look at CollectionChanged event
ObservableCollection<T>.CollectionChanged Event
Occurs when an item is added, removed, changed, moved, or the entire
list is refreshed.
You can extend your own version of ObservableCollection if you want and override the add method,
There you can fire any delegates or whatever you may want to register, the UI will update automatically using ObservableCollection with items added/removed you don't need to do anything for that,

listbox data binding fails silently

This approach seems to work half the time for me.
I can see these lines get executed in the debugger:
agencyListBox.DataBindings.Add(new Binding("DataSource", this.Data.Agencies, "AvailableAgencies"));
agencyListBox.DataBindings.Add(new Binding("SelectedItem", this.Data.Agencies, "SelectedAgency", false, DataSourceUpdateMode.OnPropertyChanged));
The agencies class looks like this:
public AgencyType SelectedAgency
{
get
{
return _selected;
}
set
{
_selected = value;
OnPropertyChanged("SelectedAgency");
}
}
public List<AgencyType> AvailableAgencies
{
get
{
return _availableList;
}
set
{
_availableList = value;
OnPropertyChanged("AvailableAgencies");
}
}
So the fields I reference in the binding do exist.
The DisplayMember is set to "Label" which is defined in the AgencyType class:
public event PropertyChangedEventHandler PropertyChanged;
private string _label { get; set; }
public string Label
{
get { return _label; }
set
{
_label = value;
OnPropertyChanged("Label");
}
}
private string _identifier { get; set; }
public string Identifier
{
get { return _identifier; }
set
{
_identifier = value;
OnPropertyChanged("Identifier");
}
}
public AgencyType()
{
Label = string.Empty;
Identifier = string.Empty;
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
The values are displayed as desired.
But then when I change the selection, data.Agencies.SelectedAgency is null!
Does anyone have any tips?

TextBox binding not initialising

I'm have a TextBox which gets added to a Window alongside a Checkbox. I've managed to bind the TextBox to property Order of the CheckedListItem handler so when a change is made it binds correctly and updates.
My problem I'm having is that I cannot get it to initialise with starting values. My constructor is as follows
public partial class OwnerSettingWindow : Window
{
public ObservableCollection<CheckedListItem<Owner>> Owners { get; set; }
public class Owner
{
public String OwnerName { get; set; }
public String OwnerOrder { get; set; }
}
public OwnerSettingWindow()
{
InitializeComponent();
Owners = new ObservableCollection<CheckedListItem<Owner>>();
string testString = #"Item1,true,1:Item2,true,2:Item3,false,24"; ;
string[] splitOwners = testString.Split(':');
foreach (string item in splitOwners)
{
string[] spOwnerSetting = item.Split(',');
bool bchecked = bool.Parse(spOwnerSetting[1].ToString());
string norder = spOwnerSetting[2].ToString();
Owners.Add(new CheckedListItem<Owner>(new Owner() { OwnerName = spOwnerSetting[0].ToString(), OwnerOrder = norder },
isChecked: bchecked));
}
DataContext = this;
}
public class CheckedListItem<T> : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool isChecked;
private T item;
private string order;
public CheckedListItem()
{ }
public CheckedListItem(T item, bool isChecked = false)
{
this.item = item;
this.isChecked = isChecked;
}
public T Item
{
get { return item; }
set
{
item = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Item"));
}
}
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
}
}
public string Order
{
get { return order; }
set
{
order = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Order"));
}
}
}
}
The following line not working as intended
Owners.Add(new CheckedListItem<Owner>(new Owner() { OwnerName =
spOwnerSetting[0].ToString(), OwnerOrder = norder },
isChecked: bchecked));
OwnerOrder = norder is not showing when the Window is opened. My TextBox binding in XAML is simply <TextBox Text ="{Binding Order}"/>
I've also tried the following with no success
Owners.Add(new CheckedListItem<Owner>(new Owner() { OwnerName = spOwnerSetting[0].ToString() },
isChecked: bchecked, order: norder));
Any ideas?
Order Property in CheckedListItem is never initialized. you can add a new ctor
public CheckedListItem(T item, string nrorder, bool isChecked = false)
{
this.item = item;
this.isChecked = isChecked;
this.order = nrorder;
}
and change your adding method
Owners.Add(new CheckedListItem<Owner>(new Owner() { OwnerName = spOwnerSetting[0].ToString(), OwnerOrder = norder }, norder, isChecked: bchecked));
In my test app now is working.

Categories

Resources