在图形用户控件中设置参数 [英] setting the parameters in graphic usercontrols

查看:62
本文介绍了在图形用户控件中设置参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设计一个具有2个圆圈(a和b)的用户控件.我想通过使用tC.a和tC.b从主页设置圆圈的大小.而且我希望能够重新使用相同的图形而无需调用新的图形.问题在于twoCircles无法响应.a和.b参数的变化.
我的用户控制代码如下:

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:

    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:

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);
    }
}


我已经搜索并搜索了原因,但没有找到.你能帮忙吗?

添加了代码块-JOAT-MON [/Edit]


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

Added code block - JOAT-MON [/Edit]

推荐答案

更改某些数据时,请不要忘记调用System.Windows.Forms.Control.Invalidate.如果要使性能相同,可以将Invalidate与参数(矩形或区域)一起使用,以仅使控制区域的一部分无效.

—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


您的属性设置者实际上需要做点事情.电脑不是神奇的东西,它们只会按照您的指示去做.在这种情况下,您需要告诉椭圆调整自身大小.

试试类似
的东西
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)
 }
}



(我删除了a1和b1字段,因为它们所做的只是存储椭圆中已经可用的参数.)



(I removed the a1 and b1 fields because all they were doing was storing parameters already available through the ellipses.)


MainPage xaml文件用于演示解决方案:请注意使用3个滑块用于UC参数
< 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"
宽度="640"高度="480">
<画布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"值="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"值="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"值="100"最小="100"/>
</画布>
</UserControl>

//twoCircles用户控制文件
使用系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Documents;
使用System.Windows.Ink;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Animation;
使用System.Windows.Shapes;
命名空间setTest1
{
公共局部类twoCircles:UserControl
{
SolidColorBrush brA =新的SolidColorBrush {Color = Colors.Brown};
SolidColorBrush brB =新的SolidColorBrush {Color = Colors.Blue};
公共椭圆eA = new Ellipse(),eB = new Ellipse();
私人双人间a1,b1,dx1;

公开双打{
得到{return a1;}
设置{this.a1 = value;
reDraw();
}
}

public double b {
得到{return b1;}
设置{this.b1 = value;
reDraw();
}
}

公开双dx {
得到{return dx1;}
设置{this.dx1 = value;
reDraw();
}
}
公共twoCircles()
{
InitializeComponent();
eA.Fill = brA;
eB.Fill = brB;
LayoutRoot.Children.Add(eA);
LayoutRoot.Children.Add(eB);
}
私有无效reDraw()
{
eA.Width = a;
eA.Height = a;
eB.Width = b;
eB.Height = b;
eB.Margin = new厚度(dx,(a-b)/2,0,0);
}
}
}

//MainPage.cs文件:
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Net;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Animation;
使用System.Windows.Shapes;
命名空间setTest1
{
公共局部类MainPage:UserControl
{
twoCircles tC = new twoCircles();
静态double a = 200,b = 100,dx = 200;
公共MainPage()
{
InitializeComponent();
tC.a = a; tC.b = b; tC.dx = 200;
Canvas.SetLeft(tC,200); Canvas.SetTop(tC,300);
LayoutRoot.Children.Add(tC);
initSliders();
}
私有无效initSliders()
{
aSlider.ValueChanged + =新的RoutedPropertyChangedEventHandler< double>(aSlider_ValueChanged);
aSlider.Value = a;
aTxt.Text ="a:" + a.ToString("000");
bSlider.ValueChanged + =新的RoutedPropertyChangedEventHandler< double>(bSlider_ValueChanged);
bSlider.Value = b;
bTxt.Text ="b:" + b.ToString("000");
dxSlider.ValueChanged + =新的RoutedPropertyChangedEventHandler< double>(dxSlider_ValueChanged);
dxSlider.Value = dx;
dxTxt.Text ="dx:" + dx.ToString("000");
}
私有void bSlider_ValueChanged(对象发送者,RoutedPropertyChangedEventArgs< double> e)
{
LayoutRoot.Children.Remove(tC);
b = bSlider.Value;
tC.b = b;
bTxt.Text ="b:" + b.ToString("000");
LayoutRoot.Children.Add(tC);
}
私有void aSlider_ValueChanged(对象发送者,RoutedPropertyChangedEventArgs< double> e)
{
LayoutRoot.Children.Remove(tC);
a = aSlider.Value;
tC.a = a;
aTxt.Text ="a:" + a.ToString("000");
LayoutRoot.Children.Add(tC);
}
私有void dxSlider_ValueChanged(对象发送者,RoutedPropertyChangedEventArgs< double> e)
{
LayoutRoot.Children.Remove(tC);
dx = dxSlider.Value;
tC.dx = dx;
dxTxt.Text ="dx:" + dx.ToString("000");
LayoutRoot.Children.Add(tC);
}
}
}
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);
}
}
}


这篇关于在图形用户控件中设置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