0

I want to change my content off an AppBar dynamicly whith this code:

<Page.Resources>
    <local:AppBarSelector x:Key="myAppBarSelector"/>
</Page.Resources>

<Page.BottomAppBar>
    <AppBar>
        <ContentControl Content="{Binding SelectedItem, ElementName=listBox}" ContentTemplateSelector="{StaticResource myAppBarSelector}">
            <ContentControl.Resources>
                <DataTemplate x:Key="1">
                    <TextBlock Text="Hallo Welt 1" Foreground="White" />
                </DataTemplate>

                <DataTemplate x:Key="2">
                    <TextBlock Text="Hallo Welt 2" Foreground="White" />
                </DataTemplate>
            </ContentControl.Resources>
        </ContentControl>
    </AppBar>
</Page.BottomAppBar>

And this is my Code behind:

public class AppBarSelector : DataTemplateSelector
{
    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
        Debug.WriteLine((string)item);
        if (item == null) return base.SelectTemplateCore(item, container);

        var contentControl = (ContentControl)container;
        var templateKey = (string)item;

        return (DataTemplate)contentControl.Resources[templateKey];
    }
}

But this method is nerver called.Even the Debug.WriteLine function. Where is my mistake?

Cilenco
  • 6,951
  • 17
  • 72
  • 152
  • are you sure your item is actually a string? it probably is excepting at that point. your item is your binding ItemsSource item. – NSGaga-mostly-inactive Apr 27 '13 at 23:30
  • Yes I fill the ListView with: `listBox.ItemsSource = new List { "1" , "2", "3" , "4"};` I have a second ContentControl which uses an other `Page.Resource` but also with the string of the listView item - that works great but here the function is not called... – Cilenco Apr 28 '13 at 08:34
  • sorry for doubting you :) do you get any errors in the debug view? and again trivial but are you sure that it doesn't 'hit' the SelectTemplateCore (put a breakpoint, not just dumping). – NSGaga-mostly-inactive Apr 28 '13 at 19:05
  • I can't find any error in the Debug View... The Method is called one time after starting the app but then it is never called again :/ But in the second ContentControl.Resources (for the main content - not for the AppBar) I get the error _declared prefix_ but I can compile and run the app. Is this the problem? – Cilenco Apr 29 '13 at 14:54
  • ok, that's different (it enters once - it's 'alive':) - that's familiar. First, you should do proper MVVM, as that's usually the source of issues(not significant in your case but - set `ItemsSource` to bind to a property in VM - which is `ObservableCollection` (not List<>)). Also look at [this](http://stackoverflow.com/questions/2565859/contenttemplateselector-is-only-called-one-time-showing-always-the-same-datatemp). It seems your Binding is not triggering (I have no way of testing this so just giving hints). The ultimate solution is to use MultiBinding and make it 'change'... – NSGaga-mostly-inactive Apr 29 '13 at 15:07
  • actually that should work 'as is' and I don't see anything here problematic - but you should still make it 'ObservableCollection' etc. just to rework, see if that helps (though that shouldn't matter in your case). And lastly `try to make a small 'primer' that repeats the issue` (with all code behind, ListBox XAML etc.) and I can take a look - that'd also help you maybe get to the bottom of it. It's likely something trivial as I said, but then you have to be consistent. Let me know and I can put some answer, but w/ some details from you. – NSGaga-mostly-inactive Apr 29 '13 at 15:33
  • Okay thanks for that I changed from a List to an `ObservableCollection` but that doesn't help. Now I will try to use an other way. Is there an Event which I can bound to my second ContentControl when the content from ContentControl1 is changed? Something like this: ` – Cilenco Apr 29 '13 at 15:36
  • You're not thinking 'MVVM' :) bind to view-model property (both, or similar), and in the setter of that you get when it changes (one is 'two-way' or both). You should get to the bottom of this anyway - that 'error' points to some issues. – NSGaga-mostly-inactive Apr 29 '13 at 15:39

1 Answers1

0

Just after some comments here...
(note: this is a bit general but I can't be more specific w/o some more code to reflect the issues)

This should work 'as is' - I don't see any problems that would produce that (I check with similar example fast and it works well with .ItemsSource = new List<string>{...}.

So that's not the culprit - but it doesn't hurt what I suggested - make a proper MVVM binding to properties, make the list ObservableCollection<> - and also it's always recommended to have a more higher-level objects (instead of just string) as your items (helps in many cases with binding with similar issues - that object implements INotifyPropertyChanged etc. - and you bind to a 'property' there, not the entire object).

The other error suggests some issues as well.

And lastly to bind two contentControls together - you don't normally need events as such. You can use Triggers from the style or XAML directly - but most of the time just bind both to a property in the view-model - and handle the 'change' in your property 'setter'.

You should put up a small primer that repeats this - who knows it might help you realize what you're doing wrong.

NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51