Reputation: 15698
First, this is my first attempt at using WPF. I'll try to spare as much information as possible, since this data-entry form is a little less than typical. However, if more information is needed, please ask.
I have a WPF window with a series of CollectionViewSource
objects in my Window.Resources
collection. These are initialized in my window's constructor.
One of these collections is an IList<T>
where T
is of a POCO type used with Entity Framework.
My form has a section of user-input controls. When the user hits the Save
button, I need to create an entity and add it to my IList
collection.
So far, I've had no trouble. I can create the entity, and I can add it to the collection without any issue. However, the problem is that I have a ListView
control that is bound to this collection. I can, and have verified, that I can load records to this list when the Window initializes in the constructor. However, when I click my Save
button, the collection gets updated, but the UI does not refresh the operation.
For reference, here is the pertinent code below.
SetResource() - Called in the constructor of my Window
private void SetResources()
{
... other objects...
//myCacheSrc is property defined in my Window.cs file.
myCacheSrc = (CollectionViewSource)FindResource("myCacheSrc");
myCacheSrc.Source = new List<MyEFPOCO>();
... more initializations of other objects.
}
Contents of my Save button click event
private void Save_Click(object sender, RoutedEventArgs e)
{
var cache = myCacheSrc.Source as List<MyEFPOCO>;
if (cache != null && !CurrentRecord.Equals(origCurrentRecord))
{
cache.Add(CurrentRecord);
}
}
It is worth mentioning, I've stepped through the above block of code. It is executing on the save event, and the CurrentRecord
property does contain a valid entity.
Here is a my Window constructor. Notice that there is a test insert operation. This works! Note, it's not much different than the code in my button event. Why would this work, and not the button event?
Window Constructor
public MyWindow()
{
InitializeComponent();
SetResources();
//TEST
var cache = myCacheSrc.Source as List<MyEFPOCO>;
if (cache != null)
{
cache.Add(new MyEFPOCO
{
userEntryType = "TEST",
numberValue= 1234567
});
}
}
MyWindow.xaml - Declaration of the ListView in question
<ListView Name="CachedChangesList" ItemsSource="{Binding Source={StaticResource myCacheSrc}}">
<ListView.ItemTemplate>
<DataTemplate>
<DockPanel>
<Button Name="UpdateChange" Content="{StaticResource iUpdate}" Width="20" />
<Button Name="DeleteChange" Content="{StaticResource iDelete}" Width="20" />
<Label Content="{Binding userEntryType}" />
<Label Content="{Binding numberValue}" />
</DockPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Upvotes: 0
Views: 566
Reputation: 5366
Use ObservableCollection Instead of List, Because it will notify the UI about the source change. https://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx
myCacheSrc = (CollectionViewSource)FindResource("myCacheSrc");
myCacheSrc.Source = new ObservableCollection<MyEFPOCO>();
Upvotes: 2