簡體   English   中英

如何以編程方式刷新WPF C#?

[英]How to programmatically refresh wpf c#?

我對這種情況很好,我讓模型解釋一下。

public class ScheduleMonthlyPerDayModel 
{
    public DateTime Date { get; set; }

    public string Day 
    { 
        get
        {
            return Date.Day.ToString();
        }
    }

    ObservableCollection<AppointmentDTO> _appointments;
    public ObservableCollection<AppointmentDTO> Appointments 
    {
        get
        {
            return _appointments;
        }
        set
        {
            _appointments = value;

            if (value.Count > 0)
                NotifyOfPropertyChange(() => HasSchedule);
        }
    }

    public bool BelongsToCurrentMonth
    {
        get;
        set;
    }

    public bool HasSchedule
    {
        get
        {
            return _appointments.Count > 0 ? true : false;
        }
    }

    public ScheduleMonthlyPerDayModel()
    {
        _appointments = new ObservableCollection<AppointmentDTO>();
    }

    public void ClearCollection()
    {
        _appointments.Clear();
    }
}

public class ScheduleMonthlyPerWeekModel
{
    public ScheduleMonthlyPerDayModel Sunday{get; set;}

    public ScheduleMonthlyPerDayModel Monday{get; set;}

    public ScheduleMonthlyPerDayModel Tuesday{get; set;}

    public ScheduleMonthlyPerDayModel Wednesday{get; set;}

    public ScheduleMonthlyPerDayModel Thursday{get; set;}

    public ScheduleMonthlyPerDayModel Friday{get; set;}

    public ScheduleMonthlyPerDayModel Saturday{get; set;}
}

到xaml的綁定可以像這樣一窺xaml:

headereditemscontrol itemsSource= weekcollection ,其中weekcollection是schedulemonthlyperweekmodel的對象。

在該headereditemscontrol內部,我為schedulemonthlyperweek模型的每個屬性設置了每天的模板,如下所示:

<Grid.ColumnDefinitions>
    <ColumnDefinition />
    <ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"  Style="{StaticResource CalendarDates}" Text="{Binding Path=Saturday.Day}" />
<ListBox Grid.Row="1" Grid.ColumnSpan="2" Padding="0" 
         ItemsSource="{Binding Path= Saturday.Appointments}"
         ItemTemplate="{StaticResource myItemStyle}"
         Visibility="{Binding Path=Saturday.HasSchedule, Converter={StaticResource BoolToVisibilityConverter}}" />

基本上,我正在努力實現每月查看一次約會的功能。 我的問題是,當我以編程方式將項目添加到saturday.appointments集合時,例如通過調試項目的添加成功並通知主集合(weekcollection)時,不會刷新UI。

我想要實現的是:在我將假定的約會添加到其對應的日期/日期之后,用戶界面也會相應更新,但是我該怎么做?

當前,僅當我更改/切換為不同然后返回時,UI才會更新,之后約會會很好地顯示。 我想將其自動化,因為要求用戶先切換到其他內容然后再返回,然后才能看到約會列表,這很丑陋。

正如Nick所建議的那樣,使用INotifyPropertyChanged接口是關鍵。

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

如果希望ObsCollection知道屬性更改,則必須實現上面的鏈接中顯示的此接口。 當添加,刪除或更改某些內容時,這將更新綁定到的UI控件。 沒有它,您實際上必須跟蹤代碼中的更改並手動更新它們。

實際上,它真的很容易實現和使用,而且簡直太棒了。 如果您不想使用此功能,則可以使用Winform。 =)

希望這可以幫助。

可見性的問題是它綁定到一個只讀屬性,該屬性計算get的值。 因此,它無法通知它已更改。

您的HasSchedule屬性需要知道“約會”屬性何時更改。 約會屬性的設置器僅知道整個列表何時更改。 對於您的情況,您需要知道列表內容何時更改。

ObservableCollection有一個事件,該事件告訴您列表內容何時更改,稱為CollectionChanged 您應該執行以下操作,以通知您HasSchedule屬性已使用此事件更改:

ObservableCollection<AppointmentDTO> _appointments;
public ObservableCollection<AppointmentDTO> Appointments
{
    get
    {
        return _appointments;
    }
    set
    {
        if (_appointments != value)
        {
            if (_appointments != null)
                _appointments.CollectionChanged -= Appointments_CollectionChanged;

            _appointments = value;

            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("HasSchedule"));

            if (_appointments != null)
                _appointments.CollectionChanged += Appointments_CollectionChanged;
        }
    }
}

void Appointments_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs("HasSchedule"));
}

如您所說,這是假設您已在ViewModel中實現了INotifyPropertyChanged。 在這種情況下,每次您的集合以某種方式更改時,它都會通知HasSchedule屬性已更改。 綁定將刷新值並更新可見性(如果已更改)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM