我有一個使用MVVM的WPF應用程序。我的ViewModel上的模型實例上綁定了一個布爾值的IsChecked值。我還需要將ViewModel上的方法綁定到Checked和Unchecked事件。 (這是這樣我就可以跟蹤未保存的更改和改變背景給我的用戶需要保存視覺指示我想:複選框上的雙重綁定
<CheckBox
Content="Enable"
Margin="5"
IsChecked="{Binding Enabled}"
Checked="{Binding ScheduleChanged}"
Unchecked="{Binding ScheduleChanged}"
/>
但我得到一個System.Windows.Data「上提供價值」。 ?結合「拋出一個異常」的錯誤建議
這裏是我一起工作的型號:
public class Schedule : IEquatable<Schedule>
{
private DateTime _scheduledStart;
private DateTime _scheduledEnd;
private bool _enabled;
private string _url;
public DateTime ScheduledStart
{
get { return _scheduledStart; }
set
{
_scheduledStart = value;
}
}
public DateTime ScheduledEnd
{
get { return _scheduledEnd; }
set
{
if(value < ScheduledStart)
{
throw new ArgumentException("Scheduled End cannot be earlier than Scheduled Start.");
}
else
{
_scheduledEnd = value;
}
}
}
public bool Enabled
{
get { return _enabled; }
set { _enabled = value; }
}
public string Url
{
get { return _url; }
set { _url = value; }
}
public bool Equals(Schedule other)
{
if(this.ScheduledStart == other.ScheduledStart && this.ScheduledEnd == other.ScheduledEnd
&& this.Enabled == other.Enabled && this.Url == other.Url)
{
return true;
}
else
{
return false;
}
}
}
我的視圖模型包含有一個ObservableCollection屬性一個ItemsControl綁定到集合並生成。 list。所以我的ViewModel有點了解我的Model實例,b我不認爲哪一個是不知道的。
謝謝你的答覆。我有這個綁定工作,但是,我希望能夠跟蹤其他領域的價值變化。一個文本框和來自Xceed的幾個DateTimePicker。所有這一切都是爲了設置一個標誌,用於觸發背景顏色的變化,如果有事情發生變化並且保存按鈕尚未被點擊。 –
@AaronRumford我剛剛添加了更多的細節。您可以使用綁定到上面的IsDirty屬性的樣式DataTrigger來更改UI的外觀。 – RQDQ