Access properties in usercontrol - user-controls

I created a usercontrol with 2 properties :
Url : image url (string)
Color : color to tint the image (System.Windows.Media.Color)
There is my XAML to call the usercontrol :
<myUserControl:MyImageTint Url="Assets/Images/decoration.png" Color="{Binding ImageColor}"/>
There is my code behind in the usercontrol
public static readonly DependencyProperty UrlProperty = DependencyProperty.Register("Url", typeof(string), typeof(MyImageTint), new PropertyMetadata(string.Empty));
public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(MyImageTint), new PropertyMetadata(null));
public string Url
{
get
{
return (string)GetValue(UrlProperty);
}
set
{
SetValue(UrlProperty, value);
}
}
public string Color
{
get
{
return (string)GetValue(ColorProperty);
}
set
{
SetValue(ColorProperty, value);
}
}
Now I would like to use this properties to tinted my image.
In the Url setter I can set my image url and display it. But I can't use the color to tint the image. The property is null.
How can I use this property in the same function ?

I tinted my image in the usercontrol Loaded event, like this and I can use my properties
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
TintMyImage(Url, Color);
}
Is it the best way ?

Related

How to make button that was created in iOS-Designer public?

Basically I want to be able to access my button, which is located in a custom cell in a table view, in my source class in the GetCell-Method. Unfortunatley iOS-Designer sets the visibility on private.
How do I change this or do I have to create a Button programmatically?
EDIT: I want to access it like this in my source class
var cell = tableView.DequeueReusableCell(CellIdentifier) as SearchDetailsCell;
var rownumber = cell.OpenDocumentButton.Tag;
Set a tag to your button (11 in the below example) and then access it like :
UIButton MyButton = (UIButton)cell.ViewWithTag (11);
You can simply change the access specifier in designer file
As, I did below;
namespace MYPROJ.iOS
{
[Register ("LargePhotoViewController")]
partial class LargePhotoViewController
{
[Outlet]
[GeneratedCode ("iOS Designer", "1.0")]
public UILabel Comment { get; set; }
[Outlet]
[GeneratedCode ("iOS Designer", "1.0")]
UIImageView Image { get; set; }
void ReleaseDesignerOutlets ()
{
if (Comment != null) {
Comment.Dispose ();
Comment = null;
}
if (Image != null) {
Image.Dispose ();
Image = null;
}
}
}
}

How to add more than one same custom view to a grid layout in android from Java code

I am learning custom views in android.
I made one custom view, with a rectangle and a text. The code of the custom view is:
public class TileGenerator extends View {
// rectangle parameters
private Rect rect = new Rect();
private Paint rectPaint = new Paint();
private Integer rectEndX;
private Integer rectEndY;
private Integer rectStartX;
private Integer rectStartY;
private Integer rectFillColor;
private Float rectTextStartX;
private Float rectTextStartY;
//rectangle text
private String rectText;
private Paint rectTextPaint = new Paint();
public TileGenerator(Context context) {
super(context);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void setTileTitleText(String rectText) {
this.rectText = rectText;
}
#Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
rectEndX = getrectEndX();
rectEndY = getrectEndY();
rectStartX = getRectStartX();
rectStartY = getRectStartY();
rectTextStartX = rectEndX/4f + rectStartX;
rectTextStartY = 3.5f * rectEndY/4f + rectStartY;
rectTextPaint.setTextSize(rectEndY/8);
rectTextPaint.setColor(Color.BLACK);
rect.set(rectStartX,rectStartY,rectEndX,rectEndY);
rectPaint.setColor(getRectFillColor());
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(rect, rectPaint);
canvas.drawText(rectText,rectTextStartX,rectTextStartY,rectTextPaint );
}
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
}
public Integer getrectEndX() {
return rectEndX;
}
public void setrectEndX(Integer rectEndX) {
this.rectEndX = rectEndX;
}
public Integer getrectEndY() {
return rectEndY;
}
public void setrectEndY(Integer rectEndY) {
this.rectEndY = rectEndY;
}
public Integer getRectStartX() {
return rectStartX;
}
public void setRectStartX(Integer rectStartX) {
this.rectStartX = rectStartX;
}
public Integer getRectStartY() {
return rectStartY;
}
public void setRectStartY(Integer rectStartY) {
this.rectStartY = rectStartY;
}
public Integer getRectFillColor() {
return rectFillColor;
}
public void setRectFillColor(Integer rectFillColor) {
this.rectFillColor = rectFillColor;
}
public String getRectText() {
return rectText;
}
}
After that I created an blank activity. I am doing all with JAVA code. No XML. Then I try to add above custom view to a gridview layout. I want to add two custom views with different text in a horizontal gridview. So far my code is as below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GridLayout gridLayout = new GridLayout(this);
// first custom view
CustomRectWithText customRectWithText = new CustomRectWithText(this);
customRectWithText.setRectEndX(200);
customRectWithText.setRectEndY(200);
customRectWithText.setRectStartX(2);
customRectWithText.setRectStartY(2);
customRectWithText.setImage(image);
customRectWithText.setRectText("Text");
customRectWithText.setRectFillColor(Color.BLUE);
gridLayout.addView(customRectWithText);
// second custom view
CustomRectWithText customRectWithText1 = new CustomRectWithText(this);
customRectWithText1.setRectEndX(400);
customRectWithText1.setRectEndY(200);
customRectWithText1.setRectStartX(200 + 5);
customRectWithText1.setRectStartY(2);
customRectWithText1.setTileTitleText("Text 1");
customRectWithText1.setRectFillColor(Color.GREEN);
gridLayout.addView(customRectWithText1);
setContentView(gridLayout);
}
But still I am not getting both of the rectangles in a grid view. Only one rectangle is displayed at a time. In above case only first custom view is displayed.
Where am I doing wrong.
All I want is to make a repetitive rectangle of varying labels of any size inside a grid view.
Is this the way to do it. I mean is there any other way around.
I dont want to use ListItems.
Sorry but i do not have enough repo to comment.
But why dont you make an adapter?
Gridview behaves same as listView.
Use adapter to fill your grid.
This is the proper way to populate listView and gridView also.

