Click here to Skip to main content
14,428,410 members

Silverlight / WPF

 
GeneralRe: Custom SplitButton Control Styling Pin
Gerry Schmitz19-May-17 6:33
mveGerry Schmitz19-May-17 6:33 
GeneralRe: Custom SplitButton Control Styling Pin
Kevin Marois22-May-17 6:23
professionalKevin Marois22-May-17 6:23 
GeneralRe: Custom SplitButton Control Styling Pin
Gerry Schmitz22-May-17 7:15
mveGerry Schmitz22-May-17 7:15 
GeneralRe: Custom SplitButton Control Styling Pin
Gerry Schmitz22-May-17 7:35
mveGerry Schmitz22-May-17 7:35 
GeneralRe: Custom SplitButton Control Styling Pin
Kevin Marois22-May-17 7:50
professionalKevin Marois22-May-17 7:50 
GeneralRe: Custom SplitButton Control Styling Pin
Gerry Schmitz22-May-17 8:07
mveGerry Schmitz22-May-17 8:07 
QuestionTheming Question Pin
Kevin Marois5-May-17 8:29
professionalKevin Marois5-May-17 8:29 
QuestionWPF MVVM ListView Bound to ObservableCollection doesnt update after i make changes Pin
Member 1288059527-Apr-17 8:25
MemberMember 1288059527-Apr-17 8:25 
Hello.
I am trying to make a small "book library" application where readers can rent books.
My application is tructured like this: 4 views(with their associated viewmodels). MainWindow is the main view. Inside it i have a content control where i display the other 3 secondary/child views:HomeView, BookManagingView, ReaderManagingView. The default selected view is HomeView, where i have 2 ListViews(for Readers and Books), and some buttons. In my ReaderManagingView i have some textboxes and buttons to add/update/delete readers from database and listview.

The database has 3 tables: Books, Readers, and RentedBooks.

What i am trying to do is: when i add/update/delete users from my database, i want my changes to reflect (almost)instantly in my ListView.
The problem: The changes are visible only after i restart the application. What should i do, so that my Readers ListView updates after i add/update/delete a user?

Here is some code:
HomeView:

<ListView x:Name="listviewReaders" ItemsSource="{Binding ReadersList, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" SelectedItem="{Binding SelectedReader, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="160" Margin="25,23,315,40">
       <ListView.ItemTemplate>
           <DataTemplate>
               <TextBlock Text="{Binding FullName}" />
           </DataTemplate>
       </ListView.ItemTemplate>
   </ListView>


