Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am trying to design a usercontrol that has 2 circles, a and b. I want to set the size of the circles from the mainpage via using tC.a and tC.b. And I want to be able to re-use the same graphic without invoking a new one. The problem is that twoCircles does not respond to changes in the .a and .b parameters.
My usercontrol code is as follows:
C#
    public partial class twoCircles : UserControl
{
    private double a1,b1;
    public double a {
    get {return a1;}
    set {this.a1=value;}
    }
    public double b {
    get {return b1;}
    set {this.b1=value;}
    }
    SolidColorBrush brA=new SolidColorBrush{Color=Colors.Brown};
    SolidColorBrush brB=new SolidColorBrush{Color=Colors.Blue};
    public twoCircles()
    {
        InitializeComponent();
        Ellipse eA=new Ellipse{Width=a,Height=a,Fill=brA};
        Ellipse eB=new Ellipse{Width=b,Height=b,Fill=brB};
        Canvas.SetLeft(eA,300);Canvas.SetTop(eA,100);
        LayoutRoot.Children.Add(eA);
        Canvas.SetLeft(eB,300);Canvas.SetTop(eB,300);
        LayoutRoot.Children.Add(eB);
    }
}

My mainpage is as follows:
C#
public partial class MainPage : UserControl
{
    twoCircles tC=new twoCircles();
    public MainPage()
    {
        InitializeComponent();
        tC.a=100;tC.b=150;
        Canvas.SetLeft(tC,200);Canvas.SetTop(tC,200);
        LayoutRoot.Children.Add(tC);
    }
}

I have searched and searched for the reason for this and have not found it. Can you help?

[Edit] Added code block - JOAT-MON [/Edit]
Posted
Updated 9-May-11 12:58pm
v3
Comments
Sergey Alexandrovich Kryukov 11-May-11 16:17pm    
John, please tag it immediately. WPF, Forms of what. It looks like WPF but from you earlier version I thought it was Forms.
I'm really tired of permanent confusions. Every inquirer should put all relevant tags properly.
Thank you.
--SA

When you change some data, don't forget to call System.Windows.Forms.Control.Invalidate. Use Invalidate with parameters (Rectangle or Region) if you want — for the same of performance — to invalidate only the part of the control area.

—SA
 
Share this answer
 
Your property setters actually need to do something. Computers are not magical, they only do what you tell them to. In this case, you need to tell the ellipses to resize themselves.

Try something like
class TwoCircles : UserControl {
 Ellipse e1, e2;
 public double A { { get { return e1.Width; } set { e1.Width = e1.Height = value; } }
 public double B { { get { return e2.Width; } set { e2.Width = e2.Height = value; } }
 
 public TwoCircles(double a, double b){
  e1=new Ellipse{Width=a,Height=a};
  e2=new Ellipse{Width=b,Height=b};
  // ... etc (position/add code)
 }
}


(I removed the a1 and b1 fields because all they were doing was storing parameters already available through the ellipses.)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-May-11 11:04am    
Sure, but it won't be shown on screen before you Invalidate! This is the only problem.
Fix it, and I'll up-vote it.
--SA
BobJanova 11-May-11 13:23pm    
Doesn't setting the Width/Height of the Ellipse class do that already? I must say I'm not familiar with that class, but I think this is WPF, and most WPF controls automatically update the view when you change their properties.
Sergey Alexandrovich Kryukov 11-May-11 16:14pm    
I don't know. When I was answering, OP had the call to Invalidate (which was missing in right place), which is the sign of Forms. So I was sure it's Forms, and Ellipse is just custom class. Of course with WPF this is not applicable...

You know what? I'm tired of it. We should refuse to give any answers until OP put all tags correctly. I don't want to discuss it until OP tags it.

Thank you for your discussion.
--SA
MainPage xaml file to be used to demonstrate solution: Note the use of 3 sliders for the UC parameters
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:setTest1"
x:Class="setTest1.MainPage"
Width="640" Height="480">
<Canvas x:Name="LayoutRoot" Background="White">
<TextBlock x:Name="aTxt" Height="28" Canvas.Left="323" TextWrapping="Wrap" Text="a:" Canvas.Top="158" Width="103"/>
<Slider x:Name="aSlider" Height="26" Canvas.Left="323" Canvas.Top="132" Width="123" LargeChange="10" Maximum="200" SmallChange="1" Value="100"/>
<TextBlock x:Name="bTxt" Height="28" Canvas.Left="463" TextWrapping="Wrap" Text="b: " Canvas.Top="158" Width="103"/>
<Slider x:Name="bSlider" Height="26" Canvas.Left="463" Canvas.Top="132" Width="123" LargeChange="10" Maximum="200" SmallChange="1" Value="100"/>
<TextBlock x:Name="dxTxt" Height="28" Canvas.Left="463" TextWrapping="Wrap" Text="dx:" Canvas.Top="216" Width="103"/>
<Slider x:Name="dxSlider" Height="26" Canvas.Left="463" Canvas.Top="190" Width="123" LargeChange="10" Maximum="400" SmallChange="1" Value="100" Minimum="100"/>
</Canvas>
</UserControl>

//twoCircles Usercontrol file
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace setTest1
{
public partial class twoCircles : UserControl
{
SolidColorBrush brA=new SolidColorBrush{Color=Colors.Brown};
SolidColorBrush brB=new SolidColorBrush{Color=Colors.Blue};
public Ellipse eA=new Ellipse(),eB=new Ellipse();
private double a1,b1,dx1;

public double a {
get {return a1;}
set {this.a1=value;
reDraw();
}
}

public double b {
get {return b1;}
set {this.b1=value;
reDraw();
}
}

public double dx {
get {return dx1;}
set {this.dx1=value;
reDraw();
}
}
public twoCircles()
{
InitializeComponent();
eA.Fill=brA;
eB.Fill=brB;
LayoutRoot.Children.Add(eA);
LayoutRoot.Children.Add(eB);
}
private void reDraw()
{
eA.Width=a;
eA.Height=a;
eB.Width=b;
eB.Height=b;
eB.Margin=new Thickness(dx,(a-b)/2,0,0);
}
}
}

//MainPage.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace setTest1
{
public partial class MainPage : UserControl
{
twoCircles tC=new twoCircles();
static double a=200,b=100,dx=200;
public MainPage()
{
InitializeComponent();
tC.a=a;tC.b=b;tC.dx=200;
Canvas.SetLeft(tC,200);Canvas.SetTop(tC,300);
LayoutRoot.Children.Add(tC);
initSliders();
}
private void initSliders()
{
aSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(aSlider_ValueChanged);
aSlider.Value=a;
aTxt.Text="a: " +a.ToString("000");
bSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(bSlider_ValueChanged);
bSlider.Value=b;
bTxt.Text="b: " +b.ToString("000");
dxSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(dxSlider_ValueChanged);
dxSlider.Value=dx;
dxTxt.Text="dx: " +dx.ToString("000");
}
private void bSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
LayoutRoot.Children.Remove(tC);
b=bSlider.Value;
tC.b=b;
bTxt.Text="b: " +b.ToString("000");
LayoutRoot.Children.Add(tC);
}
private void aSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
LayoutRoot.Children.Remove(tC);
a=aSlider.Value;
tC.a=a;
aTxt.Text="a: " +a.ToString("000");
LayoutRoot.Children.Add(tC);
}
private void dxSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
LayoutRoot.Children.Remove(tC);
dx=dxSlider.Value;
tC.dx=dx;
dxTxt.Text="dx: " +dx.ToString("000");
LayoutRoot.Children.Add(tC);
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900