public class DependencyObjectViewModel : DependencyObject
{
public static readonly DependencyProperty MainCanvasProperty =
DependencyProperty.Register("MainCanvas", typeof(string), typeof(DependencyObjectViewModel), new UIPropertyMetadata(null));
}
ViewModel
Code (C#)
public class MainViewModel : DependencyObjectViewModel
{
#region Data
private ICommand runCommand;
private ICommand shouldFailCommand;
private Boolean shouldFail = false;
private ThreadableItemViewModel<List<PropertyDataBase>> threadVM =
new ThreadableItemViewModel<List<PropertyDataBase>>();
#endregion
#region Constructor
public MainViewModel()
{
Run();
}
#endregion
private void Run()
{
int i = 0;
Func<Dictionary<String, Object>, ThreadableItem<List<PropertyDataBase>>> taskFunc = (inputParams) =>
{
try
{
#region TEST EXAMPLE CODE, YOU SHOULD NOT DO THIS IN PRODUCTION CODE
if (ShouldFail)// thread error
{
throw new InvalidOperationException(
"InvalidOperationException occurred\r\n\r\n" +
"This Exception has been raised inside the Window1ViewModel delegate " +
"which is the actual payload for the ThreadableItemViewModel<T> TaskFunc delegate.\r\n\r\n" +
"Which is obvioulsy not what you would do in a production system.\r\n\r\n" +
"You would more typically catch your own business Exceptions " +
"(say from talking to WCF) and then rethrow them. " +
"This just demomstrated how all this hangs together");
}
else
{
List<PropertyDataBase> data = new List<PropertyDataBase>();
GlobalModel.GlobalMetrolClick = true;
if (GlobalModel.GlobalMetrolClick)
{
ThreadModel dm = new ThreadModel();
dm.ConnectModel();
var ServiceNotOffJob = from ser in dm.ServiceModel
join cus in dm.CustomerModel on ser.cusID equals cus.cusID
where cus.cusID.Equals("M000000001") &&
ser.IsServices.Equals("3")
select new PropertyDataBase
{
CustomerID = cus.cusID,
ServiceID = ser.serID
};
if (ServiceNotOffJob.Count() > 0)
{
i = (Int32)inputParams["loopMax"];
foreach (var item in ServiceNotOffJob)
{
data.Add(new PropertyDataBase()
{
CustomerID = item.CustomerID,
ServiceID = item.ServiceID
});
i++;
}
}
}
return new ThreadableItem<List<PropertyDataBase>>(data, String.Empty);
}
#endregion
}
catch (Exception ex)
{
return new ThreadableItem<List<PropertyDataBase>>(null, ex.Message);
}
};
threadVM.TaskFunc = taskFunc;
Dictionary<String, Object> parameters = new Dictionary<String, Object>();
parameters.Add("loopMax", i);
threadVM.Parameters = parameters;
runCommand = new SimpleCommand // Run
{
CanExecuteDelegate = x => !threadVM.IsBusy,
ExecuteDelegate = x => { threadVM.Run(); }
};
shouldFailCommand = new SimpleCommand // Test Fail
{
CanExecuteDelegate = x => !threadVM.IsBusy,
ExecuteDelegate = x => { ShouldFail = !ShouldFail; }
};
}
public ThreadableItemViewModel<List<PropertyDataBase>> ThreadVM // Property ThreadVM
{
get { return threadVM; }
}
public ICommand RunCommand
{
get { return runCommand; }
}
public ICommand ShouldFailCommand
{
get { return shouldFailCommand; }
}
public bool ShouldFail // Property ShouldFail
{
get { return shouldFail; }
set
{
shouldFail = value;
//base.OnPropertyChanged("ShouldFail");
}
}
//ผมทำได้เท่านี้ครับ
private double initMouseX;
private double finalMouseX;
private double x;
double mspWidth;
private double newx;
private DispatcherTimer timer;
private DoubleAnimationUsingKeyFrames anim;
public LoginView MainCanvas //ผม Biding ผ่าน Property นี้ครับ เอา Position mouse จาก MainCanvas ใน view ง่ะๆๆ
{
get { return (LoginView)GetValue(MainCanvasProperty); }
set { SetValue(MainCanvasProperty, value);}
}
public LoginView MetroStackPanel
{
get { return (LoginView)GetValue(MetroStackPanelProperty); }
set { SetValue(MetroStackPanelProperty, value); }
}
public void OnMainCanvas(object sener, MouseButtonEventArgs e)
{
initMouseX = e.GetPosition(MainCanvas).X; // ได้ position mouse มาแล้วครับ
x = Canvas.GetLeft(MetroStackPanel);// <<< error ครับ ไม่รู้จะ biding ยังไง ใน view MetroStackPanel มันเป็นชื่อ ของ Control Canvas อะครับ มึนครับ จะอ้างตำแหน่ง Canvas.GetLeft ของ Canvas ใน View ได้ยังไง
MessageBox.Show(x.ToString());
}
}