简体   繁体   English

WPF绑定属性未在更新时刷新

[英]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: 在XAML部分,我将绑定设置如下:

<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? 当我改变布尔值时,我该怎么做才能刷新代表RepeatString的TextBlock? of Repeat[] ? 重复[]?

You're raising RepeatString whenever the property of Repeat changes, but you want to subscribe to the changes of collection items. 每当Repeat属性更改时,您都在引发RepeatString ,但您想订阅集合项的更改。

Subscribe to collection changes of Repeat in your constructor (or wherever you instantiate _repeat ). 在构造函数中(或实例化_repeat任何地方)订阅Repeat集合更改。

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

Raise PropertyChanged of RepeatString upon collection changes. 在更改集合时提高RepeatString PropertyChanged

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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