簡體   English   中英

datatemplate中的datacontext問題

[英]Issue with datacontext in datatemplate

我有一個ContentControl ,它具有一組觸發器,當ContentControl的Content屬性擊中一個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>

這個ContentControl可以正常工作,它將在TextBox和自定義的UserControl之間切換。 問題是我無法將ViewModel中的屬性綁定到DataTemplate定義的TextBox的屬性Text

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

我還嘗試過在堆棧面板上將DataContext顯式設置為我的viewmodel,也沒有運氣。

我已經測試了ResourceDictionary之外的綁定,它可以按預期工作。

有什么建議么?

嘗試:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM