部落,帆布子不移位
我已经构建了一个Silverlight自定义控件TextBubble,它是网格中的椭圆和文本块。我将通过代码按需将这些文本气泡添加到画布对象。当我这样做时,TextBubble的属性被设置,但是当对象出现在画布上时不会反映出来。
如果我手动构建相同的结构并将其添加到canvas,canvas.Children.Add(grid),则此子显示正确。
使用TextBubble,显示文本和椭圆,但直径和X,Y没有被应用,气泡出现在画布的左上角。
感谢您的帮助, -AM
代码工作的:一点不
Grid g = new Grid();
g.SetValue(Canvas.TopProperty, 100.0);
g.SetValue(Canvas.LeftProperty, 100.0);
Ellipse e = new Ellipse();
e.Width = 50;
e.Height = 50;
e.Fill = new SolidColorBrush(Colors.Green);
g.Children.Add(e);
TextBlock t = new TextBlock();
t.Text = "TEST";
t.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
t.VerticalAlignment = System.Windows.VerticalAlignment.Center;
g.Children.Add(t);
this.canvas.Children.Add(g);
代码:
TextBubble bubble = new TextBubble();
bubble.Text = "TEST";
bubble.Diameter = 50;
bubble.Fill = new SolidColorBrush(Colors.Green);
bubble.X = 100;
bubble.Y = 100;
canvas.Children.Add(bubble);
TextBubble对象:
public class TextBubble : Control {
public TextBubble() {
this.DefaultStyleKey = typeof(TextBubble);
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text",
typeof(string),
typeof(TextBubble),
new PropertyMetadata(new PropertyChangedCallback(OnTextChanged)));
public static readonly DependencyProperty DiameterProperty = DependencyProperty.Register(
"Diameter",
typeof(int),
typeof(TextBubble),
new PropertyMetadata(new PropertyChangedCallback(OnDiameterChanged)));
public static readonly DependencyProperty FillProperty = DependencyProperty.Register(
"Fill",
typeof(Brush),
typeof(TextBubble),
new PropertyMetadata(new PropertyChangedCallback(OnFillChanged)));
public static readonly DependencyProperty XCoordProperty = DependencyProperty.Register(
"X",
typeof(double),
typeof(TextBubble),
new PropertyMetadata(new PropertyChangedCallback(OnXCoordChanged)));
public static readonly DependencyProperty YCoordProperty = DependencyProperty.Register(
"Y",
typeof(double),
typeof(TextBubble),
new PropertyMetadata(new PropertyChangedCallback(OnYCoordChanged)));
public string Text {
get { return (string)this.GetValue(TextProperty); }
set { base.SetValue(TextProperty, value); }
}
public int Diameter {
get { return (int)this.GetValue(DiameterProperty); }
set { base.SetValue(DiameterProperty, value); }
}
public Brush Fill {
get { return (Brush)this.GetValue(FillProperty); }
set { base.SetValue(FillProperty, value); }
}
public double X {
get { return (double)this.GetValue(XCoordProperty); }
set { base.SetValue(XCoordProperty, value); }
}
public double Y {
get { return (double)this.GetValue(YCoordProperty); }
set { base.SetValue(YCoordProperty, value); }
}
}
Generic.xml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:X4S.Controls" >
<Style TargetType="local:TextBubble">
<Setter Property="Text" Value="ABC" />
<Setter Property="Diameter" Value="50" />
<Setter Property="X" Value="100.0" />
<Setter Property="Y" Value="100.0" />
<Setter Property="Fill" Value="GhostWhite" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:TextBubble">
<Grid x:Name="LayoutRoot" Canvas.Top="{TemplateBinding Y}" Canvas.Left="{TemplateBinding X}" >
<Ellipse Fill="{TemplateBinding Fill}" Width="{TemplateBinding Diameter}" Height="{TemplateBinding Diameter}"/>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{TemplateBinding Text}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Got it!
向画布子树添加新的自定义控件时,我们得到了这个额外的东西(TextBubble),它们围绕着网格 - 文本块 - 椭圆束。那么,TextBubble对象从来没有设置它的位置或大小。因此,即使Text和Fill被应用于内部控件,这些属性也不受TextBubble的位置或大小的影响。
一旦我们将相同的位置和大小属性应用到TextBubble本身 - 它真的奏效了。
的关键是这样的:。 ((控制)canvas.GetDescendant())GetDescendant()的GetValue(Canvas.LeftProperty) 200.0
这表明网格确实有它的位置设置 - 但它的父母TextBubble没有。
的修复:
private static void OnDiameterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
TextBubble textBubble = d as TextBubble;
textBubble.Width = (int)e.NewValue;
textBubble.Height = (int)e.NewValue;
}
private static void OnLeftChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
TextBubble textBubble = d as TextBubble;
textBubble.SetValue(Canvas.LeftProperty, e.NewValue);
}
private static void OnTopChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
TextBubble textBubble = d as TextBubble;
textBubble.SetValue(Canvas.TopProperty, e.NewValue);
}
有可能是一个更好的方法(我会研究它),但是这个工作。
你试过在你的自定义控件的构造函数中:this.DataContext = this; – 2011-10-05 00:04:13
我没有尝试this.DataContext = this;虽然没有帮助。 –
这里不需要代码,你的控件的模板可以很好地处理它 - 见我的答案。 – 2011-10-05 10:44:34