초보자 질문이지만 작동시키지 못합니다. SO "How Should the View Model Close the form" 에 대한 답을 사용하려고, 난은 UserControl을 함께 정의 :연결된 속성 (MVVM)이있는 모달 대화 상자를 닫을 수 없습니까?
설계 쇼 동안<UserControl
.....
h:DialogCloser.DialogResult="{Binding DialogResult}"
>
:
public static class DialogCloser
{
public static readonly DependencyProperty DialogResultProperty =
DependencyProperty.RegisterAttached(
"DialogResult",
typeof(bool?),
typeof(DialogCloser),
new PropertyMetadata(DialogResultChanged));
private static void DialogResultChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var window = d as Window;
if (window != null)
window.DialogResult = e.NewValue as bool?;
}
public static void SetDialogResult(Window target, bool? value)
{
target.SetValue(DialogResultProperty, value);
}
}
:
Property 'DialogResult' is not attachable to elements of type 'UserControl'.
DialogCloser과 같이 정의된다 UserControl은 다음에 의해 열립니다.
var win = new WindowDialog();
win.Title = title;
win.DataContext = datacontext;
return win.ShowDialog();
01 UserControl을위한 뷰 모델에서 23,516,
, 나는이 :
public new void DoExit()
{
DialogResult = true;
}
private bool? dialogresult;
public bool? DialogResult
{
get
{
return dialogresult;
}
set
{
if (dialogresult != value)
{
dialogresult = value;
OnPropertyChanged("DialogResult");
}
}
}
) (DoExit를 사용하여 모달 대화, 내 UserControl을은 하지 가까이 않습니다. 뭐가 문제 야? 디자이너 (VS 2010)에 오류가 발생하지 않도록하려면 어떻게해야합니까?
어떤 도움을 주셔서 감사합니다.
부록 :
public class ModalDialogService : IModalDialogService
{
public bool? ShowDialog(string title, object datacontext)
{
var win = new WindowDialog();
win.Title = title;
win.DataContext = datacontext;
return win.ShowDialog();
}
}
참고 : "UserControl을"이 아니라 "윈도우"로했을 경우, 디자이너가 행복하지만, 그 오류는 다음과 같습니다
"Window must be the root of the tree. Cannot add Window as a child of Visual."
도움말 누군가?
이 코드의 위치는? :'var win = new WindowDialog(); ' – Sheridan
@Sheridan (다른 usercontrol의) 다른 뷰 모델입니다. –
그 코드가있는 경우보기 모델이 아닙니다. 뷰 모델은 '창'과 뷰에 대해 * 아무것도 * 알지 못합니다. 이 경우에는 '보기 모델'이 손상되었으므로 정상적으로 다음과 같이 'Window.Close'를 처리하십시오 : if (win.ShowDialog() == DialogResult.OK) DoSomething(); ' – Sheridan