簡體   English   中英

WPF綁定屬性未在更新時刷新

[英]WPF Binding Property not refreshing on update

我在刷新對象中的屬性綁定時遇到問題。 在啟動它顯示出良好的結果,但在我編輯源之后,該值未在我的窗口中刷新。

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? 重復[]?

每當Repeat屬性更改時,您都在引發RepeatString ,但您想訂閱集合項的更改。

在構造函數中(或實例化_repeat任何地方)訂閱Repeat集合更改。

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

在更改集合時提高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