ReaderManagingView:
    <Grid>
    <TextBox x:Name="txtBxFullName" HorizontalAlignment="Left" Height="23" Margin="25,27,0,0" TextWrapping="Wrap" Text="{Binding CurrentReader.FullName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="txtBxSerialNumber" HorizontalAlignment="Left" Height="23" Margin="25,55,0,0" TextWrapping="Wrap" Text="{Binding CurrentReader.SerialNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="txtBxIdNumber" HorizontalAlignment="Left" Height="23" Margin="25,83,0,0" TextWrapping="Wrap" Text="{Binding CurrentReader.IdNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="txtBxAdress" HorizontalAlignment="Left" Height="23" Margin="25,111,0,0" TextWrapping="Wrap" Text="{Binding CurrentReader.Adress, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="txtBxAltContactMethods" HorizontalAlignment="Left" Height="23" Margin="25,139,0,0" TextWrapping="Wrap" Text="{Binding CurrentReader.AltContactMethods, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
    <Button x:Name="btnAddReader" Command="{Binding AddR, UpdateSourceTrigger=PropertyChanged}" Content="Add" HorizontalAlignment="Left" Margin="25,177,0,0" VerticalAlignment="Top" Width="60"/>
    <Button x:Name="btnDeleteReader" Command="{Binding DeleteR, UpdateSourceTrigger=PropertyChanged}" Content="Delete" HorizontalAlignment="Left" Margin="155,177,0,0" VerticalAlignment="Top" Width="60"/>
    <Button x:Name="btnClearReader" Command="{Binding ClearR, UpdateSourceTrigger=PropertyChanged}" Content="Clear" HorizontalAlignment="Left" Margin="220,177,0,0" VerticalAlignment="Top" Width="60"/>
    <Button x:Name="btnEditReader" Command="{Binding EditR, UpdateSourceTrigger=PropertyChanged}" Content="Save" HorizontalAlignment="Left" Margin="90,177,0,0" VerticalAlignment="Top" Width="60"/>

</Grid>


HomeVM:
public class HomeViewModel : ViewModelBase
{
    private Reader selectedReader;
    private Book selectedBook;
    private BookListFilter selectedFilter;


    private ObservableCollection<Book> bookList;
    private ObservableCollection<Reader> readerList;
    private IEnumerable<BookListFilter> bookLstItemSrc;

    public HomeViewModel(MainWindowViewModel _mwvm)
    {
        Mwvm = _mwvm;
        SelectedReader = new Reader();
        SelectedBook = new Book();
        SelectedFilter = BookListFilter.AllBooks;

        BookDBDataContext rdb = new BookDBDataContext();

        ReadersList = new ObservableCollection<Reader>(rdb.Readers);
        GetBookList();

        EditReaderSwitch = new DefCommand(EditReaderInfo);
        EditBookSwitch = new DefCommand(EditBookInfo);
    }

    public DefCommand EditReaderSwitch { get; private set; }
    public DefCommand EditBookSwitch { get; private set; }

    public MainWindowViewModel Mwvm { get; set; }

    //Reader List
    public ObservableCollection<Reader> ReadersList
    {
        get { return readerList; }
        set
        {
            if (readerList != value)
            {
                readerList = value;
                RaisePropertyChanged();
            }
        }
    }


ReaderManagingViewModel:
public class ReaderManagingViewModel : ViewModelBase
{
    private static Reader currentReader;

    public ReaderManagingViewModel(HomeViewModel hvm)
    {
        if (CurrentReader == null)
            CurrentReader = new Reader();

        CurrentReader = hvm.SelectedReader;

        AddR = new DefCommand(AddReader);
        EditR = new DefCommand(UpdateReader);
        DeleteR = new DefCommand(DeleteReader);
        ClearR = new DefCommand(ClearReaderFields);
    }


    public Reader CurrentReader
    {
        get { return currentReader; }
        set
        {
            if (currentReader != value)
            {
                currentReader = value;
                RaisePropertyChanged();
            }
        }
    }

    public DefCommand AddR { get; private set; }
    public DefCommand EditR { get; private set; }
    public DefCommand DeleteR { get; private set; }
    public DefCommand ClearR { get; private set; }

    private void AddReader()
    {
        BookDBDataContext db = new BookDBDataContext();
        Reader rObj = new Reader();
        rObj.FullName = CurrentReader.FullName;
        rObj.SerialNumber = CurrentReader.SerialNumber;
        rObj.Id = CurrentReader.Id;
        rObj.Adress = CurrentReader.Adress;
        rObj.AltContactMethods = CurrentReader.AltContactMethods;


        try
        {
            db.Readers.InsertOnSubmit(rObj);
            db.Readers.Context.SubmitChanges();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        //Console.WriteLine("addingrrrr");
    }

    private void UpdateReader()
    {
        BookDBDataContext db = new BookDBDataContext();
        var qry = from reader in db.Readers
                  where reader.Id == CurrentReader.Id
                  select reader;
        foreach (Reader r in qry)
        {
            r.FullName = CurrentReader.FullName;
            r.SerialNumber = CurrentReader.SerialNumber;
            r.IdNumber = CurrentReader.IdNumber;
            r.Adress = CurrentReader.Adress;
            r.AltContactMethods = CurrentReader.AltContactMethods;
        }

        try
        {
            db.SubmitChanges();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        //Console.WriteLine("editinrrrrr");
    }

    private void DeleteReader()
    {
        BookDBDataContext db = new BookDBDataContext();
        var rbTbl = (from r in db.RentedBooks
                 where r.ReaderId == CurrentReader.Id
                 select r);
        var rTbl = (from r in db.Readers
                   where r.Id == CurrentReader.Id
                   select r).SingleOrDefault();
        try
        {
            foreach (var rbr in rbTbl)
            {
                db.RentedBooks.DeleteOnSubmit(rbr);
            }

            db.Readers.DeleteOnSubmit(rTbl);
            db.SubmitChanges();
        }
        catch(Exception e)
        {
            Console.WriteLine(e);
        }
        Console.WriteLine("deleting");
    }}

AnswerRe: WPF MVVM ListView Bound to ObservableCollection doesnt update after i make changes Pin
Richard Deeming27-Apr-17 9:26
communityengineerRichard Deeming27-Apr-17 9:26 
GeneralRe: WPF MVVM ListView Bound to ObservableCollection doesnt update after i make changes Pin
Member 1288059527-Apr-17 10:02
MemberMember 1288059527-Apr-17 10:02 
GeneralRe: WPF MVVM ListView Bound to ObservableCollection doesnt update after i make changes Pin
Member 1288059528-Apr-17 11:12
MemberMember 1288059528-Apr-17 11:12 
QuestionWPF Control Template Documentation Pin
Kevin Marois20-Apr-17 6:16
professionalKevin Marois20-Apr-17 6:16 
AnswerRe: WPF Control Template Documentation Pin
CHill6020-Apr-17 6:21
mveCHill6020-Apr-17 6:21 
GeneralRe: WPF Control Template Documentation Pin
Kevin Marois20-Apr-17 7:12
professionalKevin Marois20-Apr-17 7:12 
GeneralRe: WPF Control Template Documentation Pin
CHill6020-Apr-17 8:23
mveCHill6020-Apr-17 8:23 
GeneralRe: WPF Control Template Documentation Pin
Kevin Marois20-Apr-17 8:24
professionalKevin Marois20-Apr-17 8:24 
AnswerRe: WPF Control Template Documentation Pin
Pete O'Hanlon24-Apr-17 21:31
communityengineerPete O'Hanlon24-Apr-17 21:31 
QuestionComboBox Control Template Problem Pin
Kevin Marois19-Apr-17 13:13
professionalKevin Marois19-Apr-17 13:13 
AnswerRe: ComboBox Control Template Problem Pin
Pete O'Hanlon19-Apr-17 22:42
communityengineerPete O'Hanlon19-Apr-17 22:42 
GeneralRe: ComboBox Control Template Problem Pin
Kevin Marois20-Apr-17 5:38
professionalKevin Marois20-Apr-17 5:38 
QuestionWPF Enum DP - The default value type does not match the type of the property Pin
Kevin Marois18-Apr-17 8:21
professionalKevin Marois18-Apr-17 8:21 
AnswerRe: WPF Enum DP - The default value type does not match the type of the property Pin
Pete O'Hanlon20-Apr-17 0:34
communityengineerPete O'Hanlon20-Apr-17 0:34 
QuestionTimeline Thumb Style Pin
Kevin Marois18-Apr-17 7:19
professionalKevin Marois18-Apr-17 7:19 
QuestionInterpret an Error Pin
Mycroft Holmes28-Mar-17 23:31
MemberMycroft Holmes28-Mar-17 23:31 
AnswerRe: Interpret an Error - Resolved Pin
Mycroft Holmes28-Mar-17 23:51
MemberMycroft Holmes28-Mar-17 23:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.