Note that there are some explanatory texts on larger screens.

plurals
  1. PODataTemplateSelector and its weird behavior
    text
    copied!<p>I am using a <code>DataTemplateSelector</code> inside a <code>ContentControl</code>. I have 3 different <code>DataTemplates</code> based on 3 different <code>object types</code>. When I set the <code>content</code> of my <code>ContentControl</code> to data of the mentioned types, the <code>DataTemplateSelector</code> swaps to the specific <code>DataTemplate</code> AND the selector futhermore seems to rollback/reset the values from the old template. Why is that so?</p> <p><strong>Edit:</strong> I figured out that the values get resetted because I have an attached property caled Prop and inside its OnPropertyChangedCallback it notifies me about the Prop having value null on swapping between DataTemplates. You can see that attached property in code below.</p> <p>Can somebody help me out what happens behind this swapping mechanism of <code>DataTemplateSelector</code>?</p> <p>Here is a deeper explaination with code:</p> <pre><code>public void Window1() { InitalizeComponents(); } public void OnClick(object sender, RoutedEventArgs e) { if(this.DataContext == null) this.DataContext = "Hallo"; else{ if(this.DataContext is string) this.DataContext = 123; else{ if(this.DataContext is int) this.DataContext = null; } } } By clicking on Button few times I change the type and so in ContentControl the selector changes to DataTemplate. </code></pre> <p>The selector looks like this below. It swaps between <code>textDataTemplate</code> and <code>numericDataTemplate</code> and one more template. As I mentioned i have those three type which are <code>string</code>, <code>int</code>, <code>and one more</code>, that i wish not to metion. Their <code>DataTemplates</code> are called <code>textDataTemplate</code>, <code>numericDataTemplate</code> and that one more. :)</p> <pre><code>&lt;local:MyTemplateSelector x:Key="dataTemplateSelector" TextTemplate="{StaticResource textDataTemplate}" NumericTemplate="{StaticResource numericDataTemplate}"/&gt; public class MyTemplateSelector : DataTemplateSelector { public DataTemplate TextTemplate; public DataTemplate NumericTemplate; public DataTemplate Select(object item, Culture.....) { if(item is string) { return this.TextTemplate; } else { return this.NumericTemplate; } } } </code></pre> <p><code>ContentControl</code> and XAML looks like this:</p> <pre><code>&lt;Button Click="OnClick" Content="Click Me"/&gt; &lt;ContentControl Name="contentCtrl" Content="{Binding}" Width="123" ContentTemplateSelector="{StaticResource dataTemplateSelector}" /&gt; </code></pre> <p>And this is how <code>textDataTemplate</code> looks alike. </p> <pre><code>&lt;DataTemplate x:Key="textDataTemplate"&gt; &lt;TextBox x:Name="text" my:AttProperties.Prop="{extension:MarkupExt value}" Text="{Binding Path=Txt, Mode=Default, UpdateSourceTrigger=Explicit}"/&gt; &lt;/DataTemplate&gt; </code></pre> <p><code>numericDataTemplate</code> looks similar to <code>textDataTemplate</code> just that only digits are allowed.</p> <p>The <code>Prop</code> is my <code>attached property</code> from <code>AttProperties class</code> of <code>type string</code>. The <code>Prop</code> is somewhere inside of all three DataTemplate. Above the <code>Prop</code> is sitting on a <code>TextBox</code> but it could be a <code>Label</code> too. The <code>markupextension</code> is just a <strong>"return Hello"</strong>. The extension is just there to test how to create a custom markupextension. There is no big deal with the extension. It shouldnt have to do much with the swapping of <code>DataTemplates</code>.</p> <p>One more time to explain my problem. Swapping seems reselts/rollback my old templates. I swap from textDataTemplate to lets say numericDataTemplate and the Prop of textDataTemplate gets set to null but the value before was "Hello".</p> <p>Why is that happening? It seems like the same behavior with using <code>tiggers</code>. Once a <code>Trigger</code> is no more valid it rollsback the used values. Is a <code>DataTemplateSelector</code> using some kind of same mechanism as <code>Triggers</code>?</p> <p><strong>Edited</strong>: The attached property is just a simple .RegisterAttached with an OnPropertyChangedCallback. Inside OnPropertyChangedCallback I figured the prop is null when swapping the dataTemplates.</p>
 

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