Click here to Skip to main content
15,888,351 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: ComboBox Restore Saved Item Pin
Afzaal Ahmad Zeeshan20-Jan-17 11:20
professionalAfzaal Ahmad Zeeshan20-Jan-17 11:20 
AnswerRe: ComboBox Restore Saved Item Pin
Mycroft Holmes20-Jan-17 11:54
professionalMycroft Holmes20-Jan-17 11:54 
GeneralRe: ComboBox Restore Saved Item Pin
Kevin Marois20-Jan-17 11:58
professionalKevin Marois20-Jan-17 11:58 
GeneralRe: ComboBox Restore Saved Item Pin
Mycroft Holmes20-Jan-17 14:14
professionalMycroft Holmes20-Jan-17 14:14 
QuestionEnumDescriptionConverter Exception Pin
Kevin Marois20-Jan-17 9:55
professionalKevin Marois20-Jan-17 9:55 
AnswerRe: EnumDescriptionConverter Exception Pin
Afzaal Ahmad Zeeshan20-Jan-17 11:38
professionalAfzaal Ahmad Zeeshan20-Jan-17 11:38 
GeneralRe: EnumDescriptionConverter Exception Pin
Kevin Marois20-Jan-17 11:38
professionalKevin Marois20-Jan-17 11:38 
Questionc# WPF MVVM accesing a view with from another view Pin
Member 1288059514-Jan-17 21:56
Member 1288059514-Jan-17 21:56 
Hello,
I am trying to make an app to schedule real life tasks. My main purpose is to learn how to switch between views with commands. Switching between views has partially worked.
My "app" has a main window, with a few buttons(like a horizontal menu bar), and a content control area below the menu buttons, where i display views associated with the buttons.
I managed somehow to make the buttons to switch to my views.
My first 2 views are the "HomeView" and "AddEditView".
In my HomeView i have a listview that is displaying the tasks from my database. On my listview i have attached a contextmenu with 3 options/buttons: Add Task, Edit Task, Remove Task.
I want o make the contextmenu options/buttons to take me to the AddEditView so i can Add/Edit my selected task.
I have tried to bind the contextmenu buttons to the main window buttons and commands, but most likely i did something wrong. I have also tried to link my HomeViewModel with the MainWindowViewModel , but when i am newing up a MainWindowViewModel object in my HomeViewModel i get the following error
C#
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

pointing to
C#
public TaskLINQToSQLClassesDataContext() : 
				base(global::DTS.Properties.Settings.Default.TasksConnectionString, mappingSource)

What should i do? Is there other piece of code i should post?
Thanks in advance!


MainWindowViewModel:
C#
public class MainWindowViewModel : INotifyPropertyChanged
{
    private HomeViewModel homeVM;
    private AddEditViewModel addEditVM;
    private NotesViewModel notesVM;
    private StatsViewModel statsVM;

    private object currentView;

    public SwitchViewCommand SwitchToHomeViewCommand { get; private set; }
    public SwitchViewCommand SwitchToAddEditViewCommand { get; private set; }
    public SwitchViewCommand SwitchToNotesViewCommand { get; private set; }
    public SwitchViewCommand SwitchToStatsViewCommand { get; private set; }

    public MainWindowViewModel()
    {
        homeVM = new HomeViewModel();
        addEditVM = new AddEditViewModel();
        notesVM = new NotesViewModel();
        statsVM = new StatsViewModel();

        SwitchToHomeViewCommand = new SwitchViewCommand(DisplayHomeView);
        SwitchToAddEditViewCommand = new SwitchViewCommand(DisplayAddEditView);
        SwitchToNotesViewCommand = new SwitchViewCommand(DisplayNotesView);
        SwitchToStatsViewCommand = new SwitchViewCommand(DisplayStatsView);

        CurrentView = homeVM;
    }

    public object CurrentView
    {
        get { return currentView; }
        set { currentView = value; RaisePropertyChanged(); }
    }

    public void DisplayHomeView()
    {
        CurrentView = homeVM;
    }

    public void DisplayAddEditView()
    {
        CurrentView = addEditVM;
    }

    public void DisplayNotesView()
    {
        CurrentView = notesVM;
    }