Exposing child's property for binding

Assume we have a CustomView with Entry (or any other view) inside of it:
public class CustomView : ContentView
{
public CustomView()
{
var entry = new Entry();
Content = entry;
}
}
How to expose entry's Text property to make view's Text property that is bindable? So view's Text property should be two-way bindable and it should be synchronised with entry's Text property.
You can create your own BindableProperties. In this case, as it's an entry, make sure the default binding mode is 2-way. Then you can just set the BindingContext of the Entry to the current object, and bind the ContentView.Text to the the Entry.TextProperty.
public class CustomView : ContentView
{
public CustomView ()
{
var entry = new Entry ();
entry.SetBinding (Entry.TextProperty, "Text");
entry.BindingContext = this;
}
public static readonly BindableProperty TextProperty =
BindableProperty.Create ("Text", typeof(string), typeof(CustomView), default(string), BindingMode.TwoWay);
public string Text {
get { return (string)GetValue (TextProperty); }
set { SetValue (TextProperty, value); }
}
}

Custom bindable control in a MvvmCross Touch project

I have a MvxBaseBindableCollectionViewCell which loads a xib that contains a custom button. I would like to be able to pass this custom button a ViewModel to bind to. Is this possible?
I'm trying to acheive something like MyButton.ViewModel = ViewModel.ChildViewModel and have ViewModel.ChildViewModel.Name show as the button title.
If you want to custom bind a cell, then there's a tutorial on this in http://slodge.blogspot.co.uk/2013/01/uitableviewcell-using-xib-editor.html
If you want to create a fully bindable UIButton within that View then you can do this using some inheritance like:
[Register("MyButton")]
public class MyButton
: UIButton
, IMvxServiceConsumer
{
private IList<IMvxUpdateableBinding> _bindings;
private const string BindingText = "SpecialTitle Customer.Name";
public MyButton()
{
}
public MyButton(IntPtr handle)
: base(handle)
{
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
foreach (var binding in _bindings)
{
binding.Dispose();
}
_bindings.Clear();
}
base.Dispose(disposing);
}
private object _dc;
public object DataContext
{
get { return _dc; }
set
{
_dc = value;
if (_bindings == null)
{
var binder = this.GetService<IMvxBinder>();
_bindings = binder.Bind(_dc, this, BindingText).ToList();
}
else
{
foreach (var binding in _bindings)
{
binding.DataContext = _dc;
}
}
}
}
public string SpecialTitle
{
get { return this.GetTitle(UIControlState.Normal); }
set { this.SetTitle(value, UIControlState.Normal); }
}
}
Aside> MvvmCross v3 "Hot Tuna" will contain some helper classes to make this a bit simpler to do.

Edit string collection in VS2010 collection editor

As all my my doubts are used to vanish here... :) I have got an other question.
I have a custom control in which I have a list of strings List and I'd like the user of my control to be able to edit the list in the properties editor but I doesn't work.. I can click on the button to make the Collection editor visible but the add key is not enabled and there's a message 'Property editing is not available'.
I made a custom quick and dirty class
public class DataUrl
{
public string Url {get; set;}
public DataUrl() { }
public override string ToString()
{
return Url.ToString();
}
}
and with this it works but its...
I suspect it doesn't work because string (or String) does not have a parameter-less constructor. I also tried to use the attribute
[NewItemTypesAttribute(typeof(string))]
but worthless..
Could someone help me ?
public class DataUrl : Component
{
private readonly List<string> _urlList = new List<string>();
public DataUrl() : base() {}
public DataUrl(IContainer container) : base()
{
container.Add(this);
InitializeComponent();
}
[Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(System.Drawing.Design.UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<string> UrlList { get { return _urlList; } }
public override string ToString()
{
return Url.ToString();
}
}

Resources