Difference between Window.DataContext and Window.Resources

Markus Freitag 3,786 Reputation points
2020-08-14T06:57:52.563+00:00

Hello,

Can someone tell me what the difference is and when to take what?

17681--23.png

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,326 Reputation points
    2020-08-16T13:49:16.593+00:00

    Hi Markus,
    iit's easy to bind SelectedItem an in Setter call your Processing. Try this demo:

    <Window x:Class="WpfApp1.Window72"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp72"  
            mc:Ignorable="d"  
            Title="Window72" Height="450" Width="800">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
        <Grid>  
        <ComboBox Height="20" Width="100"  
                  ItemsSource="{Binding ComList}"  
                  DisplayMemberPath="CmbValue"  
                  SelectedItem="{Binding SelectedCom}"/>  
      </Grid>  
    </Window>  
    

    And classes:

    17776-x.png

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. DaisyTian-1203 11,626 Reputation points
    2020-08-14T07:24:32.307+00:00

    FrameworkElement.DataContext property gets or sets the data context for an element when it participates in data binding.FrameworkElement.Resources Property gets or sets the locally-defined resource dictionary. They are both can set the datacontext for the FrameworkElement. If you use the Resources to host the data soruce, you need to set the DataContext for the element. In this demo, you can use it like below:

     <Window.Resources>  
            <local:ViewModel x:Key="MyData"></local:ViewModel>  
        </Window.Resources>  
        <Grid>  
            <ComboBox Name="cbStatus"  
                      DataContext ="{StaticResource MyData}"   
                      ItemsSource="{Binding Comlist}"  
                      SelectedItem="{Binding Comlist[4]}"  
                      SelectedValuePath="CmbValue"       
                      DisplayMemberPath="CmbText"     
                      Width="100" Height="30"                 
                      SelectionChanged="cbStatus_SelectionChanged"  
                      />  
            
        </Grid>  
    

  2. Markus Freitag 3,786 Reputation points
    2020-08-14T08:38:47.7+00:00

    OK, I understood the difference.

    1- UserInterface
    2- ViewModel, I use this

     public class RelayCommand : ICommand  
        {  
            private readonly Predicate<object> _canExecute;  
            private readonly Action<object> _action;  
            public RelayCommand(Action<object> action) { _action = action; _canExecute = null; }  
            public RelayCommand(Action<object> action, Predicate<object> canExecute) { _action = action; _canExecute = canExecute; }  
            public void Execute(object o) => _action(o);  
            public bool CanExecute(object o) => _canExecute == null ? true : _canExecute(o);  
            public event EventHandler CanExecuteChanged  
            {  
                add { CommandManager.RequerySuggested += value; }  
                remove { CommandManager.RequerySuggested -= value; }  
            }  
    

    3-Model, here my special combo box
    4 - MainWindow according to my research, there shouldn't be any code.
    17667--mvvm.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.