2017-09-08 119 views
0

我在试图总结我的周围棱镜和Unity如何WPF中工作头上的过程,但目前已经由简单的任务包版广告时,如何重置/刷新绑定视图模型。可能我对它的工作原理有误解。WPF利用棱镜/统一

怎样才能刷新绑定视图模型?

我有使用棱镜RegionManager WPF应用程序加载了一个用户控件:

<ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion" x:Name="mainContent" HorizontalAlignment="Center" Margin="0,25,0,0"/> 

在我的用户控件我有一个字段的用户与一个提交按钮和清除按钮(简称用户填写控制内容)

<TextBox Margin="10,3,15,0" Text="{Binding LastName, UpdateSourceTrigger=PropertyChanged}" MinWidth="150" materialDesign:HintAssist.Hint="*Last Name" Style="{StaticResource MaterialDesignFloatingHintTextBox}" FontSize="16"/> 
<Button Command="{Binding ClearCommand}" Style="{StaticResource MaterialDesignRaisedAccentButton}" Margin="0 12 8 0" Width="155" ToolTip="Discard information entered and reset form" Background="#FF990B0B" Foreground="#FFECE9E9" BorderBrush="DarkRed">Cancel and Discard</Button> 

我场的结合的伟大工程,我也绑不住我的按钮,一个命令,将调用,我会想重置形式的方法:

public class CheckInViewModel : BindableBase 
{ 
    private IEventAggregator _eventAggregator; 

    private string _lastName; 
    public string LastName 
    { 
     get { return _lastName; } 
     set { SetProperty(ref _lastName, value); } 
    } 

    public DelegateCommand ClearCommand { get; set; } 

    private void ExecuteClear() 
    { 
     //reset form here 
    } 
    public CheckInViewModel(IEventAggregator eventAggregator) 
    { 
     _eventAggregator = eventAggregator; 
     ClearCommand = new DelegateCommand(ExecuteClear); 
    } 
} 

我知道,我很可能只是手动复位所有字段在ExecuteClear方法但这似乎有点笨重且容易出错在现实中我有40+场处理。

我试着将我的绑定字段移动到一个完全独立的模型,然后使该模型成为我的视图模型的一个属性,以便我可以在clear方法中重新实例化它,但似乎并没有在更新视图时进行更新。我想我必须错过一个方法调用来解除绑定我的最后一个模型,并重新绑定到新的,但我不知道如何去做,也找不到任何文档。

尝试失败例子:

public class CheckInViewModel : BindableBase 
{ 
    private IEventAggregator _eventAggregator; 
    public CheckInModel checkInModel { get; set; } 
    public DelegateCommand ClearCommand { get; set; } 

    private void ExecuteClear() 
    { 
     checkInModel = new CheckInModel(); 
    } 

    public CheckInViewModel(IEventAggregator eventAggregator) 
    { 
     checkInModel = new CheckInModel(); 
     _eventAggregator = eventAggregator; 
     ClearCommand = new DelegateCommand(ExecuteClear); 
    } 

} 


public class CheckInModel : BindableBase 
{ 

    private string _lastName 
    public string LastName 
    { 
     get { return _lastName; } 
     set { SetProperty(ref _lastName, value); } 
    } 

} 


<TextBox Margin="10,3,15,0" Text="{Binding checkInModel.LastName, UpdateSourceTrigger=PropertyChanged}" MinWidth="150" materialDesign:HintAssist.Hint="*Last Name" Style="{StaticResource MaterialDesignFloatingHintTextBox}" FontSize="16"/> 
<Button Command="{Binding ClearCommand}" Style="{StaticResource MaterialDesignRaisedAccentButton}" Margin="0 12 8 0" Width="155" ToolTip="Discard information entered and reset form" Background="#FF990B0B" Foreground="#FFECE9E9" BorderBrush="DarkRed">Cancel and Discard</Button> 

回答

1

在你失败的尝试,当你在ExecuteClear更新checkInModel不提高PropertyChanged财产。

将其更改为

private CheckInModel _checkInModel; 
public ChechInModel CheckInModel 
{ 
    get { return _checkInModel; } 
    set { SetPropery(ref _checkInModel, value); } 
} 

private void ExecuteClear() 
{ 
    CheckInModel = new CheckInModel(); 
} 

<TextBox Text="{Binding CheckInModel.LastName}"/> 
<Button Content="Cancel and Discard" Command="{Binding ClearCommand}"/> 

,你应该罚款。

+0

就是这样,不敢相信我忽略了这一点。感谢您接收它。 –