바인딩 할 수있는 "ClearCommand"ICommand 종속성 속성이있는 몇 가지 사용자 지정 컨트롤 (UserControl이 아님)을 만들었습니다. 이 속성은 소리 나는대로 정확하게 처리합니다. 컨트롤 (텍스트 상자 등)의 모든 값을 지 웁니다. 또한 아래에서 설명하는 VM에 동일한 속성 중 일부 (일부)를 바인딩합니다.MVVM을 사용하는 WPF의 ViewModel에서 명령 트리거
은 내가보기에 몇 이러한 컨트롤을 추가 한 :
지금 나는 다음과 같은 MVVM 시나리오에서 이러한 컨트롤에 ClearCommand을 트리거하기 위해 노력하고 붙어있어. 보기에는 내 ViewModel의 SaveCommand
DelegateCommand
속성에 바인딩되는 "저장"버튼도 있습니다.
내가해야 할 일은 성공적인 저장시 VM이보기에있는 해당 컨트롤에서 ClearCommand
을 트리거해야한다는 것입니다.
나는 아래의 코드 예제를 추가 한
UPDATE. ExampleCustomControl과 유사한 몇 가지 컨트롤이 있습니다. 또한 메모하기 만하면 완전히 해제 된 경우 일부를 재구성 할 수 있습니다.
예 제어 니펫을 :
public class ExampleCustomControl : Control {
public string SearchTextBox { get; set; }
public IEnumerable<CustomObject> ResultList { get; set; }
public ExampleCustomControl() {
ClearCommand = new DelegateCommand(Clear);
}
/// <summary>
/// Dependency Property for Datagrid ItemSource.
/// </summary>
public static DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem",
typeof(CustomObject), typeof(ExampleCustomControl), new PropertyMetadata(default(CustomObject)));
public CustomObject SelectedItem {
get { return (CustomObject)GetValue(SelectedCustomObjectProperty); }
set { SetValue(SelectedCustomObjectProperty, value); }
}
public static DependencyProperty ClearCommandProperty = DependencyProperty.Register("ClearCommand", typeof(ICommand),
typeof(ExampleCustomControl), new PropertyMetadata(default(ICommand)));
/// <summary>
/// Dependency Property for resetting the control
/// </summary>
[Description("The command that clears the control"), Category("Common Properties")]
public ICommand ClearCommand {
get { return (ICommand)GetValue(ClearCommandProperty); }
set { SetValue(ClearCommandProperty, value); }
}
public void Clear(object o) {
SearchTextBox = string.Empty;
SelectedItem = null;
ResultList = null;
}
}
예보기 미리보기 :
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<control:ExampleCustomControl Grid.Row="0"
SelectedItem="{Binding Selection, UpdateSourceTrigger=PropertyChanged}" />
<Button Grid.Row="1" x:Name="ResetButton" Command="{Binding SaveCommand}">
Save
</Button>
</Grid>
예 뷰 모델 :이 잘못된 길에 대해 생각처럼 소리
public class TestViewModel : WorkspaceTask {
public TestViewModel() {
View = new TestView { Model = this };
SaveCommand = new DelegateCommand(Save);
}
private CustomObject _selection;
public CustomObject Selection {
get { return _selection; }
set {
_selection = value;
OnPropertyChanged("Selection");
}
}
public DelegateCommand SaveCommand { get; private set; }
private void Save(object o) {
// perform save
// clear controls
}
}
일반적으로 내 명령은 내 ViewModel에 있습니다. ViewModel에서, 당신은'MyCommand.Execute();'를 호출 할 수있다. 그것이 프로젝트 구조화가 아니라면 명확히 할 수있는 코드를 게시하십시오. – cadrell0
빠른 해결 방법으로 SelectedIOtemProperty를 등록 할 때 콜백에서 PropertyMetadata에 PropertyChangedCallback을 설정하고 원하는 모든 항목을 지울 수 있습니다. 그러나 나는 당신의 셋업을 재구성하고 모든 바인딩 된 프로퍼티와 함께 VM에 명확한 정보를 넣는 것에 대해 생각해 보라고 권하고 싶습니다. – HAdes
ExampleCustomControl의 모든 속성을 뷰 모델에 바인딩하면 내 문제가 재사용됩니다. 검색 로직/etc를 사용자 정의 컨트롤에두고 싶습니다. 몇 가지 다른보기에서 동일한 컨트롤을 재사용 할 수 있기를 원합니다. – Alex