[英]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或xpath ”,这是第一个警告标志。 我将绑定更改为{Binding .}
起作用了(即使此MSDN文章明确指出它们是等效的)。 伏都教正在帮助的事实是第二个警告信号。 然后,它可以正确显示,并且检查状态是根据布尔值确定的,但是单击复选框(即使使用UpdateSourceTrigger=PropertyChanged
)时,无论我尝试了什么,它都拒绝更新绑定源。 使用diagnostics:PresentationTraceSources.TraceLevel=High
表明它甚至没有尝试绑定(第三个警告信号)。
我继续创建了一个简单的“盒子”的布尔值-一个叫单布尔属性的类Value
与INotifyPropertyChanged
实现。 我将绑定更改为{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.