NotifyPropertyChanged not firing - c#

I am trying to capture the SelectedItem for a ListBox when then data template click event is triggered for the Button. I put a breakpoint in the notifypropertychanged event handler, but it never triggers. What am I doing wrong here?
xaml:
<ListBox x:Name="lstbox_playerContainer"
ItemsSource="{Binding ChildObjectOC}"
SelectedItem="{Binding SelectedChildObject, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Name="btn_childButton" Click="btn_childButton_Click"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
c#:
public partial class PlayerCrowdPromptPage: INotifyPropertyChanged {
public PlayerCrowdPromptPage() {
InitializeComponent();
DataContext = this;
}
private ObservableCollection<PlayerCrowdObjectBO> childObjectOC = new ObservableCollection<PlayerCrowdObjectBO>();
public ObservableCollection<PlayerCrowdObjectBO> ChildObjectOC {
get {
return childObjectOC;
}
set {
childObjectOC = value;
}
}
private PlayerCrowdObjectBO selectedChildObject;
public PlayerCrowdObjectBO SelectedChildObject {
get { return selectedChildObject; }
set {
selectedChildObject = value;
OnPropertyChanged("SelectedChildObject");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null){
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

Clicking on the Button won't automatically select the corresponding item. You may select it programmatically in the event handler though:
private void btn_childButton_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
ListBoxItem lbi = lstbox_playerContainer.ItemContainerGenerator.ContainerFromItem(btn.DataContext) as ListBoxItem;
lbi.IsSelected = true;
}
This should set the SelectedChildObject property and raise the PropertyChanged event.

Related

Revit API WPF C#: Check All button for Check Boxes in Listbox

I'm pretty new to programming with WPF and C# and I have a question regarding the possibility to automatically check all the CheckBoxes in a Listbox. I'm developing a plugin for Autodesk Revit and, after having listed all the names of the rooms in a list box, I want to check them all using the button "Check All"
I've read the thread at this page but still, I'm not able to make it work. May someone help me with my code?
Here is what I've done:
XAML:
<ListBox x:Name='roomlist'
SelectionMode='Multiple'>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked='{Binding IsChecked}'
Content="{Binding}" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.InputBindings>
<KeyBinding Command="ApplicationCommands.SelectAll"
Modifiers="Ctrl"
Key="A" />
</ListBox.InputBindings>
<ListBox.CommandBindings>
<CommandBinding Command="ApplicationCommands.SelectAll" />
</ListBox.CommandBindings>
</ListBox>
C#
public partial class RoomsDistance_Form : Window
{
UIDocument _uidoc;
Document _doc;
public RoomsDistance_Form(Document doc, UIDocument uidoc)
{
InitializeComponent();
FilteredElementCollector collector = new FilteredElementCollector(doc)
.WhereElementIsNotElementType()
.OfCategory(BuiltInCategory.OST_Rooms);
List<String> myRooms = new List<String>();
foreach (var c in collector)
{
myRooms.Add(c.Name);
}
myRooms.Sort();
roomlist.ItemsSource = myRooms;
}
private void checkAllBtn_Click(object sender, RoutedEventArgs e)
{
foreach (CheckBox item in roomlist.Items.OfType<CheckBox>())
{
item.IsChecked = true;
}
}
public class Authority : INotifyPropertyChanged
{
private bool isChecked;
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
NotifyPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Thank you very much for your help!
In the thread you are linking to, they are setting the "IsChecked" on the data object (Authority), not the CheckBox control itself.
foreach (var a in authorityList)
{
a.IsChecked = true;
}
You have a binding to IsChecked that will update the Checkbox control when NotifyPropertyChanged() is called.
After having lost my mind in the effort i solved my problem by avoiding the Listbox.. I simply added single CheckBoxes in the StackPanel.
XAML:
<ScrollViewer Margin='10,45,10,100'
BorderThickness='1'>
<StackPanel x:Name='stack'
Grid.Column='0'></StackPanel>
</ScrollViewer>
C#:
foreach (var x in myRooms)
{
CheckBox chk = new CheckBox();
chk.Content = x;
stack.Children.Add(chk);
}
Not what i was looking for but now it works and that's the point.
Thank you for your help!
I usually use CheckBoxList in the following way:
In xaml:
<ListBox ItemsSource="{Binding ListBoxItems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> //+some dimensional properties
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In xaml.cs:
public partial class MyWindow : Window
{
public ViewModel ViewModel {get; set; }
public MyWindow(ViewModel viewModel)
{
//keep all the mess in ViewModel, this way your xaml.cs will not end up with 1k lines
ViewModel = viewModel;
DataContext = ViewModel;
InitializeComponent();
}
void BtnClick_SelectAll(object sender, RoutedEventArgs e)
{
ViewModel.CheckAll();
}
}
ViewModel preparation:
public class ViewModel
{
public List<ListBoxItem> ListBoxItems { get; set; }
//InitializeViewModel()...
//UpdateViewModel()...
//other things....
public void CheckAll()
{
foreach (var item in ListBoxItems)
{
item.IsSelected = true;
}
}
public class ListBoxItem : INotifyPropertyChanged
{
public string Name { get; set; }
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
if (_isSelected != value)
{
_isSelected = value;
OnPropertyChanged(nameof(IsSelected));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

Prevent Bound ComboBox from firing SelectionChanged Event at application startup

I have a ComboBox bound to a ViewModel Items Source List.
The ComboBox also has a SelectionChanged Event.
I want the event to fire only when the user has clicked the ComboBox and selected a new item.
But the event fires at application startup when the bound items are loaded and the default item is programmatically selected.
XAML
<ComboBox x:Name="cboDisplay"
ItemsSource="{Binding Display_Items}"
SelectedValue="{Binding Display_SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Margin="10,10,0,0"
Height="22"
Width="100"
SelectionChanged="cboDisplay_SelectionChanged" />
C#
private void cboDisplay_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show("Event Fired");
}
ViewModel
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void OnPropertyChanged(string prop)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(prop));
}
}
public MainViewModel()
{
// Default Item at Startup
Display_SelectedItem = "Windowed";
}
// Items Source
//
private List<string> _Display_Items = new List<string>()
{
"Fullscreen",
"Windowed"
};
public List<string> Display_Items
{
get { return _Display_Items; }
set
{
_Display_Items = value;
OnPropertyChanged("Display_Items");
}
}
// Selected Item
//
private string _Display_SelectedItem { get; set; }
public string Display_SelectedItem
{
get { return _Display_SelectedItem; }
set
{
if (_Display_SelectedItem == value)
{
return;
}
_Display_SelectedItem = value;
OnPropertyChanged("Display_SelectedItem");
}
}

Unchecking One of the child checkboxes should uncheck the parent checkbox in WPF MVVM pattern

First two ways are working for me, the third and fourth points mentioned below by me are not working
if i check the header check box ,all the child checkbox will be checked
if uncheck, all will be unchecked
if everything is checked..if i uncheck one child checkbox...header checkbox will be unchecked
if i check one by one all the child checkbox....header checkbox will be checked.
Here is my code
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<CheckBox Content="Included" x:Name="headerCheckBox" />
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chkselectAll" IsChecked="{Binding IsChecked,ElementName=headerCheckBox,Mode=OneWay}"></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
could I get some help here. How do I achieve it in xaml?
I want the select all checkbox behaviour like this
https://www.codeproject.com/Articles/42437/Toggling-the-States-of-all-CheckBoxes-Inside-a-Dat#
You should bind the CheckBox in the CellTemplate to a source property of your data object:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chkselectAll" IsChecked="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}"></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
Make sure that the data object implements the INotifyPropertyChanged interface:
public class Item : INotifyPropertyChanged
{
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set { _isChecked = value; OnPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
You could then bind bind the property of the header CheckBox to a property of your view model, that you set whenever an item in the source collection of the DataGrid is changed, e.g.:
<DataGridTemplateColumn.Header>
<CheckBox Content="Included"
x:Name="headerCheckBox"
IsChecked="{Binding DataContext.AllChecked, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</DataGridTemplateColumn.Header>
public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
Items = new ObservableCollection<Item>();
Items.CollectionChanged += Items_CollectionChanged;
//add the items..:
Items.Add(new Item());
Items.Add(new Item() { IsChecked = true });
Items.Add(new Item());
}
private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (object item in e.NewItems)
{
(item as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
}
}
if (e.OldItems != null)
{
foreach (object country in e.OldItems)
{
(country as INotifyPropertyChanged).PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
}
}
}
private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
OnPropertyChanged("AllChecked");
}
public ObservableCollection<Item> Items { get; private set; }
public bool AllChecked
{
get { return Items.All(x => x.IsChecked); }
set
{
foreach (var item in Items)
item.IsChecked = value;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

Setting checkbox selected items to False in User interface - C# WPF

I have a simple checkbox items and when items are selected, it works fine.
I put a button to unselect all selected items . In the debug mode, I can see the checked state being set to unchecked (false) although it is not reflected in the UI. Here is the code:
XAML for Listbox-Checkbox:
<ListBox x:Name="Listitems" Grid.Column="0" SelectionMode="Multiple" ItemsSource="{Binding MonthlyResults}" >
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding logdate}" IsChecked="{Binding Checked ,Mode=TwoWay}" Click="CheckBox_Click"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
XAML for UncheckALL button:
<Button Grid.Row="0" Name="ClearALL" Margin="4,10,4,75" Content="Unselect All" FontFamily="Tahoma" FontSize="12" Click="Button_Click"/>
Code behind:
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
var cb = sender as CheckBox;
var item = cb.DataContext;
Listitems.SelectedItem = item;
HornerPlotPluginModel model = DataContext as HornerPlotPluginModel;
var checkedItems1 = model.MonthlyResults.Where(B => B.Checked == true);
//monthlyresults is the observable collection that populates the checkbox items
model.CDFResults.Clear(); // some function
Chart1.Series.Clear();
Chart1.Axes.Clear();
model.DisplayLogs(); // some function
DrawCurves(); // some function
}
Code behind for the UncheckAll button:
private void Button_Click(object sender, RoutedEventArgs e)
{
HornerPlotPluginModel model = DataContext as HornerPlotPluginModel;
var checkedItems1 = model.MonthlyResults.Where(B => B.Checked == true);
Listitems.SelectedItems.Clear(); //SET CHECKED ITEMS TO FALSE!!!
model.CDFResults.Clear();
Chart1.Series.Clear();
}
I did look at similar post here: WPF UserControl property change not updating
but it went over my head!
Make sure that the class where the Checked property is defined implements the INotifyPropertyChanged interface and raises the PropertyChanged event in the setter of the Checked property:
public class MonthlyReport : INotifyPropertyChanged
{
private bool _checked;
public bool Checked
{
get { return _checked; }
set { _checked = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Then you should be able to simply set the Checked property of all those objects to false to refresh the CheckBox:
HornerPlotPluginModel model = DataContext as HornerPlotPluginModel;
foreach(var item in model.MonthlyResults)
{
item.Checked = false;
}
HornerPlotPluginModel model = DataContext as HornerPlotPluginModel;
foreach(var item in model.MonthlyResults)
{
item.Checked = false;
}

ListView not updating on the itemsource ObservableCollection Item Property change

Consider the following Sample Code :
View.xml
<Grid>
<ListView Name="NameList" HorizontalAlignment="Left" Height="142" Margin="55,45,0,0" VerticalAlignment="Top" Width="389">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Content="{Binding FirstName}"/>
<Label Content="{Binding LastName}" Grid.Column="1"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Content="Button" HorizontalAlignment="Left" Margin="120,256,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
View.xml.cs
public partial class MainWindow : Window
{
ViewModel vm;
public MainWindow()
{
InitializeComponent();
vm = new ViewModel();
this.DataContext = vm;
NameList.ItemsSource = vm.fn;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
vm.fn.Add(new fullname("P", "Q"));
vm.fn[0].FirstName = "NewName";
}
}
ViewModel.cs
class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
fn = new ObservableCollection<fullname>();
fn.CollectionChanged += ContentCollectionChanged;
fn.Add(new fullname("A", "B"));
fn.Add(new fullname("C", "D"));
}
public void ContentCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (fullname item in e.OldItems)
{
//Removed items
item.PropertyChanged -= EntityViewModelPropertyChanged;
}
}
else if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (fullname item in e.NewItems)
{
//Added items
item.PropertyChanged += EntityViewModelPropertyChanged;
}
}
}
public void EntityViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
{
//This will get called when the property of an object inside the collection changes
}
public ObservableCollection<fullname> _fn;
public ObservableCollection<fullname> fn
{
get { return _fn; }
set { _fn = value; OnPropertyChanged("fn"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
}
}
}
Model.cs
class fullname : INotifyPropertyChanged
{
public fullname(string f, string l)
{
FirstName = f;
LastName = l;
}
public string _FirstName;
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; OnPropertyChanged("FirstName"); }
}
public string _LastName;
public string LastName
{
get { return _LastName; }
set { _LastName = value; OnPropertyChanged("LastName"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
}
}
}
If I add or remove any items in the ObservableCollection it updates the ListView in the View correctly but the problem is that if i modify the ObservableCollection item property the ListView is not updated.
for ex:
On click of above specified button it
a. Adds a new item (Successfully reflected in the ListView)
b. Modify FirstName of first item (Not reflected in the ListView)
What should i do so as to make the modifications reflect in View.
I would be very thankful if anyone could point out what i am doing wrong.
ObservableCollection event are not fired when an item is changed, only when it is added, removed or moved.
You must implement INotifyPropertyChanged interface in item class, and register each item event in collection.
See here
If you make heavy changes in the collection, you can raise a
NotifyCollectionChangedAction.Reset
event to reset them all
See here
I was also struggling with the same issue on WinUI with ListView and ObservableCollection.
What you need to understand is that ObservableCollection is only responsible to notify collection changes. Item level change are handle through INotifyPropertyChanged. However for those to be applied you need to make sure your binding mode is at least one way, two way if you need to.
Your XAML should look like that:
Text="{x:Bind Title, Mode=OneWay}
Do not attempt what's proposed there, it's just wrong:
ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged)

Categories