question

MarkusFreitag-0088 avatar image
0 Votes"
MarkusFreitag-0088 asked MarkusFreitag-0088 commented

Difference between Window.DataContext and Window.Resources

Hello,

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

17681--23.png


windows-wpf
-23.png (52.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PeterFleischer-3316 avatar image
1 Vote"
PeterFleischer-3316 answered MarkusFreitag-0088 commented

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



x.png (68.0 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Peter,
that looks really good and understandable. Thank you very much, I'll test it.

0 Votes 0 ·

If you still want to selected some item when load the ComboBox, you can use SelectedIndex="" to implement it.SelectedIndex="4" and SelectedItem="{Binding Comlist[4]}" have the same effect.

1 Vote 1 ·

Thanks, helpful.

0 Votes 0 ·
DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered DaisyTian-1203 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello,
Thanks for quick response.

Am I seeing this right? See my comment.
And how do I get the selected data into the ViewModel correctly? According to MVVM.



17691-is-global.png


17645-is-special-for-one-control.png17646-how-you-get-the-selected-data-to-viewmodel.png


0 Votes 0 ·

The diference you said is right.If you want to use diferent data collections in the View.Why do not you set them in the same viewmodel? And could you tell me why do you want get the data from View in ViewModel?

0 Votes 0 ·
MarkusFreitag-0088 avatar image
0 Votes"
MarkusFreitag-0088 answered

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



-mvvm.png (178.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.