2012-05-03 81 views
0

當我開發我的應用程序時,我發現我經常重新創建「tile」控件。因此,我正在嘗試將其移入用戶控件以供重新使用。但是,它目前不接受之前工作的任何綁定。因此,例如:WP7:UserControl的綁定問題

​​

正常工作與綁定,

<views:Tile Height="73" Width="73" Background="{Binding Path=Active, Converter={StaticResource IconBackground}, Mode=OneWay}" Icon="{Binding Path=Tone.Image, Mode=OneTime}" /> 

產生錯誤 「的參數不正確」。

這裏是我的瓷磚用戶控件的代碼:

Tile.xaml

<UserControl x:Class="RSS_Alarm.Views.Tile" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}" 
    FontSize="{StaticResource PhoneFontSizeNormal}" 
    Foreground="{StaticResource PhoneForegroundBrush}" 
    d:DesignHeight="100" d:DesignWidth="100"> 

    <Grid x:Name="LayoutRoot"> 
     <Canvas Height="100" Width="100" Margin="0,0,0,0"> 
      <Rectangle Name="rectBackground" Height="100" Width="100" /> 
      <Image Name="imgIcon" Height="80" Width="80" VerticalAlignment="Center" HorizontalAlignment="Center" Canvas.Left="10" Canvas.Top="10" /> 
     </Canvas> 
    </Grid> 
</UserControl> 

Tile.xaml.cs

namespace RSS_Alarm.Views 
{ 
    public partial class Tile : UserControl 
    { 
     public Tile() 
     { 
      InitializeComponent(); 
     } 

     public String Icon 
     { 
      get 
      { 
       return imgIcon.Source.ToString(); 
      } 

      set 
      { 
       BitmapImage alarmIcon = new BitmapImage(); 
       alarmIcon.UriSource = new Uri(value, UriKind.Relative); 
       imgIcon.Source = alarmIcon; 
      } 

     } 

     new public Brush Background 
     { 
      get 
      { 
       return rectBackground.Fill; 
      } 

      set 
      { 
       rectBackground.Fill = value; 
      } 
     } 

     new public double Height 
     { 
      get 
      { 
       return rectBackground.Height; 
      } 

      set 
      { 
       rectBackground.Height = value; 
       imgIcon.Height = value * 0.8; 
      } 
     } 

     new public double Width 
     { 
      get 
      { 
       return rectBackground.Width; 
      } 

      set 
      { 
       rectBackground.Width = value; 
       imgIcon.Width = value * 0.8; 
      } 
     } 
    } 
} 

如果您需要更多的來源,讓我知道,我會發布它。使用固定值時,我沒有任何問題(HeightWidth很好,如果我將Background設置爲紅色,那麼也可以正常工作),但更改爲綁定值會引發異常。


編輯1

下面是一些更新的代碼:

Tile.xaml.cs

#region Background 
     public static readonly DependencyProperty RectBackgroundProperty = 
      DependencyProperty.Register(
       "RectBackground", 
       typeof(SolidColorBrush), 
       typeof(Tile), 
       new PropertyMetadata(new SolidColorBrush(Colors.Green), new PropertyChangedCallback(OnBackgroundChanged)) 
      ); 

     public static void OnBackgroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      Debug.WriteLine("Filling background"); 
      ((Tile)d).rectBackground.Fill = (Brush)e.NewValue; 
     } 

     new public SolidColorBrush Background 
     { 
      get { return (SolidColorBrush)GetValue(RectBackgroundProperty); } 
      set { 
       Debug.WriteLine("Setting colour"); 
       SetValue(RectBackgroundProperty, value); 
      } 
     } 
#endregion 

MainMenuControl.xaml.cs

// Class to determine the background colour of the icon (active/inactive) 
public class IconBackground : System.Windows.Data.IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     bool b = (bool)value; 

     Debug.WriteLine("Converting colour. Value is " + b.ToString()); 

     if (b) 
     { 
      return (Brush)App.Current.Resources["PhoneAccentBrush"]; 
     } 
     else 
     { 
      return new SolidColorBrush(Colors.DarkGray); 
     } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     SolidColorBrush brush = (SolidColorBrush)value; 

     if (brush.Color.Equals(Colors.DarkGray)) 
     { 
      return false; 
     } 
     else 
     { 
      return true; 
     } 
    } 

} 

我還在並排比較這兩種方法。左邊的瓦片是定義畫布綁定的全面工作,而右邊的瓷磚是瓷磚用戶控件,它只能與定義的顏色作品(藍在這種情況下)

Screenshot

回答

2

爲了能夠綁定在XAML中是不夠的,使一個屬性。您必須創建一個DependencyProperty

您的Background綁定工作原因是UserControl本身具有此屬性。如果你在你的Background屬性設置器中設置了一個斷點,你會發現它永遠不會被調用。

這裏是一個DependencyProperty爲您Background(未測試)

#region Background 
     public const string BackgroundPropertyName = "Background"; 
     public new Brush Background 
     { 
      get { return (Background)GetValue (BackgroundProperty); } 
      set { SetValue (Background, value); } 
     } 
     public static new readonly DependencyProperty BackgroundProperty = DependencyProperty.Register (
      BackgroundPropertyName, 
      typeof (Brush), 
      typeof (Tile), 
      new PropertyMetadata (BackgroundChanged)); 

     static void BackgroundChanged (DependencyObject d, DependencyPropertyChangedEventArgs e) 
     {     
      ((Tile) d).rectBackground = (Brush)e.NewValue;  
     } 
    #endregion 
+0

您可以在此推薦任何教程的例子嗎?到目前爲止,我嘗試實現這一目標一直沒有成功。 –

+0

請參閱編輯,其他屬性只需更改名稱和類型 – thumbmunkeys

+0

它適用於圖標。仍然試圖讓它與背景一起工作。 –