Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I override or alter the inheritance of the child elements when my datacontext is set on the parent element?
    primarykey
    data
    text
    <p><strong>Disclaimer</strong> I feel like this is a fairly simple question, so i must reiterate that I did look for an answer and couldn't find anything!</p> <p>Not sure if I am asking the question correctly, but I will tell you this. I am working on becoming more familiar with MVVM, so I am messing around with a ridiculously simple project of two stackpanels, ten textboxes, and some simple binding. Everything works now, since I have two panels, which separates my boxes and lets me set two datacontext. </p> <p>My question is this: </p> <p>a) is it possible to set the datacontext on a parent element (Stackpanel) and have half of my child elements (textboxes) use that context via inheritance and then give the other half of the elements A DIFFERENT data context? </p> <p>and </p> <p>b) if this is possible, how??</p> <p>Thanks people-smarter-than-I</p> <p>Here is the code that is trying so hard, but not really doing anything I want it to be doing:</p> <p>XAML</p> <pre><code> &lt;Grid&gt; &lt;StackPanel&gt; &lt;StackPanel HorizontalAlignment="Left" Margin="8,8,0,75" Width="224" DataContext="{Binding Path=Customer}"&gt; &lt;TextBox Text="{Binding Path=FirstName}" Height="28" Name="label1"/&gt; &lt;TextBox Text="{Binding Path=MiddleName}" Height="28" Name="l2"/&gt; &lt;TextBox Text="{Binding Path=LastName}" Height="28" Name="l3"/&gt; &lt;TextBox Text="{Binding Path=CompanyName}" Height="28" Name="l4"/&gt; &lt;TextBox Text="{Binding Path=EmailAddress}" Height="28" Name="l5"/&gt; &lt;!--I want the next five TextBox elements to bind to a different datacontext--&gt; &lt;TextBox Text="{Binding Path=FirstName}" Height="28" Name="label11"/&gt; &lt;TextBox Text="{Binding Path=MiddleName}" Height="28" Name="l21"/&gt; &lt;TextBox Text="{Binding Path=LastName}" Height="28" Name="l1lgh3"/&gt; &lt;TextBox Text="{Binding Path=CompanyName}" Height="28" Name="l1hj4"/&gt; &lt;TextBox Text="{Binding Path=EmailAddress}" Height="28"/&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; </code></pre> <p>Code Behind C#</p> <pre><code> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { this.DataContext = new MainViewModel(); } } </code></pre> <p>View Model</p> <pre><code> public class MainViewModel : INotifyPropertyChanged { public MainViewModel() { PopulateCustomerInfo(); } private Customer customer; public Customer Customer { get { return customer; } set { customer = value; OnPropertyChanged("Customer"); } } private Customer customer2; public Customer Customer2 { get { return customer2; } set { customer2 = value; OnPropertyChanged("Customer"); } } private void PopulateCustomerInfo() { AdventureWorksLTE ctx = new AdventureWorksLTE(); this.Customer = (from c in ctx.Customers select c).FirstOrDefault(); this.Customer2 = (from c in ctx.Customers orderby c.FirstName descending select c).FirstOrDefault(); } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { var handle = PropertyChanged; if (handle != null) { var e = new PropertyChangedEventArgs(propertyName); handle(this, e); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload