2009-05-26 63 views
0

我使用Visual Studio 2008的Silverlight中2.0的最新版本我有下面的代碼一個簡單的Silverlight用戶控件:刷新Silverlight的用戶控件通過XAML

public partial class SilverlightControl1 : UserControl 
{ 
    public SilverlightControl1() 
    { 
     InitializeComponent(); 
     this.Loaded += new RoutedEventHandler(SilverlightControl1_Loaded); 
     Composite = new Composite(); 
    } 

    void SilverlightControl1_Loaded(object sender, RoutedEventArgs e) 
    { 
     Composite.Width = this.Width/2.0; 
     Composite.Height = this.Height/2.0; 
     if (!this.LayoutRoot.Children.Contains(Composite)) 
      this.LayoutRoot.Children.Add(Composite); 
    } 

    public Composite Composite 
    { 
     get; 
     set; 
    } 
} 

public class Composite : ContentControl 
{ 
    private Grid grid; 
    private Canvas canvas; 

    public Composite() 
    { 
     if (grid == null) grid = new Grid(); 
     if (canvas == null) canvas = new Canvas(); 
     if (!grid.Children.Contains(canvas)) 
      grid.Children.Add(canvas); 
     Content = grid; 
     this.Loaded += new RoutedEventHandler(Composite_Loaded); 
    } 

    private Rectangle rectangle; 

    void Composite_Loaded(object sender, RoutedEventArgs e) 
    { 
     if (rectangle == null) rectangle = new Rectangle(); 
     Canvas.SetTop(rectangle, 0); 
     Canvas.SetLeft(rectangle, 0); 
     rectangle.Fill = new SolidColorBrush(Color); 
     rectangle.Width = Width; 
     rectangle.Height = Height; 
     if (!canvas.Children.Contains(rectangle)) 
      canvas.Children.Add(rectangle); 
    } 

    public Color Color 
    { 
     get; 
     set; 
    } 
} 

然後我在Silverlight應用程序使用此用戶控件,在頁面的XAML看起來像這樣:

<UserControl x:Class="SilverlightApplication1.Page" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:test="clr-namespace:SilverlightClassLibrary1;assembly=SilverlightClassLibrary1" 
    Width="400" Height="300"> 
    <Grid x:Name="LayoutRoot" Background="Green"> 
    <test:SilverlightControl1 Name="uControl1"> 
     <test:SilverlightControl1.Composite> 
     <test:Composite Color="Yellow"/> 
     </test:SilverlightControl1.Composite> 
    </test:SilverlightControl1> 
    </Grid> 
</UserControl> 

我的問題是:我有什麼代碼添加到上面,這樣通過改變「綜合色」到黃色之外的東西,打的返回按鈕, UserControl會自動刷新?正如代碼所示,刷新UserControl的唯一方法是通過移動VS2008 IDE中的Slider欄來改變Silverlight頁面的縮放百分比。一個側面的問題,儘管對上述問題不太重要,但是:如上面的代碼,爲什麼我不能更改LayoutRoot的「背景」顏色?如果我刪除我的UserControl它按預期工作。

回答

1

該解決方案是雙重的。首先要在LayoutUpdated事件而不是Loaded事件中進行更改,其次要訂閱PropertyMetadata的PropertyChangedCallback。這是完整的工作代碼:

public partial class SilverlightControl1 : UserControl 
    { 
    public SilverlightControl1() 
    { 
     InitializeComponent(); 
     this.LayoutUpdated += new EventHandler(SilverlightControl1_LayoutUpdated); 
     Composite = new Composite(); 
    } 

    void SilverlightControl1_LayoutUpdated(object sender, EventArgs e) 
    { 
     Composite.Width = this.Width/2.0; 
     Composite.Height = this.Height/2.0; 
     if (!this.LayoutRoot.Children.Contains(Composite)) this.LayoutRoot.Children.Add(Composite); 
    } 

    public Composite Composite 
    { 
     get; 
     set; 
    } 
    } 

    public class Composite : ContentControl 
    { 
    private Grid grid; 
    private Canvas canvas; 

    public Composite() 
    { 
     if (grid == null) grid = new Grid(); 
     if (canvas == null) canvas = new Canvas(); 
     if (!grid.Children.Contains(canvas)) grid.Children.Add(canvas); 
     Content = grid; 
     this.LayoutUpdated += new EventHandler(Composite_LayoutUpdated); 
    } 

    void Composite_LayoutUpdated(object sender, EventArgs e) 
    { 
     if (rectangle == null) rectangle = new Rectangle(); 
     Canvas.SetTop(rectangle, 0); 
     Canvas.SetLeft(rectangle, 0); 
     rectangle.Fill = new SolidColorBrush(Color); 

     rectangle.Width = Width; 
     rectangle.Height = Height; 
     if (!canvas.Children.Contains(rectangle)) canvas.Children.Add(rectangle); 
    } 

    public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(Composite), new PropertyMetadata(Colors.Red, new PropertyChangedCallback(OnColorPropertyChanged))); 

    private static void OnColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     Composite comp = (Composite)d; 
     comp.InvalidateArrange(); 
    } 

    private Rectangle rectangle; 

    public Color Color 
    { 
     get { return (Color)GetValue(ColorProperty); } 
     set { SetValue(ColorProperty, value); } 
    } 
    } 
0

我認爲你需要將複合變成依賴項屬性。可能會想爲Composite上的Color做同樣的事情,這樣你就可以綁定它。

+0

感謝您的回答,科雷爾!正如你所建議的那樣,我已經將Composite和Color屬性實現爲依賴屬性,但不幸的是,這對問題沒有任何影響! – 2009-05-26 14:36:11