locked
DataTemplate, Triggers, and Binding (sryan) - 2/21/2008 7:53 AM PST RRS feed

  • Question

  • By: sryan


    I have a gridview which uses two datatemplates. One shows a check that tells 
    me if a user has a password and the other is a button that is supposed to 
    clear that password out of the database. When the application first runs, 
    everything looks great. Users that have passwords have a check next to them, 
    those that don't have nothing. When I click my Clear Password Button the 
    password gets deleted from the database (after hitting save of course). My 
    problem is that my UI doesn't refresh.
    
    When I click my Clear Password button I would expect my datatemplate for my 
    check to se that there's not a password any more (because it's bound) 
    andtherefore get rid of the check. However, the check stays onscreen. The 
    check doesn't go away until the entire area is saved. I want it to happen 
    immediately. I'm currently using binding , UpdateSourceTrigger and 
    NotifyPropertyChanged. What else could I be missing?
    
    These are my datatemplates:
    <DataTemplate x:Key="PasswordCell">	
    		<Canvas Visibility="Visible" Margin="2,-2,-2,2" x:Name="canvas">
    			<Path Fill="{x:Null}" Stretch="Fill" Stroke="{DynamicResource BrushDark}" 
    StrokeThickness="2" Width="10" Height="15"  Data="M0,13 L12.835282,1.4144527" 
    Canvas.Left="4" Canvas.Top="-2" />
    			<Path Fill="{x:Null}" Stretch="Fill" Stroke="{DynamicResource BrushDark}" 
    StrokeThickness="2" Width="7" Height="7" Data="M0,0.36238045 L11.151966,13" 
    Canvas.Top="5.79" />
    		</Canvas>
    		<DataTemplate.Triggers>
    			<DataTrigger Binding="{Binding Path=ProfileSectionPassword, 
    UpdateSourceTrigger=PropertyChanged}" Value="">
    				<Setter Property="Visibility" TargetName="canvas" Value="Collapsed" />
    			</DataTrigger>
    		</DataTemplate.Triggers>
    	</DataTemplate>
    
    <DataTemplate x:Key="PasswordClearCell">
    		<Button IsEnabled="{Binding Path=EnablePasswordClear}"  
    Click="OnClearSectionPassword" Content="Clear Password" />
    	</DataTemplate>
    
    Here's the Properties they're Binding to:
    private Visibility sectionHasPassword;
    public Visibility SectionHasPassword
            {
                get { return sectionHasPassword; }
                set { sectionHasPassword = value; 
    NotifyPropertyChanged("SectionHasPassword"); }
            }
    private string profileSectionPassword = "";
    public string ProfileSectionPassword
            {
                get {if (profileSectionPassword == null) profileSectionPassword 
    = "";
                    return profileSectionPassword; 
                }
                set { profileSectionPassword = value; 
    NotifyPropertyChanged("ProfileSectionPassword"); }
            }
    
    Here's the rest of the code:
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
    
    private void OnClearSectionPassword(object sender, RoutedEventArgs e)
            {
                Button temp = (Button)sender;
                
    ((ProfileExpanderLineItem)(((ContentPresenter)(temp.TemplatedParent)).Content)).ProfileSectionPassword = "";
            }
    
    Anybody have any ideas? I'd appreciate it.
    Friday, February 22, 2008 7:07 PM