views:

794

answers:

1

List item

I have defined a style in app.xaml. This style contains several text TextBlocks which I would like to controle as I apply the style to an object, in this case a UserPin. How can I access these TextBlocks runtime? I get the style by:

Style = Application.Current.Resources["UserPin"] as Style;

The style looks like this:

<Style x:Name="UserPin" TargetType="RRML_UserControls:UserPin" >
  <Setter Property="RenderTransformOrigin" Value="0.5,0.5" />
  <Setter Property="AnchorPoint" Value="0.5,0.5" />
  <Setter Property="Template">
   <Setter.Value>
    <ControlTemplate TargetType="RRML_UserControls:UserPin">
     <Grid Height="71.969" Width="Auto">
      <Grid.RenderTransform>
       <ScaleTransform x:Name="PART_PinScale" />
      </Grid.RenderTransform>
      <Grid.RowDefinitions>
       <RowDefinition Height="29"/>
       <RowDefinition Height="16"/>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
       <ColumnDefinition Width="0.247*"/>
       <ColumnDefinition Width="20"/>
       <ColumnDefinition Width="0.753*"/>
      </Grid.ColumnDefinitions>
      <Image Height="Auto" Source="Resources/Users.png" x:Name="PART_imgUser" VerticalAlignment="Top" Stretch="Uniform" Margin="0,0,0,0" Grid.Column="1">
       <Image.RenderTransform>
        <TransformGroup>
         <ScaleTransform/>
         <SkewTransform/>
         <RotateTransform/>
         <TranslateTransform/>
        </TransformGroup>
       </Image.RenderTransform>
      </Image>
      <TextBlock HorizontalAlignment="Center" Margin="0,0,0,0" Width="Auto" Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Center" TextAlignment="Center" x:Name="txtBottom" Text="{Binding Mode=OneWay, Path=LocationName}">
       <TextBlock.DataContext>
        <RRML_RRMLServiceReference:Location LocationName="Initial Name"/>
       </TextBlock.DataContext>
      </TextBlock>       
      <TextBlock HorizontalAlignment="Right" Margin="0,0,0,0" VerticalAlignment="Center" Text="L" TextWrapping="Wrap"/>
      <TextBlock Margin="0,0,0,0" Text="R" TextWrapping="Wrap" d:LayoutOverrides="Width, Height" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Center"/>       
     </Grid>
    </ControlTemplate>
   </Setter.Value>
     </Setter>
    </Style>

The TextBlock value I'm trying to set is 'txtBottom'. As you can see I have tried to apply a datacontext and a databinding to the field. This works, but all objects get the value "Initial Name" of course.

My questions are:

  1. how can I apply my datacontext so txtBottom.Text changes, or
  2. how can I change the value of the TextBlock named txtBottom without databinding?
  3. in short can I access these fields or properties at all?

Runtime :) So fare I have found that Triggers may be used only in WPF.

I think of somethning like this:

var styledobject = new NiceObject();
styledobject.Style = Application.Current.Resources["UserPin"] as Style;
styledobject.DataContext = locationData;

Where locationData is my object containing data.

If anyone wonders; I am placing icons on a map and want to name them.

/Øyvind

A: 
  1. You should not explicitly apply DataContext on the TextBlock. DataContext is inherited by child FrameworkElements. You should try to set data context explicitly as little and as high up the Visual Tree as possible (for your own sanity's sake :-))
  2. If this is a custom control, you can override on the OnApplyTemplate method and use the GetTemplateChild(string name) to retrieve references to named elements within your control.

    public override void OnApplyTemplate() { base.OnApplyTemplate();

    TextBlock txtBottom = GetTemplateChild("txtBottom") as TextBlock; }

  3. Externally, if you must, you can imperatively access that specific control at runtime using an extension method to traverse the Visual Tree to find it by name.

    public static T FindChild(this DependencyObject element, string name) where T : FrameworkElement { //Code to find the control }

markti
Thanks for the answer.Alternative 2 solved the problem. I found that the key here is OnApplyTemplate due to what I found under Remarks in Help:"Call GetTemplateChild in order to return references to objects that come from the template after it is instantiated."/Øyvind
Øyvind