views:

30

answers:

3

Hello again. I'm working on a WPF project, and my intention is to make two specific RadioButtons alter properties of another specified Component. But for now, i'm just trying to store a String inside the RadioButton.

For that, I've created a behavior class:

    public class AdjustBehavior : Behavior<RadioButton>
{

With this property:

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

And these getters and setters:

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

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

On the XAML side, I did this:

    <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>

Where int: is the namespace to Interaction.Behaviors and i: is the namespace to the AdjustBehavior class. But whenever I start my application, LabelContent is set to null. Why?

I didn't post the rest of my Behavior class because I think it won't matter, but I'll do if necessary.

Thanks in Advance.

Clark

+1  A: 

You should use DependencyProperty.Register, not RegisterAttached. This isn't being used as an attached property, but rather a standard dependency property.

Reed Copsey
Thanks a lot that solved my problem
Conrad Clark
A: 

Attached property requires target to be attached to. In your case that target is radio button, so you should use

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

If you need to just create property of AdjustBehavior, use normal dependency property, not attached.

STO
Makes sense, thanks a lot! It's working now
Conrad Clark
A: 

LabelContent should be either an attached property on RadioButton or dependency property on AdjustBehavior .

Kai Wang