Click here to Skip to main content
15,881,248 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: Icon Strangeness Pin
Gerry Schmitz3-Feb-21 7:25
mveGerry Schmitz3-Feb-21 7:25 
QuestionDispatcherTimer Pin
michaelbarb14-Jan-21 6:29
michaelbarb14-Jan-21 6:29 
AnswerRe: DispatcherTimer Pin
Mycroft Holmes14-Jan-21 11:06
professionalMycroft Holmes14-Jan-21 11:06 
GeneralRe: DispatcherTimer Pin
michaelbarb14-Jan-21 11:52
michaelbarb14-Jan-21 11:52 
GeneralRe: DispatcherTimer Pin
Mycroft Holmes14-Jan-21 12:15
professionalMycroft Holmes14-Jan-21 12:15 
GeneralRe: DispatcherTimer Pin
michaelbarb14-Jan-21 13:23
michaelbarb14-Jan-21 13:23 
AnswerRe: DispatcherTimer Pin
Gerry Schmitz15-Jan-21 2:31
mveGerry Schmitz15-Jan-21 2:31 
QuestionBetter way to access ViewModels Pin
Mc_Topaz5-Jan-21 4:11
Mc_Topaz5-Jan-21 4:11 
In my current way to structure a WPF-application I heavily lean on UserControls for my Views and place them in the MainWindow inside a container, usually a DockPanel. For instance in the MainWindow:
XML
<DockPanel LastChildFill="True">
        <views:Header DockPanel.Dock="Top" />
        <views:Footer DockPanel.Dock="Bottom" />
        <views:Body />
</DockPanel>
For each View I have a corresponding ViewModel. This ViewModel is instantiated inside the View's XAMl-code. In the DataContext element. For instance in the Body View:
XML
<UserControl.DataContext>
    <vm:BodyViewModel />
</UserControl.DataContext>
I like this approach, because I can alter any properties in the ViewModel and see the View automatically change in the view during design time.


But I have not find a good way to access any ViewModels from some class where I happened to need to alter the View through a property in a ViewModel.

My current solution - which I don't like - to gain access of the ViewModels is:

1. Give each View a name in the MainWindow.xaml file.
XML
<DockPanel LastChildFill="True">
    <views:Header DockPanel.Dock="Top" x:Name="viewHeader" />
    <views:Footer DockPanel.Dock="Bottom" x:Name="viewFooter" />
    <views:Body x:Name="viewBody" />
</DockPanel>
2. Do some logic in the MainWindow to setup the ViewModels for each View.
C#
public partial class MainWindow : Window
{
    HeaderViewModel Header { get; set; }
    FooterViewModel Footer { get; set; }
    BodyViewModel Body { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        Header = viewHeader.DataContext as HeaderViewModel;
        Footer = viewFooter.DataContext as FooterViewModel;
        Body = viewBody.DataContext as BodyViewModel;
    }
}
3. Then for every class where I need a ViewModel I need to pass it from the MainWindow in some way.
C#
class SomeClass
{
    public void AlterHeaderText(HeaderViewModel header)
    {
        // Do some logic with viewmodel.
    }
}
This approach works, but I don't like it. It's annoying to pass ViewModels from the MainWindow every time a class need one or more ViewModels to alter a View.

Is there a solution to this approach to improve it?


/BR
Steffe
AnswerRe: Better way to access ViewModels Pin
Gerry Schmitz5-Jan-21 21:01
mveGerry Schmitz5-Jan-21 21:01 
AnswerRe: Better way to access ViewModels Pin
#realJSOP9-Jan-21 1:03
mve#realJSOP9-Jan-21 1:03 
GeneralRe: Better way to access ViewModels Pin
michaelbarb10-Jan-21 8:20
michaelbarb10-Jan-21 8:20 
GeneralRe: Better way to access ViewModels Pin
#realJSOP10-Jan-21 11:15
mve#realJSOP10-Jan-21 11:15 
AnswerRe: Better way to access ViewModels Pin
#realJSOP21-Feb-21 0:42
mve#realJSOP21-Feb-21 0:42 
QuestionIDataErrorInfo Pin
#realJSOP28-Dec-20 4:15
mve#realJSOP28-Dec-20 4:15 
AnswerRe: IDataErrorInfo Pin
Gerry Schmitz28-Dec-20 6:24
mveGerry Schmitz28-Dec-20 6:24 
GeneralRe: IDataErrorInfo Pin
#realJSOP28-Dec-20 8:13
mve#realJSOP28-Dec-20 8:13 
GeneralRe: IDataErrorInfo Pin
#realJSOP29-Dec-20 1:56
mve#realJSOP29-Dec-20 1:56 
QuestionDesktop Push Notifications on multiple devices in WPF Desktop Application Pin
Member 1495324013-Dec-20 4:47
Member 1495324013-Dec-20 4:47 
AnswerRe: Desktop Push Notifications on multiple devices in WPF Desktop Application Pin
Gerry Schmitz13-Dec-20 5:32
mveGerry Schmitz13-Dec-20 5:32 
GeneralRe: Desktop Push Notifications on multiple devices in WPF Desktop Application Pin
Member 1495324013-Dec-20 7:09
Member 1495324013-Dec-20 7:09 
GeneralRe: Desktop Push Notifications on multiple devices in WPF Desktop Application Pin
Gerry Schmitz13-Dec-20 19:18
mveGerry Schmitz13-Dec-20 19:18 
AnswerRe: Desktop Push Notifications on multiple devices in WPF Desktop Application Pin
Dave Kreskowiak13-Dec-20 10:20
mveDave Kreskowiak13-Dec-20 10:20 
QuestionDispatcherTimer Pin
michaelbarb8-Dec-20 3:55
michaelbarb8-Dec-20 3:55 
AnswerRe: DispatcherTimer Pin
michaelbarb8-Dec-20 4:12
michaelbarb8-Dec-20 4:12 
AnswerRe: DispatcherTimer Pin
Dave Kreskowiak8-Dec-20 4:15
mveDave Kreskowiak8-Dec-20 4:15 

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.