0

I have a very simple XAML Visibility="Collapsed" X1="1" Margin="-35 0 0 0" Y1="0.4">

 <Label.Content> <Slider Grid.Column="0" Width="20" Height="65" IsDirectionReversed="True" Maximum="0.1" Minimum="-4" Orientation="Vertical" x:Name="Slider1" Value="{Binding Source={x:Reference scaleFactorModifier}, Path=ZoomScaleFactor, Mode=TwoWay}" /> </Label.Content> </Label> </SciChart:CustomAnnotation.Content> </SciChart:CustomAnnotation> 

Now for some reason I need to set the CustomControl.Content property from code behind. Is there any possibility I move all the label control to some style and template and set the CustomControl content property at runtime with that particular style or template.

Update

Reason for using Code behind

Actually I have Annotations property in my control which could have any control in it as we required. Previously I had used hard coded annotations in my control and placed the controls manually. Now I want to bind the Annotations property. I could create a property of this type and add CustomAnnotation objects in it. But customAnnotation objects need to have labels and other controls in them, how could I do that?

2
  • 1
    Probably a XY problem. Why do you want to set something from code-behind? Commented Jan 30, 2015 at 8:48
  • @dymanoid Hi, I got your point and update my question and mentioning the X instead of asking Y. Please take a look. Commented Jan 30, 2015 at 11:42

2 Answers 2

1

If I have understood your problem correctly, I believe that you can do what you want by using a DataTemplate and a ContentControl. First, define a DataTemplate with your Label in:

<DataTemplate DataType="{x:Type YourPrefix:YourDataType}"> <!-- define your Label here --> </DataTemplate> 

Then you can set the Content property of your CustomControl to a ContentControl that has its own Content property set to an instance of an object of type YourDataType:

<ContentControl Content="{Binding InstanceOfYourDataType}" /> 

I'm aware that you want to do this programmatically, but that's easy enough to work out:

ContentControl contentControl = new ContentControl(); contentControl.Content = instanceOfYourDataType; yourCustomControl.Content = contentControl; 

I'm wondering if you even really need to use your CustomControl at all, but I'll leave that up to you.

Sign up to request clarification or add additional context in comments.

Comments

0

I create a user control from that xaml and then set the CustomControl.Content as new instance of user control. This might not be the best solution, but this is all that I have for now.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.