    public void DisplayStatsView()
    {
        CurrentView = statsVM;
    }



    #region INPC
    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
    {
        var hndlr = PropertyChanged;
        if (hndlr != null)
        {
            hndlr(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion

}

HomeViewModel:
C#
public class HomeViewModel : BaseViewModel
{
    private Models.Task selectedTask;
    private string isCompleted;
    private ObservableCollection<Models.Task> taskList;

    public HomeViewModel()
    {
        TaskList = new ObservableCollection<Models.Task>(tDC.Tasks);
        MwVm = new MainWindowViewModel();
    }

    public MainWindowViewModel MwVm { get; set; }

    public Models.Task SelectedTask
    {
        get { return selectedTask; }
        set
        {
            if (selectedTask!=value)
            {
                selectedTask = value;
                RaisePropertyChanged();
                IsCompleted = selectedTask.Completed == true ? "Completed" : "Not Completed";
            }
        }
    }

    public string IsCompleted
    {
        get { return isCompleted; }
        set
        {
            isCompleted = value;
            RaisePropertyChanged();
        }
    }

    public ObservableCollection<Models.Task> TaskList
    {
        get { return taskList; }
        set
        {
            if (taskList != value)
            {
                taskList = value;
                RaisePropertyChanged();
            }
        }
    }

    public void ViewChange()
    {
        MwVm.SwitchToAddEditViewCommand.Execute(this);
    }
}


MainWindowView buttin bindings and conent control:
XML
<StackPanel Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="8" Grid.RowSpan="1" Orientation="Horizontal" HorizontalAlignment="Center" >
            <Button x:Name="btnHome" Style="{StaticResource MenuButton}" Content="Home" Grid.Column="1" Grid.ColumnSpan="2" Width="73" Command="{Binding SwitchToHomeViewCommand}">
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="DodgerBlue" Offset="0.777"/>
                        <GradientStop Color="#FF9BCF31" Offset="1"/>
                    </LinearGradientBrush>
                </Button.Background>
            </Button>
            <Button x:Name="btnAddEdit" Style="{StaticResource MenuButton}" Content="Add/Edit" Grid.Column="2" Grid.ColumnSpan="2" Width="73" Command="{Binding SwitchToAddEditViewCommand}">
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="DodgerBlue" Offset="0.777"/>
                        <GradientStop Color="#FF9BCF31" Offset="1"/>
                    </LinearGradientBrush>
                </Button.Background>
            </Button>
            <Button x:Name="btnNotes" Style="{StaticResource MenuButton}" Content="Notes" Grid.Column="4" Grid.ColumnSpan="2" Width="73" Command="{Binding SwitchToNotesViewCommand}">
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="DodgerBlue" Offset="0.777"/>
                        <GradientStop Color="#FF9BCF31" Offset="1"/>
                    </LinearGradientBrush>
                </Button.Background>
            </Button>
            <Button x:Name="btnSettings" Style="{StaticResource MenuButton}" Content="Settings" Grid.Column="6" Grid.ColumnSpan="2" Width="73" Command="{Binding SwitchToStatsViewCommand}">
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="DodgerBlue" Offset="0.777"/>
                        <GradientStop Color="#FF9BCF31" Offset="1"/>
                    </LinearGradientBrush>
                </Button.Background>
            </Button>
        </StackPanel>

        <ContentControl Grid.Column="0" Grid.ColumnSpan="8" Grid.Row="2" Grid.RowSpan="6" Content="{Binding CurrentView}" HorizontalAlignment="Center" VerticalAlignment="Stretch"/>


HomeView :
XML
<UserControl x:Class="DTS.Views.HomeView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:DTS.Views"
             mc:Ignorable="d" d:DesignWidth="400" Height="220">
    <Grid Margin="0,0,0,-6">
        <TextBlock Style="{StaticResource ListviewTitleTxtBlock}" Text="Task List" Margin="10,10,184,196" />
        <ListView x:Name="LstVviewTaskList" Height="181" Width="120" Margin="10,35,270,0" VerticalAlignment="Top" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" ItemsSource="{Binding TaskList}" SelectedItem="{Binding SelectedTask,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
            <ListView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Add Task" Background="AliceBlue" Command="{Binding HomeViewModel.SwitchViewAddEditCmd}"/>
                    <MenuItem Header="Edit Task" Background="AliceBlue" Command="{Binding HomeViewModel.SwitchViewAddEditCmd}"/>
                    <MenuItem Header="Remove Task" Background="AliceBlue" Command="{Binding HomeViewModel.SwitchViewAddEditCmd}"/>
                </ContextMenu>
            </ListView.ContextMenu>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=TaskTitle}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <TextBox Height="20" Margin="140,35,10,171" IsReadOnly="True" Text="{Binding ElementName=LstVviewTaskList, Path=SelectedItem.TaskTitle}"/>
        <TextBox Margin="140,61,10,80" IsReadOnly="True" Text="{Binding ElementName=LstVviewTaskList, Path=SelectedItem.TaskDescripion}" />
        <Button Content="Mark as completed" FontSize="10" Margin="140,172,13,31" Command="{Binding SwitchViewAddEditCmd}"/>
        <TextBlock FontSize="12" Foreground="Black" TextAlignment="Center" Text="{Binding IsCompleted, FallbackValue='Task Completion'}" Margin="140,200,13,10"  />
        <TextBlock FontSize="12" TextAlignment="Center" Text="{Binding ElementName=LstVviewTaskList, Path=SelectedItem.TaskDate, FallbackValue='Task Date'}"  Margin="143,151,10,59"  />

    </Grid>
</UserControl>

AnswerRe: c# WPF MVVM accesing a view with from another view Pin
Pete O'Hanlon15-Jan-17 21:46
mvePete O'Hanlon15-Jan-17 21:46 
GeneralRe: c# WPF MVVM accesing a view with from another view Pin
Member 1288059516-Jan-17 8:05
Member 1288059516-Jan-17 8:05 
AnswerRe: c# WPF MVVM accesing a view with from another view Pin
Richard Deeming16-Jan-17 2:26
mveRichard Deeming16-Jan-17 2:26 
GeneralRe: c# WPF MVVM accesing a view with from another view Pin
Member 1288059516-Jan-17 8:31
Member 1288059516-Jan-17 8:31 
GeneralRe: c# WPF MVVM accesing a view with from another view Pin
Richard Deeming16-Jan-17 9:12
mveRichard Deeming16-Jan-17 9:12 
GeneralRe: c# WPF MVVM accesing a view with from another view Pin
Member 1288059517-Jan-17 8:05
Member 1288059517-Jan-17 8:05 
GeneralRe: c# WPF MVVM accesing a view with from another view Pin
Richard Deeming17-Jan-17 8:40
mveRichard Deeming17-Jan-17 8:40 
GeneralRe: c# WPF MVVM accesing a view with from another view Pin
Member 1288059517-Jan-17 8:52
Member 1288059517-Jan-17 8:52 
QuestionBind To Static Property Pin
Kevin Marois13-Jan-17 5:32
professionalKevin Marois13-Jan-17 5:32 
AnswerRe: Bind To Static Property Pin
Richard Deeming13-Jan-17 5:55
mveRichard Deeming13-Jan-17 5:55 
GeneralRe: Bind To Static Property Pin
Kevin Marois13-Jan-17 6:01
professionalKevin Marois13-Jan-17 6:01 
GeneralRe: Bind To Static Property Pin
Richard Deeming13-Jan-17 7:49
mveRichard Deeming13-Jan-17 7:49 
GeneralRe: Bind To Static Property Pin
Kevin Marois13-Jan-17 7:56
professionalKevin Marois13-Jan-17 7:56 
GeneralRe: Bind To Static Property Pin
Richard Deeming13-Jan-17 8:18
mveRichard Deeming13-Jan-17 8:18 
GeneralRe: Bind To Static Property Pin
Kevin Marois13-Jan-17 8:23
professionalKevin Marois13-Jan-17 8:23 
GeneralRe: Bind To Static Property Pin
Richard Deeming13-Jan-17 8:24
mveRichard Deeming13-Jan-17 8:24 
GeneralRe: Bind To Static Property Pin
Kevin Marois13-Jan-17 8:25
professionalKevin Marois13-Jan-17 8:25 

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.