0

FixedDocument に入れたいユーザー コントロールがありますが、その前にラベルのテキストを変更する必要があります。依存関係プロパティを使用する必要があると思います。

簡略化された XAML を次に示します。

<UserControl x:Class="PrinterTest.TestControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <Label Content="{Binding LabelCaption}"
               Height="24" HorizontalContentAlignment="Right" Name="lblCaption"     
               Width="140" />
    </Grid>
</UserControl>

そしてコードビハインド

public partial class TestControl : UserControl
{
    public TestControl()
    {
        InitializeComponent();
    }

    public readonly static DependencyProperty 
        LabelCaptionDP = DependencyProperty.Register("LabelCaption",
                                                     typeof(string), 
                                                     typeof(TestControl),
                                                     new FrameworkPropertyMetadata("no data"));

    public string LabelCaption
    {
        get { return (string)GetValue(LabelCaptionDP); }
        set { SetValue(LabelCaptionDP, value); }
    }

呼び出しビットでインスタンス化しますTestControl myControl = new TestControl();

コントロールの新しいコピーのプロパティにアクセスできないため、何が間違っていますか? ありがとうございました!

4

1 に答える 1

2

に変更LabelCaptionDPLabelCaptionPropertyます。

依存関係プロパティの概要から:

プロパティとそのバッキング DependencyProperty フィールドの命名規則は重要です。フィールドの名前は、常にプロパティの名前に接尾辞 Property を追加したものです。この規則とその理由の詳細については、「カスタム依存関係プロパティ」を参照してください。

カスタム依存関係プロパティの依存関係プロパティの命名規則についてお読みください。

于 2013-05-06T18:12:13.007 に答える