2014-05-11 60 views
0

這可能是一個新手問題,但我打破了這個問題。 我正在使用MVVM模式在WPF中構建應用程序。我有一個觀點,其中有一個領域綁定一個屬性暴露當前的客戶實體。在視圖內,我有一個命令來更改屬於該客戶的銀行帳號。所調用的命令將整個子實體作爲參數。然後調用另一個函數,該函數還將子實體作爲參數,並將其傳遞給綁定到新視圖的新視圖模型,該視圖是displayd作爲更改的對話框。這一切都有效。但是當我改變對話框中的銀行賬號時,原始視圖也會實時改變賬號號碼。他們仍然互相連接。我想取消這個鏈接,以便能夠取消對話框以及我在該對話框中所做的更改。但我無法得到這個工作。打破實體框架實體引用鏈接

代碼說的更多的話。

視圖主

<dxlc:LayoutGroup Header="Rekeningen" View="GroupBox" Orientation="Vertical" VerticalAlignment="Stretch"> 
    <dxlc:LayoutItem> 
     <StackPanel> 
      <Button Content="{x:Static language:Common.NieuwRekeningnrToevoegen}" Command="{Binding NieuwRekeningCommand}" /> 
      <ListView ItemsSource="{Binding CurrentRelatie.Rekeningnummers}" ItemTemplate="{StaticResource RelatieRekeningnrTemplate}" /> 
     </StackPanel> 
    </dxlc:LayoutItem> 
</dxlc:LayoutGroup> 

查看項目模板主要

<DataTemplate x:Key="RelatieRekeningnrTemplate"> 
    <Grid> 
     <TextBlock > 
      <Run Text="{Binding Omschrijving}" FontWeight="Bold" FontStyle="Italic" /> <LineBreak/> 
      <Run Text="{Binding RekNummer}" /> - <Run Text="{Binding BicNummer}" FontStyle="Italic" /> 
     </TextBlock> 
     <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top"> 
      <Button Command="{Binding DataContext.VerwijderRekeningCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" CommandParameter="{Binding}"> 
       <Button.Style> 
        <Style TargetType="{x:Type Button}"> 
         <Setter Property="Background" Value="{x:Null}" /> 
         <Setter Property="BorderBrush" Value="{x:Null}" /> 
         <Setter Property="BorderThickness" Value="1" /> 
         <Setter Property="Template" Value="{DynamicResource tplFlatButton}" /> 
        </Style> 
       </Button.Style> 
       <Path Height="9" Stretch="Uniform" Fill="{DynamicResource AccentColorDarkGray}" Data="{DynamicResource Delete}" /> 
      </Button> 
      <Button Command="{Binding DataContext.EditRekeningCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" CommandParameter="{Binding}"> 
       <Button.Style> 
        <Style TargetType="{x:Type Button}"> 
         <Setter Property="Background" Value="{x:Null}" /> 
         <Setter Property="BorderBrush" Value="{x:Null}" /> 
         <Setter Property="BorderThickness" Value="1" /> 
         <Setter Property="Template" Value="{DynamicResource tplFlatButton}" /> 
        </Style> 
       </Button.Style> 
       <Path Height="10" Stretch="Uniform" Fill="{DynamicResource AccentColorDarkGray}" Data="{DynamicResource Edit}" > 
       </Path> 
      </Button> 
     </StackPanel> 
    </Grid> 
</DataTemplate> 

視圖模型

private model.Relatie _CurrentRelatie = null; 
public model.Relatie CurrentRelatie 
{ 
    get { return _CurrentRelatie; } 
    set { SetProperty(ref _CurrentRelatie, value,() => CurrentRelatie); } 
} 

public ICommand EditRekeningCommand { get; private set; } 
void OnEditRekeningExecute(model.Rekeningnummer Rek) 
{ 
    EditRekeningnummer(Rek); 
} 

