views:

222

answers:

2

In the following Silverlight application why does the property OuterPadding not change the padding in the outer border, although the TextBlock correctly displays the value of OuterPadding? If I change the Border padding to a simple integer it the padding works fine, but not when it is defined by the property in code behind.

This same code works fine in WPF.

XAML:

<UserControl x:Class="Test222.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:pages="clr-namespace:Test222.Pages"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    Width="600" Height="480">
    <Border Background="#eee" Padding="{Binding OuterPadding}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="34"/>
                <RowDefinition Height="426"/>
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0" Grid.Column="0">
                <StackPanel x:Name="QuickMenu" Orientation="Horizontal"/>
            </StackPanel>
            <Border Grid.Row="1" Grid.Column="0"
                Background="#fff"
                Padding="10" 
                Width="580"
                Height="426"
                VerticalAlignment="Top"
                CornerRadius="5">
                <TextBlock Text="{Binding OuterPadding}"/>
            </Border>
        </Grid>
    </Border>
</UserControl>

Code Behind:

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace Test222
{
    public partial class MainPage : UserControl, INotifyPropertyChanged
    {

        #region ViewModelProperty: OuterPadding
        private int _outerPadding;
        public int OuterPadding
        {
            get
            {
                return _outerPadding;
            }

            set
            {
                _outerPadding = value;
                OnPropertyChanged("OuterPadding");
            }
        }
        #endregion

        public MainPage()
        {
            InitializeComponent();
            DataContext = this;

            RefreshApplication();
        }

        void RefreshApplication()
        {
            OuterPadding = 5;


            for (int i = 0; i < 5; i++)
            {
                var button = new Button();
                button.Content = "Button " + i;
                button.Margin = new Thickness { Right = 3 };
                QuickMenu.Children.Add(button);
            }
        }


        #region INotifyPropertyChanged Member

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

    }
}
+1  A: 

If I had to guess I'd say its because the value converter for Thickness does not handle Int32 -> Thickness conversion. What happens if you make OuterPadding a Thickness instead of int?

EDIT Just checked Reflector and it seems ThicknessConverter is hard coded to handle conversion from either String or Double to Thickness, but not Int32.

I misunderstood what I saw in Reflector. It looks like it should handle Int32 ok.

Josh Einstein
+2  A: 

An int works when defining the value in xaml, but when you do it in code it doesn't. But if you change your property from an int to a ThickNess it works fine.

Henrik Söderlund
Using an int in code works in WPF, but apparently not in Silverlight (3). Thickness works in both. Funny that using an int in code in Silverlight was actually crashing Visual Studio (2008) on one machine.
Edward Tanguay