我在一個WPF項目中工作,其中我的視圖模型有超過90個屬性。我點擊「保存」按鈕,將屬性保存到數據庫。我需要禁用保存按鈕,直到至少有一個屬性被修改。我不能去爲每個屬性進行OnPropertyChanged事件處理(僅僅因爲它對於超過90個屬性太麻煩)。是否有我可以這樣做,即使一個財產被更改,我應該得到通知,以便我可以啓用保存按鈕?在課堂上的OnPropertyChanged
<StackPanel>
<StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="{x:Static res:Resources.SaveToDB}" Command="{Binding Save}" IsEnabled="{Binding CanSaveToDB}" Margin="0,0,4,0" HorizontalAlignment="Center" Style="{StaticResource OverlayDialogButtonCentered}" FontSize="14"/>
<Button Content="{x:Static res:Resources.DiscardDBSave}" Command="{Binding DiscardSave}" Margin="0,0,4,0" HorizontalAlignment="Center" Style="{StaticResource OverlayDialogButtonCentered}" FontSize="14"/>
</StackPanel>
<StackPanel>
<views:MySampleAppView1 DataContextChanged="MySampleApp_DataContextChanged" DataContext="{Binding MySampleApp.View1}"/>
</StackPanel>
<StackPanel>
<views:MySampleAppView2 DataContextChanged="MySampleApp_DataContextChanged" DataContext="{Binding MySampleApp.View2}"/>
</StackPanel>
<StackPanel>
<views:MySampleAppView3 DataContextChanged="MySampleApp_DataContextChanged" DataContext="{Binding MySampleApp.View3}"/>
</StackPanel>
<StackPanel>
<views:MySampleAppView4 DataContextChanged="MySampleApp_DataContextChanged" DataContext="{Binding MySampleApp.View4}"/>
</StackPanel>
<StackPanel>
<views:MySampleAppView5 DataContextChanged="MySampleApp_DataContextChanged" DataContext="{Binding MySampleApp.View5}"/>
</StackPanel>
<StackPanel>
<views:MySampleAppView6 DataContextChanged="MySampleApp_DataContextChanged" DataContext="{Binding MySampleApp.View6}"/>
</StackPanel>
<StackPanel>
<views:MySampleAppView7 DataContextChanged="MySampleApp_DataContextChanged" DataContext="{Binding MySampleApp.View7}"/>
</StackPanel>
</StackPanel>
然後上面的每個視圖都有WPF控件。我對每個上述視圖都有viewmodels。然後是代表上述代碼的主視圖模型。
顯示可幫助我們幫助您的一些代碼。 –
添加示例代碼 –
只需忽略PropertyChangedEventArgs中的PropertyName,即在第一個PropertyChanged事件觸發時立即啓用Button,而不管實際的屬性名稱如何。 – Clemens