Quantcast
Viewing all articles
Browse latest Browse all 18858

Binding Visible/Collapsed to ContentPresenters within a TabControl.

Hello,

I've been struggling with this WPF issue for a few days, and I'm pretty much at the end of my rope. I'm a WPF newbie...

- I have two DataTemplates declared as Window.Resources.

- I have a TabControl with two ContentPresenter controls within a Grid, within the ContentTemplate/DataTemplate section. If I manually specify eithers Visibility to "Visible" then the DataTemplate bound to that specific presenter appears as it should (assuming the other one is set to "Hidden"). So the DataTemplates themselves, and the general functionality of the ContentPresenters is wired just fine.

- What I need to do is, based on two buttons to the left of the TabControl in my window, is show/hide the ContentPresenters based on which button was clicked. So if a user clicks "Patient" the PatientContentPresenter is visible (along with it's bound DataTemplate), if the user clicks on "Family" then the PatientContentPresenter collapses, and the FamilyContentPresenter becomes visible. So in essance there's 2 possible Views that could appear withing the TabControls tab.

- The issue seems to be the RaisePropertyChanged not having any effect on the the TabControl, and re-drawing the screen with the correct ContentPresenter active. I added debug statements that tell me they are being set correctly in the ViewModel, but the UI is not updating.

If anyone can tell me why the screen isn't updating to show the correct ContentPresenter, I'd be very grateful....here is the code:

XAML:

    <Window.Resources>

       <DataTemplate x:Key="PatientDataTemplate">

           <Views:PatientRecordView x:Name="header" Grid.Row="0" Grid.Column="1" />

       </DataTemplate>

       <DataTemplate x:Key="FamilyDataTemplate">

           <Views:FamilyRecordView x:Name="header" Grid.Row="0" Grid.Column="1" />

       </DataTemplate>

   </Window.Resources>

 

       <TabControl SelectedItem="{Binding SelectedCall}"

                   ItemsSource="{Binding Calls }"

                   SelectionChanged="Selector_OnSelectionChanged"

                   Margin="0"

                   Grid.Row="0" Grid.Column="1" Padding="0" BorderThickness="1,0,1,1" Background="#1d1d1d"

                    Name="MainTabControl">

           <TabControl.ItemTemplate>

               <DataTemplate>

                   <TextBlock Height="20" FontSize="12" Foreground="White" Text="{Binding DisplayName}" />

               </DataTemplate>

           </TabControl.ItemTemplate>

           <TabControl.ContentTemplate>

               <DataTemplate><!-- see above for Windows.Resources for Patient and Family templates -->

                    <Grid x:Name="ContentPresenterGrid">

                       <ContentPresenter x:Name="FamilyContentPresenter" ContentTemplate="{StaticResource FamilyDataTemplate}" Visibility="{Binding ViewFamily}" />

                       <ContentPresenter x:Name="PatientContentPresenter" ContentTemplate="{StaticResource PatientDataTemplate}" Visibility="{Binding ViewPatient}" />

                   </Grid>

               </DataTemplate>       

            </TabControl.ContentTemplate>

        </TabControl>

ViewModel:

publicvoid ViewButtonClick(String buttonClicked, String value)

        {

           Debug.WriteLine("CallContainerViewModel - ViewButtonClick (buttonClicked/value): "+ buttonClicked +"/"+ value);

           //Close all, then open the one we actually want below

            ViewPatient =Visibility.Collapsed;

            ViewFamily =Visibility.Collapsed;

 

           if (buttonClicked.Equals("PatientButtonClick"))

            {

                ViewPatient =Visibility.Visible;

            }

           elseif (buttonClicked.Equals("FamilyButtonClick"))

            {

                ViewFamily =Visibility.Visible;

            }

 //I thought maybe "raising" the ContentPresenters might help...it didn't

            RaisePropertyChanged("PatientContentPresenter");

            RaisePropertyChanged("FamilyContentPresenter");

        }

   

privateVisibility viewPatient;

       publicVisibility ViewPatient

        {

           get {return ViewPatient; }

           set

            {

               Debug.WriteLine("CallContainerViewModel - ViewPatient:"+ value);

                viewPatient =value;

                RaisePropertyChanged("ViewPatient"); //FINALLY, THIS SHOULD REFRESH THE SCREEN WITH THE SELECTED TEMPLATE

            }

        }

 

       privateVisibility viewFamily;

       publicVisibility ViewFamily

        {

           get {return ViewFamily; }

           set

            {

               Debug.WriteLine("CallContainerViewModel - ViewFamily:"+ value);

                viewFamily =value;

                RaisePropertyChanged("ViewFamily");//FINALLY, THIS SHOULD REFRESH THE SCREEN WITH THE SELECTED TEMPLATE

            }

        }

This is the debug statements at run-time, and the values are what I'd expect:

CallContainerViewModel - ViewButtonClick (buttonClicked/value): FamilyButtonClick/Visible

CallContainerViewModel - ViewPatient: Collapsed

CallContainerViewModel - ViewFamily: Collapsed

CallContainerViewModel - ViewFamily: Visible

...so the ContentPresenters get collapased/visible get set properly, or so it seems, and theRaisePropertyChanged is called for both of them, but the tab (and the screen) does not refresh to show the selected ContentPresenter. Strangely enough, when the screen initially loads, the second ContentPresenter inside the Grid is open and showing, even though I did not specify that...but pressing the buttons to make the one I want appear does not work.

If anyone can fix what I'm doing wrong (with the corrected code in their answer), I'd be eternally grateful !

Barry

P.S. I just noticed that the "<DataTemplate>" within the TabControl XAML may be redundant as the value is supposed to be bound to the <DataTemplate> definitions within the Window.Resources section...

P.S.S. If someone wants to suggest an alternate approach, then please include all the required code in the response...but I'd prefer to just make what I already have work :-) thanx again !


Barry O'Neill


Viewing all articles
Browse latest Browse all 18858

Trending Articles