简体   繁体   中英

Issue with datacontext in datatemplate

I have a ContentControl which has a set of Triggers, which will be fired when the Content Property of the ContentControl, hits one of the Style.Triggers

   <ContentControl  Content="{Binding SelectedSearchQueryParameter.DataType}" Grid.Row="0" VerticalContentAlignment="Center" Height="25"  MaxWidth="200">
     <ContentControl.Style>
           <Style TargetType="{x:Type ContentControl}">
                <Setter Property="ContentTemplate" Value="{StaticResource TextBoxTemplate}" />
                   <Style.Triggers>
                      <DataTrigger Binding="{Binding SelectedSearchQueryParameter.DataType}" Value="{x:Type sys:DateTime}">
                         <Setter Property="ContentTemplate" Value="{StaticResource DateTimeTemplate}" />
                      </DataTrigger>
                   </Style.Triggers>
           </Style>
      </ContentControl.Style>
    </ContentControl>

This ContentControl works as expect, it will switch between a TextBox and a custom made UserControl . The issue is that I'm unable to bind to a property in my ViewModel to the Property Text in the TextBox defined in the DataTemplate

<DataTemplate x:Key="TextBoxTemplate">
   <StackPanel>
     <TextBox HorizontalAlignment="Stretch" MinWidth="200" Text="{Binding SearchQuery, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"/>
   </StackPanel>
</DataTemplate>

I've also tried to explicity set the DataContext to my viewmodel on the stackpanel, with no luck either.

I've tested the binding outside of the ResourceDictionary , and it works as expected.

Any suggestions?

尝试:

Text="{Binding Path=DataContext.SearchQuery, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM