質問

私はWPFプロジェクトに取り組んでいますが、私の意図は、2つの特定のラジオバットンを別の指定されたコンポーネントのプロパティを変更することです。しかし今のところ、私はただラジオボタンに文字列を保存しようとしています。

そのために、私は行動クラスを作成しました:

    public class AdjustBehavior : Behavior<RadioButton>
{

このプロパティで:

        public static DependencyProperty AdjustLabelContentProperty =
        DependencyProperty.RegisterAttached("LabelContent", typeof(String), typeof(AdjustBehavior),
            new FrameworkPropertyMetadata(null,
    FrameworkPropertyMetadataOptions.Inherits));

そして、これらのゲッターとセッター:

       public static String GetLabelContent(RadioButton tb)
    {
        return (String)tb.GetValue(AdjustLabelContentProperty);
    }

    public static void SetLabelContent(RadioButton tb, String value)
    {
        tb.SetValue(AdjustLabelContentProperty, value);
    }

XAML側で、私はこれをしました:

    <RadioButton Content="Banana" Height="16" HorizontalAlignment="Left" Margin="30,216,0,0" Name="radioButton1" VerticalAlignment="Top" GroupName="a" IsThreeState="False" IsChecked="True" Checked="radioButton1_Checked" >
        <int:Interaction.Behaviors>
            <i:AdjustBehavior LabelContent="Apple" />
        </int:Interaction.Behaviors>
    </RadioButton>

ここで、int:interaction.behaviorsの名前空間とi:は、Adustbehaviorクラスの名前空間です。しかし、アプリケーションを開始するたびに、LabelContentはnullに設定されます。なんで?

私はそれが問題ではないと思うので、私は自分の行動クラスの残りの部分を投稿しませんでしたが、必要に応じてやります。

前もって感謝します。

クラーク

役に立ちましたか?

解決

添付のプロパティでは、ターゲットを添付する必要があります。あなたの場合、そのターゲットはラジオボタンであるため、使用する必要があります

<RadioButton i:AdjustBehavior.LabelContent="Apple" ... />

AdustBehaviorのプロパティを作成する必要がある場合は、添付されていない通常の依存関係プロパティを使用します。

他のヒント

RegisterAttachedではなく、DependencyProperty.registerを使用する必要があります。これは、添付のプロパティとしてではなく、標準的な依存関係プロパティとして使用されています。

LabelContentは、RadioButtonに接続されたプロパティまたはAdustBehaviorの依存関係プロパティのいずれかである必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top