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

时间:2014-11-13 10:04:38

标签: c# wpf xaml binding

我在刷新对象中的属性时遇到了问题。在启动它时会显示精细的结果,但在我编辑源代码后,我的窗口中的值不会刷新。

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;
        }
        [...]
    }
    [...]
}

在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>

这正在更新数组,因为我点击了Checkboxes。但不是以下文本块绑定。

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

当我更改bool时,我需要做些什么来刷新表示RepeatString的TextBlock?重复[]?

1 个答案:

答案 0 :(得分:1)

只要RepeatString的属性发生更改,您就会提出Repeat,但您想订阅集合项的更改。

订阅构建器中Repeat的集合更改(或您实例化_repeat的任何位置)。

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

在收集更改时提升PropertyChanged RepeatString

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