Click here to Skip to main content
15,887,361 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: Custom SplitButton Control Styling Pin
Gerry Schmitz19-May-17 5:33
mveGerry Schmitz19-May-17 5:33 
GeneralRe: Custom SplitButton Control Styling Pin
Kevin Marois22-May-17 5:23
professionalKevin Marois22-May-17 5:23 
GeneralRe: Custom SplitButton Control Styling Pin
Gerry Schmitz22-May-17 6:15
mveGerry Schmitz22-May-17 6:15 
GeneralRe: Custom SplitButton Control Styling Pin
Gerry Schmitz22-May-17 6:35
mveGerry Schmitz22-May-17 6:35 
GeneralRe: Custom SplitButton Control Styling Pin
Kevin Marois22-May-17 6:50
professionalKevin Marois22-May-17 6:50 
GeneralRe: Custom SplitButton Control Styling Pin
Gerry Schmitz22-May-17 7:07
mveGerry Schmitz22-May-17 7:07 
QuestionTheming Question Pin
Kevin Marois5-May-17 7:29
professionalKevin Marois5-May-17 7:29 
QuestionWPF MVVM ListView Bound to ObservableCollection doesnt update after i make changes Pin
Member 1288059527-Apr-17 7:25
Member 1288059527-Apr-17 7: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:

XML
<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:
XML
    <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:
C#
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:
C#
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 8:26
mveRichard Deeming27-Apr-17 8:26 
GeneralRe: WPF MVVM ListView Bound to ObservableCollection doesnt update after i make changes Pin
Member 1288059527-Apr-17 9:02
Member 1288059527-Apr-17 9:02 
GeneralRe: WPF MVVM ListView Bound to ObservableCollection doesnt update after i make changes Pin
Member 1288059528-Apr-17 10:12
Member 1288059528-Apr-17 10:12 
QuestionWPF Control Template Documentation Pin
Kevin Marois20-Apr-17 5:16
professionalKevin Marois20-Apr-17 5:16 
AnswerRe: WPF Control Template Documentation Pin
CHill6020-Apr-17 5:21
mveCHill6020-Apr-17 5:21 
GeneralRe: WPF Control Template Documentation Pin
Kevin Marois20-Apr-17 6:12
professionalKevin Marois20-Apr-17 6:12 
GeneralRe: WPF Control Template Documentation Pin
CHill6020-Apr-17 7:23
mveCHill6020-Apr-17 7:23 
GeneralRe: WPF Control Template Documentation Pin
Kevin Marois20-Apr-17 7:24
professionalKevin Marois20-Apr-17 7:24 
AnswerRe: WPF Control Template Documentation Pin
Pete O'Hanlon24-Apr-17 20:31
mvePete O'Hanlon24-Apr-17 20:31 
QuestionComboBox Control Template Problem Pin
Kevin Marois19-Apr-17 12:13
professionalKevin Marois19-Apr-17 12:13 
AnswerRe: ComboBox Control Template Problem Pin
Pete O'Hanlon19-Apr-17 21:42
mvePete O'Hanlon19-Apr-17 21:42 
GeneralRe: ComboBox Control Template Problem Pin
Kevin Marois20-Apr-17 4:38
professionalKevin Marois20-Apr-17 4:38 
QuestionWPF Enum DP - The default value type does not match the type of the property Pin
Kevin Marois18-Apr-17 7:21
professionalKevin Marois18-Apr-17 7:21 
AnswerRe: WPF Enum DP - The default value type does not match the type of the property Pin
Pete O'Hanlon19-Apr-17 23:34
mvePete O'Hanlon19-Apr-17 23:34 
QuestionTimeline Thumb Style Pin
Kevin Marois18-Apr-17 6:19
professionalKevin Marois18-Apr-17 6:19 
QuestionInterpret an Error Pin
Mycroft Holmes28-Mar-17 22:31
professionalMycroft Holmes28-Mar-17 22:31 
AnswerRe: Interpret an Error - Resolved Pin
Mycroft Holmes28-Mar-17 22:51
professionalMycroft Holmes28-Mar-17 22: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.