2011-10-04 49 views
0

部落,帆布子不移位

我已经构建了一个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); 
} 

有可能是一个更好的方法(我会研究它),但是这个工作。

+0

你试过在你的自定义控件的构造函数中:this.DataContext = this; – 2011-10-05 00:04:13

+0

我没有尝试this.DataContext = this;虽然没有帮助。 –

+0

这里不需要代码,你的控件的模板可以很好地处理它 - 见我的答案。 – 2011-10-05 10:44:34

回答

0

下面是工作解决方案,我刚刚检查过它。不过,你有两个选择: 1.设置的DataContext在构造和使用常规绑定 2.不要设置的DataContext并使用的RelativeSource作为源(请参见下面的XAML)

这不完全明显的画布。左& Canvas.Top应该设置为canvas的直接子节点,它是本地的:TextBubble。

<Style TargetType="local:TextBubble"> 
     <Setter Property="Text" Value="ABC" /> 
     <Setter Property="Diameter" Value="50" /> 


     <Setter Property="Canvas.Top" 
       Value="{Binding Path=Y, RelativeSource={RelativeSource Self}}" /> 
     <Setter Property="Canvas.Left" 
       Value="{Binding Path=X, RelativeSource={RelativeSource Self}}" /> 

     <Setter Property="Fill" Value="GhostWhite" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="local:TextBubble"> 
        <Grid x:Name="LayoutRoot" > 
         <Ellipse Fill="{TemplateBinding Fill}" Width="{TemplateBinding Diameter}" Height="{TemplateBinding Diameter}"/> 
         <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{TemplateBinding Text}"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
+0

显然,FindAncestor和AncestorType在Silverlight 4.0中不可用。谢谢你的尝试。我会继续放弃这种方法,因为笨重的方法可行,但很丑。 –

+0

奇怪的是,文本和填充属性工作并应用于文本块和椭圆。但直径和X,Y值不是。是否有某种“更新你的懒狗”命令让画布更新? –