【问题标题】:WPF Binding Property not refreshing on updateWPF 绑定属性在更新时不刷新
【发布时间】:2014-11-13 10:04:38
【问题描述】:

我在刷新与对象中属性的绑定时遇到问题。在启动时它显示了很好的结果,但是在我编辑源之后,我的窗口中的值没有刷新。

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>

当我单击复选框时,这可以很好地更新数组。但不是以下文本块绑定。

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

当我更改布尔值时,我需要做什么来刷新代表 RepeatString 的 TextBlock?重复次数[]?

【问题讨论】:

    标签: c# wpf xaml binding


    【解决方案1】:

    每当Repeat 的属性发生变化时,您都会提高RepeatString,但您想订阅集合项的更改。

    在您的构造函数中订阅Repeat 的集合更改(或在您实例化_repeat 的任何位置)。

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

    在集合更改时提高 PropertyChangedRepeatString

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

    【讨论】:

      猜你喜欢
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-28
      • 2020-09-21
      • 2019-03-24
      相关资源
      最近更新 更多