WPF Data binding C# in xaml - class

I would like to make the following code to be XAML:
CData cData = new CData();
MyClass mc1 = new MyClass();
MyClass mc2 = new MyClass();
mc1.Data = cData;
mc2.Data = cData;
How to make the above code to be XAML format?
Since the property .Data is a custom class CData and Both mc1 and mc3 has to point to the same cData. How to make it in XAML?

This should do:
<Window.Resources>
<local:CData x:Key="CData"/>
<local:MyClass x:Key="mc1" Data="{StaticResource CData}" />
<local:MyClass x:Key="mc2" Data="{StaticResource CData}" />
</Window.Resources>
where local points to your namespace where both classes are defined.

Related

How can I include a subclass definition file into an Octave class

I have two octave classes defined in the same directory:
/home/me/octave/#myclass/myclass.m
/home/me/octave/#mysubclass/mysubclass.m
mysubclass is a subclass of myclass:
mysubclass.m:
classdef mysubclass < handle
properties
my_property = 0;
endproperties
methods
function property = get()
property = my_property;
endfunction
endmethods
endclassdef
myclass.m:
classdef myclass < handle
properties
subclass = mysubclass();
endproperties
methods
function sub = get()
sub = subclass;
endfunction
endmethods
endclassdef
However I don't seem to be able to 'include' the subclass definition in the same way as I might for e.g. C++. I tried adding
addpath("../#mysubclass/");
at various points in myclass.m, but I get a syntax error.
Can someone help?

How to make property definition using own class in Matlab?

In Matlab I have the package "+mypackage". All the following definitions are under this folder/package.
MyClass.m
classdef MyClass
properties
prop1 MyProperty
end
methods (Static)
function obj = with_five()
obj = mypackage.MyClass(5);
end
end
methods
function obj = MyClass(num)
obj.prop1 = mypackage.MyProperty(num);
end
end
end
MyProperty.m
classdef MyProperty
properties
num double
end
methods
function obj = MyProperty(val)
obj.num = val;
end
end
end
So in MyClass.m I define the prop1 property to be an object of class MyProperty.
Now I would like to create objects with this script.
import mypackage.MyClass
class_test1 = MyClass(5);
class_test2 = MyClass.with_five();
When I run it I get following errors:
Error using Testpackage (line 3)
Error defining property 'prop1' of class 'mypackage.MyClass':
Class named 'MyProperty' is undefined or does not support property validation.
When I remove the property definition from MyClass.m it works.
Is there a way to define properties to be an object of an own class in Matlab?
Thank you Wolfie your Help!
I changed the definition for prop1 to mypackage.MyProperty.
My Class definition looks now like this:
classdef MyClass
properties
prop1 mypackage.MyProperty
end
methods (Static)
function obj = with_five()
obj = mypackage.MyClass(5);
end
end
methods
function obj = MyClass(num)
obj.prop1 = mypackage.MyProperty(num);
end
end
end
Its necessary to point to MyProperty inside my package mypackage.
Otherwise Matlab is not able to find it.
I found it in the Matlab help:
https://de.mathworks.com/help/matlab/matlab_oop/scoping-classes-with-packages.html?s_tid=srchtitle#brfynt_-3

Variable YYY originally saved as a XXX cannot be instantiated as an object and will be read in as a uint32

I've made a mistake and recorded a bunch of important data using a class definition that I can't find anymore. The methods are not important, I just want the data; a struct conversion would be fine.
Is there any way I can recover this?
Googling it did not help.
The solution is to create a new class that overloads the loadobj method. See here for more information regarding the load process for classes.
I replicated your problem by creating a class:
classdef myclass
properties
Prop1
Prop2
Prop3
end
methods
function obj = myclass(a,b,c)
obj.Prop1 = a;
obj.Prop2 = b;
obj.Prop3 = c;
end
end
end
Then I created an object of this class and saving it to a file "x.mat":
x = myclass('a',56,[1,2,3]);
save x
Next, I deleted the myclass file and did clear classes. This put me in your situation.
I then created a new myclass class that overloads the loadobj method:
classdef myclass
properties
data
end
methods (Static)
function obj = loadobj(s)
obj = myclass;
obj.data = s;
end
end
end
Note how it doesn't know any of the original properties. This does not matter. If any of the original properties is missing when loading an object from a MAT-file, loadobj will be called with s being a struct containing all the properties of the original object.
With this new class definition, load x created an object x of class myclass, where x.data was a struct containing the properties of the object saved in "x.mat".

how do you make classes with objects as member variables in matlab?

I have a project in matlab with the following directory structure:
+namespace\
#class1\
class1.m
#class2\
class2.m
mainfile.m
in class1.m I have something like the following
classdef class1
%readonly variables
properties(GetAccess = 'public',SetAccess = 'private')
forename;
lastname;
middlename;
end
properties(Constant = true)
%in centipascals
p1 = class2(param1,param2); %this is the part I need to work
end
methods(Access = public)
function this = class1(fname,lname,mname)
this.forename = fname;
this.lastname = lname;
this.middlename = mname;
end
end
end
I can't seem to get this class working. Class1 doesn't recognize the constructor of class2 (probably because something isn't being imported correctly). How do I import class2 or what do I need to do in order to have other class instances as member variables?
In Matlab, you need to fully qualify references to classes in a namespace, even from other classes within that same namespace. Like this.
classdef class1
properties (Constant = true)
%in centipascals
p1 = namespace.class2(param1,param2);
end
end
You can import other classes from the same namespace, but imports only work at a per-function level, and don't work in properties blocks at all AFAIK, so it won't work in this specific case, and may be more trouble than it's worth elsewhere.

GWT Exporter - How do i create an instance of object at runtime. Object type will be available as a string argument

I am using the following code in GWT client
Inside my jsni method I am using the following code, Assume that typeName is String argument
typeName = '$wnd.mysample.SampleButton'
var sample = new window[typeName]();
sample.addButton(name, parent);
SampleButton implements Exportable class, I used #ExportPackage("mysample") and #Export(all = true).
In My entry module I called ExporterUtil.exportAll();
Note: if I replace
var sample = new $wnd.mysample.SampleButton() with new window[typeName]() then it is working fine otherwise it is throwing undefined function called.
Kindly let me know how to create an instance for the type in JSNI code
eval('var sample = $wnd.mysample.SampleButton();'); solves the issue.