Note that there are some explanatory texts on larger screens.

plurals
  1. POWP7: Binding problems with UserControl
    primarykey
    data
    text
    <p>As I'm developing my app, I'm finding that I'm re-creating a "tile" control far too often. Therefore I'm currently trying to move it into a User Control for re-use. However, it's currently not accepting any bindings that were previously working. So for example:</p> <pre class="lang-xml prettyprint-override"><code>&lt;Canvas Height="73" Width="73" VerticalAlignment="Top" Margin="10,10,8,0"&gt; &lt;Rectangle Height="73" Width="73" VerticalAlignment="Top" Fill="{Binding Path=Active, Converter={StaticResource IconBackground}}" /&gt; &lt;Image Height="61" Width="61" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="6" Source="{Binding Tone.Image}" /&gt; &lt;/Canvas&gt; </code></pre> <p>Works fine with the bindings,</p> <pre class="lang-xml prettyprint-override"><code>&lt;views:Tile Height="73" Width="73" Background="{Binding Path=Active, Converter={StaticResource IconBackground}, Mode=OneWay}" Icon="{Binding Path=Tone.Image, Mode=OneTime}" /&gt; </code></pre> <p>produces the error "the parameter is incorrect".</p> <p>Here is the code for my Tile UserControl:</p> <p><strong>Tile.xaml</strong></p> <pre class="lang-xml prettyprint-override"><code>&lt;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"&gt; &lt;Grid x:Name="LayoutRoot"&gt; &lt;Canvas Height="100" Width="100" Margin="0,0,0,0"&gt; &lt;Rectangle Name="rectBackground" Height="100" Width="100" /&gt; &lt;Image Name="imgIcon" Height="80" Width="80" VerticalAlignment="Center" HorizontalAlignment="Center" Canvas.Left="10" Canvas.Top="10" /&gt; &lt;/Canvas&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p><strong>Tile.xaml.cs</strong></p> <pre class="lang-cs prettyprint-override"><code>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; } } } } </code></pre> <p>If you need any more source, let me know and I'll post it. I don't have any problems when using a fixed value (<code>Height</code> and <code>Width</code> are fine, and if I set <code>Background</code> to Red then that also works fine), but changing to a Binding value throws the exception.</p> <hr> <p><strong>EDIT 1</strong></p> <p>Here's some updated code:</p> <p><strong>Tile.xaml.cs</strong></p> <pre class="lang-cs prettyprint-override"><code>#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 </code></pre> <p><strong>MainMenuControl.xaml.cs</strong></p> <pre class="lang-cs prettyprint-override"><code>// 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; } } } </code></pre> <p>I'm also comparing the two methods side-by-side. The tile on the left is the defined Canvas with bindings fully working, while the tile on the right is the Tile UserControl, which only works with defined colours (Blue in this case)</p> <p><img src="https://i.stack.imgur.com/okqVW.png" alt="Screenshot"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload