简体   繁体   中英

WPF Binding Property not refreshing on update

I've got a problem refreshing the binding to a property in my object. at initiating it shows fine result, but after i edit the source the value isn't refreshed in my window.

public class Marker : INotifyPropertyChanged 
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, e);
        }
    }
    private ObservableCollection<bool?> _repeat;
    [...]
    public ObservableCollection<bool?>    Repeat
    {
        get { return _repeat; }
        set 
        {
            _repeat = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Repeat"));
            OnPropertyChanged(new PropertyChangedEventArgs("RepeatString"));
        }
    }

    [...]

    [XmlIgnore]
    public string RepeatString
    {
        get
        {
            string wt = string.Empty;
            wt += (bool)Repeat[0] ? "Mo, " : string.Empty;
            wt += (bool)Repeat[1] ? "Di, " : string.Empty;
            wt += (bool)Repeat[2] ? "Mi, " : string.Empty;
            wt += (bool)Repeat[3] ? "Do, " : string.Empty;
            wt += (bool)Repeat[4] ? "Fr, " : string.Empty;
            wt += (bool)Repeat[5] ? "Sa, " : string.Empty;
            wt += (bool)Repeat[6] ? "So, " : string.Empty;
            if (wt != string.Empty)
            {
                wt = wt.Remove(wt.Length - 2);
            }
            return wt;
        }
        [...]
    }
    [...]
}

In XAML part I've set bindings as following:

<Grid DataContext="{Binding ElementName=lvMarker, Path=SelectedItem}">
    [...]
        <CheckBox Content="Montag" IsChecked="{Binding Repeat[0], Mode=TwoWay}"/>
        <CheckBox Content="Dienstag" IsChecked="{Binding Repeat[1], Mode=TwoWay}"/>
        <CheckBox Content="Mittwoch" IsChecked="{Binding Repeat[2], Mode=TwoWay}"/>
        <CheckBox Content="Donnerstag" IsChecked="{Binding Repeat[3], Mode=TwoWay}"/>
        <CheckBox Content="Freitag" IsChecked="{Binding Repeat[4], Mode=TwoWay}"/>
        <CheckBox Content="Samstag" IsChecked="{Binding Repeat[5], Mode=TwoWay}"/>
        <CheckBox Content="Sonntag" IsChecked="{Binding Repeat[6], Mode=TwoWay}"/>
    </StackPanel>
</Grid>

this is updating the array just fine as I'm Clicking the Checkboxes. but not the following text block binding.

<ListView x:Name="lvMarker" ItemsSource="{Binding MarkerCollection}" Width="Auto" ItemContainerStyle="{StaticResource lvItemStyle}">
    <ListView.View>
        <GridView>
            [...]
            <GridViewColumn Header="Zeit" Width="Auto" CellTemplate="{StaticResource DateCell}"/>
        </GridView>
    </ListView.View>
</ListView>

...

<DataTemplate x:Key="DateCell">
    <StackPanel>
        <TextBlock FontSize="14">
            [...]
        </TextBlock>
        <TextBlock FontSize="10" Text="{Binding RepeatString, Mode=OneWay}"/>
    </StackPanel>
</DataTemplate>

What would I have to do to refresh the TextBlock representing RepeatString, when I change the bool? of Repeat[] ?

You're raising RepeatString whenever the property of Repeat changes, but you want to subscribe to the changes of collection items.

Subscribe to collection changes of Repeat in your constructor (or wherever you instantiate _repeat ).

public Marker()
{
    _repeat.CollectionChanged += Repeat_CollectionChanged;
}

Raise PropertyChanged of RepeatString upon collection changes.

private void Repeat_CollectionChanged(object sender, CollectionChangedEventArgs e)
{
    OnPropertyChanged(new PropertyChangedEventArgs("RepeatString"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM