tags:

views:

159

answers:

1

I have a xaml file with this code:

 <GridViewColumn 
                            x:Name="lvCol3"
                            Header="Quantità"
                            Width="120"
                        >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Control x:Name="host">
                                    <Control.Template>
                                        <ControlTemplate>
                                            <TextBlock
                                                Text="{Binding Path=Entity.Quantita}"
                                            />
                                        </ControlTemplate>
                                    </Control.Template>
                                </Control>

                                <DataTemplate.Triggers>
                                    <DataTrigger
                                        Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"
                                        Value="True"
                                    >
                                        <Setter TargetName="host" Property="Template">
                                            <Setter.Value>
                                                <ControlTemplate x:Name="myControlTemplate" />
                                            </Setter.Value>
                                        </Setter>
                                    </DataTrigger>
                                </DataTemplate.Triggers>

                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

I would manage "myControlTemplate" from code behind in order to assign different UI object.

I try to use FindResource but it don't work. How can I do?

A: 

Probably the easiest way is to extract your ControlTemplate to a resource: then you can use FindResource.

Something like this:

<UserControl>
  <UserControl.Resources>
    <ControlTemplate x:Key="MyControlTemplate">
         <TextBlock
            Text="{Binding Path=Entity.Quantita}"/>
    </ControlTemplate>
  </UserControl.Resources>

  ...

  <GridViewColumn 
     x:Name="lvCol3"
     Header="Quantità"
     Width="120">
        <GridViewColumn.CellTemplate>
            <DataTemplate>
               <Control x:Name="host" Template="{StaticResouce MyControlTemplate}">   
               </Control>
        </GridViewColumn.CellTemplate>
   </GridViewColumn>

  ...
</UserControl>

Then in the code for your UserControl (or whatever is the root element) you can do

var resource = FindResource("MyControlTemplate") as ControlTemplate;
Samuel Jack