بستن یک پنجره از طریق ViewModel با استفاده از خصوصیت های پیوست شده هنگام استفاده از الگوی MVVM
نویسنده: محبوبه محمدی
تاریخ: ۱۳۹۲/۱۱/۰۲ ۱۴:۲۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace TestProject.XamlServices
{
public class CloseBehavior
{
public static readonly DependencyProperty CloseProperty =
DependencyProperty.RegisterAttached("Close", typeof(bool), typeof(CloseBehavior), new UIPropertyMetadata(false, OnClose));
private static void OnClose(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (!(e.NewValue is bool) || !((bool) e.NewValue)) return;
var win = GetWindow(sender);
if (win != null)
win.Close();
}
private static Window GetWindow(DependencyObject sender)
{
Window w = null;
if (sender is Window)
w = (Window)sender;
return w ?? (w = Window.GetWindow(sender));
}
public static bool GetClose(Window target)
{
return (bool)target.GetValue(CloseProperty);
}
public static void SetClose(DependencyObject target, bool value)
{
target.SetValue(CloseProperty, value);
}
}
} private bool _isClose;
public bool IsClose
{
get { return _isClose; }
set
{
_isClose = value;
OnClosed();
RaisePropertyChanged("IsClose");
}
} <Window x:Class="TestProject.TestView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xamlServices="clr-namespace:TestProject.XamlServices;assembly=TestProject.XamlServices"
xamlServices:CloseBehavior.Close="{Binding IsClose}">
...
</Window>