質問

I created a UserControl with a Label and a Rectangle inside 2 grid rows. I added the property

public string SetText
{
    get
    {
        return (string)GetValue(mLabel.ContentProperty);
    }
    set
    {
        SetValue(mLabel.ContentProperty, value);
    }
}

Usage of the property

<local:PlayerMiniImage SetText="Player 1" ...

When I used the property, the font of the label changed and the rectangle disappeared Have any idea?

役に立ちましたか?

解決

If you define a UserControl...

<UserControl x:Class="...">
    <Border>
        <!-- ... -->
    </Border>
</UserControl>

Then everything inside it, here a Border, is the Content, hence if you set the ContentProperty everything will be replaced.


To set the label content create a new DP:

public static readonly DependencyProperty LabelContentProperty =
    DependencyProperty.Register("LabelContent", typeof(object), typeof(MyUserControl), new UIPropertyMetadata(null));
public object LabelContent
{
    get { return (object)GetValue(LabelContentProperty); }
    set { SetValue(LabelContentProperty, value); }
}

and bind the label to it:

<Label Content="{Binding LabelContent, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top