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; }
想知道下面的答案是否對你有幫助? –
是的,謝謝,我沒有MemberwiseClone的家庭。但我已經查閱並實施了它。它的作用就像魅力。 – mrfatmen
很好聽。感謝您的反饋。 –