zak
zak

Reputation: 193

WPF Prism how to access Parent Model From DataTemplate

For some DataTemplates in a view, I am trying to create some DataTriggers that change the property on a DataTemplate control.. based on a property in the parent class

The following pseudo code is not real the case nor code ..take to long to explain real case. DataTriggers are doing more than shown here

So within DataTemplate "OderValueDT" for "PreviousOrder" just need to bind\access to the parent property "OrderViewModel.AlertUser" from within OderValueDT. DataTrigger

OrderViewModel{
   Property bool AlertUser {get;}
   Property PreviousOrder OrderValue {get;}  
   .. 
}
PreviousOrder {
  Property string OrderComment {get;}
  .. 
}

<OrderView  ..> 
        <DataTemplate x:Key="OderValueDT">
            <Border
                <TextBlock
                    Name="PART_Comment"
                    Text="{Binding OrderComment, Mode=OneWay}"

                </TextBlock>
            </Border>
        </DataTemplate>
      <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=AlertUser, RelativeSource={RelativeSource TemplatedParent}}"  Value="True">
                    <Setter TargetName="PART_Comment" Property="Foreground" Value= "Red"/>
                </DataTrigger>
                </DataTrigger>
      </DataTemplate.Triggers>
    </DataTemplate>

Searches for solution have drawn a blank , most examples I have seen are to access Parent controls ,e.g.

{Binding Path=DataContext.Index, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}

but "AlertUser" is not a control

Upvotes: 0

Views: 42

Answers (1)

mm8
mm8

Reputation: 169370

You specify the type of the element which has the parent model as DataContext as the AncestorType and bind to its DataContext property, e.g.:

<DataTrigger Binding="{Binding DataContext.AlertUser,
     RelativeSource={RelativeSource AncestorType={x:Type local:OrderView}}" ... />

Upvotes: 0

Related Questions