5

Statusという DependencyProperty を持つSmartFormという UserControl があります。

私の Window1.xaml には、要素があります<local:SmartForm Status="Ready"/>

その場合、SmartForm オブジェクトのコンストラクターでは、 Status は "Ready" と等しくなりますが、代わりにnullと等しくなると思います。

それでは、 SmartForm のコンストラクターでStatus プロパティの値が NULL になるのはなぜですか?

UserControl コンストラクターにない場合、いつ value にアクセスできますか?

Window1.xaml:

<Window x:Class="TestPropertyDefine23282.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestPropertyDefine23282"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <local:SmartForm Status="Ready"/>
    </Grid>
</Window>

SmartForm.xaml:

<UserControl x:Class="TestPropertyDefine23282.SmartForm"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid>
        <TextBlock x:Name="TestingMessage"/>
    </Grid>
</UserControl>

SmartForm.xaml.cs:

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

namespace TestPropertyDefine23282
{
    public partial class SmartForm : UserControl
    {
        public SmartForm()
        {
            InitializeComponent();

            TestingMessage.Text = Status; //WHY IS STATUS NOT YET SET HERE?

        }

        #region DependencyProperty: Status
        public string Status
        {
            get
            {
                return (string)GetValue(StatusProperty);
            }
            set
            {
                SetValue(StatusProperty, value);
            }
        }

        public static readonly DependencyProperty StatusProperty =
            DependencyProperty.Register("Status", typeof(string), typeof(SmartForm),
            new FrameworkPropertyMetadata());
        #endregion

    }
}
4

4 に答える 4

4
<local:SmartForm Status="Ready"/>

翻訳先:

SmartForm f = new SmartForm();
f.Status = Status.Ready;

セッターが呼び出されると、その値にアクセスできます。

于 2009-05-25T14:57:17.303 に答える
3

そのテスト メッセージを次のように設定できます。

...
    public static readonly DependencyProperty StatusProperty = 
        DependencyProperty.Register("Status", typeof(string), typeof(SmartForm),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.None,
            new PropertyChangedCallback(OnStatusChanged)));

    public static void OnStatusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        ((SmartForm)d).TestingMessage.Text = e.NewValue.ToString();
    }
...

または次のように:

<UserControl 
x:Class="TestPropertyDefine23282.SmartForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestPropertyDefine23282"
Height="300" Width="300"
>
<Grid>
    <TextBlock
        x:Name="TestingMessage"
        Text="{Binding Path=Status, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:SmartForm}}}"
        />
</Grid>
</UserControl>
于 2009-05-25T14:30:21.227 に答える
1

Szymon Rozga はこの問題を素晴らしい方法で説明しました。パラメータを設定する前に確認しますが、コンストラクタが初期化された後に確認します。

適切な解決策は、代わりに次のように loaded イベントを使用することです。

(未テスト)

    public SmartForm()
    {
        InitializeComponent();

        Loaded += (sender, args) =>
        {
            TestingMessage.Text = Status; 
        };
    }
于 2016-12-16T15:45:50.167 に答える
-1

これは一種の 3 次ですが、なぜこのセッターが必要なのですか?

<UserControl x:Class="TestPropertyDefine23282.SmartForm"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Control"
    Height="300" Width="300">
    <Grid>
        <TextBlock Text="{Binding Path=Status, ElementName=Control}" />
    </Grid>
</UserControl>
于 2009-05-26T06:37:05.757 に答える