DataTrigger does not overestimate after changing properties

[Original]
I have ListBoxone that has it ItemsSource(this is done in the code behind when creating the window), data binding to ObservableCollection. ListBoxhas the following DataTemplateassigned against elements:

usercontrol.xaml

<ListBox x:Name="communicatorListPhoneControls"
         ItemContainerStyle="{StaticResource templateForCalls}"/>

app.xaml

<Style x:Key="templateForCalls" TargetType="{x:Type ListBoxItem}">  
    <Setter Property="ContentTemplate" Value="{StaticResource templateRinging}"/>  
        <Style.Triggers>  
            <DataTrigger Binding="{Binding Path=hasBeenAnswered}" Value="True">  
                <Setter Property="ContentTemplate" Value="{StaticResource templateAnswered}"/>  
            </DataTrigger>  
        </Style.Triggers>  
    </Setter>
</Style>

When an object is ObservableCollectionupdated by the object, it appears in ListBoxwith the correct start DataTemplate, however, when the property is hasBeenAnsweredset to true(when debugging, I see the collection correctly) it DataTriggerdoes not overestimate, and then updates ListBoxto use the correct one DataTemplate.

INotifyPropertyChanged , , . , DataTrigger .

, DataTrigger , , , , hasBeenAnswered true.

[edit 1]
, :

usercontrol.xaml

<ListBox x:Name="communicatorListPhoneControls"
         ItemTemplate="{StaticResource communicatorCallTemplate}"/>`  

app.xaml:

<DataTemplate x:Key="communicatorCallTemplate">
    <Label x:Name="test">Not answered</Label>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=hasBeenAnswered}" Value="True">
                <Setter TargetName="test" Property="Background" Value="Blue"/>
            </DataTrigger>    
        </DataTemplate.Triggers>
    </Label>
</DataTemplate>

, , , " " ( , , - , , ), , proptery hasBeenAnswered true, "Not Answered" . ( hasBeenAnswered true), . , datatrigger , .

+5
1

, ItemContainerStyle ItemTemplate.

ItemContainerStyle ListBoxItem, ItemsSource. ListboxItem hasBeenAnswered, , .

DataTemplate , templateAnswered.

: , OP ItemTemplate.

, . XAML (, , ):

                                                                                                        

    <ListBox x:Name="communicatorListPhoneControls" 
             ItemTemplate="{StaticResource communicatorCallTemplate}"/>

    <Button Margin="0,20,0,0" Click="OnToggleAnswer" Content="Toggle answer status" />
</StackPanel>

:

public partial class Window1 : Window {

    public Window1() {
        InitializeComponent();

        List<PhoneCall> lpc = new List<PhoneCall>()
        {new PhoneCall(), new PhoneCall(), new PhoneCall(), new PhoneCall()};

        communicatorListPhoneControls.ItemsSource = lpc;
    }

    private void OnToggleAnswer(object sender, RoutedEventArgs e) {

        object o = communicatorListPhoneControls.SelectedItem;

        if (o != null) {

            PhoneCall pc = (PhoneCall) o;
            pc.hasBeenAnswered = ! pc.hasBeenAnswered;
        }
    }
}

public class PhoneCall : INotifyPropertyChanged {

    private bool _answered;


    public bool hasBeenAnswered {
        get { return _answered;  }
        set {
            if (_answered != value) {
                _answered = value;
                FirePropertyChanged("hasBeenAnswered");
            }
        }
    }

    private void FirePropertyChanged(string propName) {

        if (PropertyChanged != null) {

            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

? . , PropertyChanged, . , .

+1

All Articles