Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically Load Content with WPF
    primarykey
    data
    text
    <p>Ok I have a container that I have created two data templates for. Basically one template will show 5 textboxes with and objects data bound to them and the other template will show a button to add that particular object. I subclassed DataTemplateSelector and it works, but when I navigate through my records the Selector never gets called again.</p> <p>So how would I for the container to reselect it's template. The container is a StackPanel and I have already tried UpdateVisuals, InvalidateVisuals, InvalidateArrange, and ApplyTemplate.</p> <p>XAML Code</p> <pre><code>&lt;DataTemplate x:Key="advisorTemplate"&gt; &lt;StackPanel Orientation="Vertical" Margin="2,2,2,2" HorizontalAlignment="Stretch" VerticalAlignment="Top"&gt; &lt;StackPanel Orientation="Horizontal" Margin="2,2,2,2" HorizontalAlignment="Center"&gt; &lt;extToolkit:WatermarkTextBox Name="txtAcadAdv" Watermark="Acad Adv" Width="125" Margin="2" Text="{Binding Path=Adv.AcadAdv}"/&gt; &lt;extToolkit:WatermarkTextBox Name="txtProgAdv" Watermark="Prog Adv" Width="125" Margin="2" Text="{Binding Path=Adv.ProgAdv}"/&gt; &lt;/StackPanel&gt; &lt;StackPanel Orientation="Horizontal" Margin="2,2,2,2" HorizontalAlignment="Center"&gt; &lt;extToolkit:WatermarkTextBox Name="txtPortAdv" Watermark="Port Adv" Width="125" Margin="2" Text="{Binding Path=Adv.PortAdv}"/&gt; &lt;extToolkit:WatermarkTextBox Name="txtEleTws" Watermark="Ele Tws" Width="125" Margin="2" Text="{Binding Path=Adv.EleTws}"/&gt; &lt;/StackPanel&gt; &lt;StackPanel Orientation="Horizontal" Margin="2,2,2,2" HorizontalAlignment="Center"&gt; &lt;extToolkit:WatermarkTextBox Name="txtMatTws" Watermark="Mat Tws" Width="125" Margin="2" Text="{Binding Path=Adv.MatTws}"/&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="addAdvisor"&gt; &lt;Button HorizontalAlignment="Center" VerticalAlignment="Center" Margin="2" Name="btnAddAdvisor" Click="ButtonClick" Content="Add Advisor"/&gt; &lt;/DataTemplate&gt; </code></pre> <p>Initialization of the Content Changed on the Group Box</p> <pre><code>grpAdv.ContentTemplateSelector = _advisorSelector; </code></pre> <p>And Finally the Selector Code</p> <pre><code>private readonly StudentWin _win; public AdvisorDataTemplateSelector(StudentWin win) { _win = win; } public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container) { var sp = item as StackPanel; var adv = sp.DataContext as Advisor; if (adv == null) return _win.FindResource("addAdvisor") as DataTemplate; return _win.FindResource("advisorTemplate") as DataTemplate; } </code></pre> <p>And Here is a snippet of my navigation code</p> <pre><code>case "btnNext": { if(_view.CurrentPosition &lt; _view.Count - 1) { CheckForUnusedReferences(_view.GetItemAt(_view.CurrentPosition) as Student); _view.MoveCurrentToNext(); CheckForNullReferences(_view.CurrentPosition); grpAdv.ApplyTemplate(); } } </code></pre> <p>The two additional Methods are to check if a relationship is null on the student and they will create it and add it to the data context for me or else Entity Framework won't save the changes. The data templates above will basically help me with a problem of not having the studentId when I try to create a new student.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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