簡體   English   中英

ControlTemplate中的DataTemplate不更新綁定

[英]DataTemplate in ControlTemplate not updating Binding

我創建了一個具有3個PART_的控件,其中一個PART_隨綁定類型而變化,但是控件中更改的值不會更新Binding,它似乎可以作為OneWay Binding工作。

我相信的這部分代碼是相關的:

<DataTemplate x:Key="BooleanDAView" DataType="{x:Type sys:Boolean}">
    <CheckBox IsChecked="{Binding ., Mode=TwoWay}"/>
</DataTemplate>
<DataTemplate x:Key="DateTimeDAView" DataType="{x:Type sys:DateTime}">
    <extToolkit:DateTimePicker Value="{Binding ., Mode=TwoWay}"/>
</DataTemplate>
<DataTemplate x:Key="Int32DAView"  DataType="{x:Type sys:Int32}">
    <extToolkit:IntegerUpDown Value="{Binding ., Mode=TwoWay}"/>
</DataTemplate>
<DataTemplate x:Key="StringDAView"  DataType="{x:Type sys:String}">
    <TextBox Text="{Binding ., Mode=TwoWay}"/>
</DataTemplate>

....

<ContentControl x:Name="PART_Content"
        Grid.Row="0" Grid.Column="1"
        Margin="{TemplateBinding Padding}"
        VerticalAlignment="Center"
        VerticalContentAlignment="Center"
        Content="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
        >
    <ContentControl.ContentTemplateSelector>
        <controls:TypeBasedDataTemplateSelector>
            <controls:TypeBasedDataTemplateSelector.Templates>
                <controls:TypedDictionary>
                    <sys:String x:Key="{x:Type sys:Boolean}">BooleanDAView</sys:String>
                    <sys:String x:Key="{x:Type sys:DateTime}">DateTimeDAView</sys:String>
                    <sys:String x:Key="{x:Type sys:Int32}">Int32DAView</sys:String>
                    <sys:String x:Key="{x:Type sys:String}">StringDAView</sys:String>
                </controls:TypedDictionary>
            </controls:TypeBasedDataTemplateSelector.Templates>
        </controls:TypeBasedDataTemplateSelector>
    </ContentControl.ContentTemplateSelector>
</ContentControl>

對於內容,我也嘗試過... RelativeSource={RelativeSource AncestorType=local:DABaseControl}但沒有更改。

如果DataTemplate綁定使用"{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"則模板一旦設置就不會更改。

還是有更好的方法來做到這一點?

謝謝

我剛剛遇到了同樣的問題,我想創建一個帶有DataType="{x:Type sys:Boolean}DataTemplate ,該DataTemplate只有一個復選框,但是一路上有很多警告標志告訴我這不是應該的方式完成。

首先,簡單的{Binding}將引發異常“ 雙向綁定需要path或xp​​ath ”,這是第一個警告標志。 我將綁定更改為{Binding .}起作用了(即使此MSDN文章明確指出它們是等效的)。 伏都教正在幫助的事實是第二個警告信號。 然后,它可以正確顯示,並且檢查狀態是根據布爾值確定的,但是單擊復選框(即使使用UpdateSourceTrigger=PropertyChanged )時,無論我嘗試了什么,它都拒絕更新綁定源。 使用diagnostics:PresentationTraceSources.TraceLevel=High表明它甚至沒有嘗試綁定(第三個警告信號)。

我繼續創建了一個簡單的“盒子”的布爾值-一個叫單布爾屬性的類ValueINotifyPropertyChanged實現。 我將綁定更改為{Binding Value} ,現在一切正常,包括雙向綁定。

結論:綁定似乎無法更新綁定對象本身,而只能更新該對象的屬性(這就是為什么{Binding}拋出異常,而根據HB的回答 ,更明確的{Binding .}抑制該異常)。 無論如何,創建ViewModel並創建針對它的模板的方法似乎不僅僅是一個設計准則,而是實際的技術要求。

實際上,我從來沒有使用過ContentTemplateSelector ,但是如果我不得不冒險猜測的話,我會說它要么不響應ContentControl.Content屬性上的PropertyChanged事件,要么您的Content綁定不正確。

您可以通過刪除ContentTemplateSelector並查看數據是否全部顯示來輕松檢查綁定是否正確。 如果是這樣,則您的綁定是正確的。 如果不是,那是不正確的,您需要修復它。

如果問題是ContentTemplateSelector ,那么我建議切換到DataTrigger ,后者根據Content確定要使用哪個ContentTemplate 這是我通常所做的,它使用一個僅返回typeof(value)的Converter。

 <Style TargetType="{x:Type ContentControl}">
     <Setter Property="ContentTemplate" Value="{StaticResource StringDAView}" />
     <Style.Triggers>
         <DataTrigger Binding="{Binding Converter={StaticResource ObjectToTypeConverter}" 
                      Value="{x:Type sys:Boolean">
             <Setter Property="ContentTemplate" Value="{StaticResource BooleanDAView}" />
         </DataTrigger>
         <DataTrigger Binding="{Binding Converter={StaticResource ObjectToTypeConverter}" 
                      Value="{x:Type DateTime">
             <Setter Property="ContentTemplate" Value="{StaticResource DateTimeDAView}" />
         </DataTrigger>
         <DataTrigger Binding="{Binding Converter={StaticResource ObjectToTypeConverter}" 
                      Value="{x:Type sys:Int32">
             <Setter Property="ContentTemplate" Value="{StaticResource Int32DAView}" />
         </DataTrigger>
     </Style.Triggers>
 </Style>

暫無
暫無

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

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