private void EditRekeningnummer(model.Rekeningnummer Rek) 
{ 
    Dialog.dRekeningnummerEditViewModel ReknummerVM = new Dialog.dRekeningnummerEditViewModel(); 
    ReknummerVM.SetRekening(Rek); 

    UICommand ResCommand = DialogService.ShowDialog(ReknummerVM.DialogUICommand, string.Format("{0} {1}", Common.Rekening, Rek.Omschrijving ?? Rek.RekNummer), "viewdRekeningnummerEdit", ReknummerVM); 
    if (ResCommand == null || ResCommand.IsCancel == true) 
     return; 
} 

查看RekeningnummerEdit

<dxlc:LayoutGroup Orientation="Vertical"> 

    <dxlc:LayoutItem Label="{Binding CurrentRekening, ConverterParameter=Omschrijving, Converter={StaticResource ModelToDisplay}}"> 
     <dxe:TextEdit EditValue="{Binding CurrentRekening.Omschrijving, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" ValidateOnTextInput="True" utils:FocusAdvancement.AdvancesByEnterKey="true"/> 
    </dxlc:LayoutItem> 

    <dxlc:LayoutItem Label="{Binding CurrentRekening, ConverterParameter=RekNummer, Converter={StaticResource ModelToDisplay}}"> 
     <dxe:TextEdit EditValue="{Binding CurrentRekening.RekNummer, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" ValidateOnTextInput="True" utils:FocusAdvancement.AdvancesByEnterKey="true"/> 
    </dxlc:LayoutItem> 

    <dxlc:LayoutItem Label="{Binding CurrentRekening, ConverterParameter=BicNummer, Converter={StaticResource ModelToDisplay}}"> 
     <dxe:TextEdit EditValue="{Binding CurrentRekening.BicNummer, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" ValidateOnTextInput="True" utils:FocusAdvancement.AdvancesByEnterKey="true"/> 
    </dxlc:LayoutItem> 

</dxlc:LayoutGroup> 

視圖模型RekeningnummerEdit

public dRekeningnummerEditViewModel() 
{ 
DialogUICommand = new List<UICommand>(); 

    AnnuleerUICommand = new UICommand() { 
     Caption=Common.Annuleren, 
     Id = MessageBoxResult.Cancel, 
     IsCancel=true 
    }; 

    OKUICommand = new UICommand() { 
     Caption=Common.Opslaan, 
     Id = MessageBoxResult.OK, 
     IsDefault=true 
    }; 
    DialogUICommand.Add(OKUICommand); 
    DialogUICommand.Add(AnnuleerUICommand); 

    CurrentRekening = new model.Rekeningnummer(); 
} 



public void SetRekening(model.Rekeningnummer Rek) 
{ 
    CurrentRekening = Rek; 
    IsInEditMode = true; 
} 

#region "Properties" 
private model.Rekeningnummer _CurrentRekening; 
public model.Rekeningnummer CurrentRekening 
{ 
    get { return _CurrentRekening; } 
    set { SetProperty(ref _CurrentRekening, value,() => CurrentRekening); } 
} 

#endregion 

#region "Private function" 


#endregion 

#region "Commands" 

public List<UICommand> DialogUICommand { get; private set; } 
protected UICommand AnnuleerUICommand { get; private set; } 
protected UICommand OKUICommand { get; private set; } 
+0

想知道下面的答案是否對你有幫助? –

+0

是的,謝謝,我沒有MemberwiseClone的家庭。但我已經查閱並實施了它。它的作用就像魅力。 – mrfatmen

+0

很好聽。感謝您的反饋。 –

回答

2

你看到的行爲是因爲你傳遞從您的視圖對象引用(model.Rek)到您的對話框。因此,當您的對話框更改model.Rek的值時,更改會立即反映在您的視圖中。

解決這個問題的一種常見方法是:

  1. 克隆(複製)你的模型,即讓具有相同值的新對象。您可以使用ICloneable接口爲標準模式(MemberwiseClone可以幫助在這裏,如果你只需要一個淺拷貝)
  2. 發送克隆到您的對話框
  3. 如果用戶按下OK,然後採取克隆的價值及複製他們回到原來的模型。如果用戶按下取消,則什麼也不做