为什么 TextBlock 可以显示代码隐藏属性值,但 border/padding 却无法使用它?

发布于 2024-08-19 06:34:28 字数 3173 浏览 2 评论 0原文

在以下 Silverlight 应用程序中尽管 TextBlock 正确显示了 OuterPadding 的值,但为什么属性 OuterPadding 不更改外边框中的填充?如果我将边框填充更改为简单整数,则填充会起作用很好,但当它是由后面的代码中的属性定义时就不行了。

相同的代码在 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>

代码隐藏:

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

    }
}

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

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℉服软 2024-08-26 06:34:28

在 xaml 中定义值时 int 可以工作,但在代码中定义值时则不行。但如果你将属性从 int 更改为 ThickNess ,它就可以正常工作。

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.

生来就爱笑 2024-08-26 06:34:28

如果我不得不猜测,我会说它是因为厚度的值转换器不处理 Int32 ->厚度换算。如果将 OuterPadding 设置为 Thickness 而不是 int 会发生什么?

编辑刚刚检查了Reflector,似乎ThicknessConverter被硬编码来处理从String或Double到Thickness的转换,但不是Int32。

我误解了我在Reflector中看到的内容。看起来它应该可以正常处理 Int32 。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文