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 Outer开发者_StackOverflow中文版Padding? 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
}
}
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.
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.
精彩评论