开发者

DataTrigger not reevaluating after property changes

开发者 https://www.devze.com 2022-12-22 14:16 出处:网络
[Original] I have a ListBox which has its ItemsSource (this is done in the code behind on as the window is created) databound to an ObservableCollection. The ListBox then has the following DataTemplat

[Original]

I have a ListBox which has its ItemsSource (this is done in the code behind on as the window is created) databound to an ObservableCollection. The ListBox then has the following DataTemplate assigned against the items:

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 the ObservableCollection is updated with an object, this appears in the ListBox with the correct initial DataTemplate, however when the hasBeenAnswered property is set to true (when debugging i can see the collection is correct) the DataTrigger does not re-evaluate and then update the ListBox to use the correct DataTemplate.

I have implemented the INotifyPropertyChanged Event in my object, and if in the template is bound to a value, i can see the value update. Its just that the DataTrigger will not re-evaluate and change to the correct template.

I know the DataTrigger binding is correct because if i close the window and open it again, it will correctly apply the second datatemplate, because the hasBeenAnswered is set to true.

[edit 1]

Following on from comments made by Timores I've tried the following:

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>
开发者_Go百科

What happens now is similar to the first example, when a call comes in the "Not answered" label shows (one per call that exists as this is a listbox - normally when the window loads there will be no calls), the call is then answered and the proptery hasBeenAnswered is set to true, yet the "Not Answered" remains the same. If i close the window, and re-open it again (with the active call still with the property hasBeenAnswered set to true) the background is then blue. So it would appear to me like the datatrigger is simply not being run, until the window is re-run.


What seems strange to me in the example is that you are using an ItemContainerStyle instead of an ItemTemplate.

ItemContainerStyle applies to the ListBoxItem that contains each element in your ItemsSource. The ListboxItem does not have an hasBeenAnswered property, so I don't see how the binding could work.

I suggest creating a DataTemplate for the data type in your list box and using triggers to make the same changes as in your templateAnswered style.

Edit: after OP used the suggestion of the ItemTemplate.

I tried to reproduce the example, and it works fine for me. Here is my XAML (please disregard style, this is just an example):

Not answered

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

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

And in the code-behind:

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;
}

Could you try to reproduce this and compare with your code ? Note: the smallest error in the property name given to PropertyChanged could explain your behaviour. The trigger could be based on the right property, but the notification could have a misspelled name.

0

精彩评论

暂无评论...
验证码 换一张
取 消