在DataTemplate中不ControlTemplate中的绑定更新 [英] DataTemplate in ControlTemplate not updating Binding

查看:128
本文介绍了在DataTemplate中不ControlTemplate中的绑定更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了3 PART_s控制,取决于绑定到它的类型有一个PART_的变化,但数值在控制范围内变化不更新约束力,但它似乎单向绑定工作。

I've created a control with 3 PART_s, one PART_ changes depending on the type bound to it, however values changed within the Control do not update the Binding, it seems to work as OneWay Binding.

这里的code我beleive的一部分是相关的:

Here's part of the code I beleive is relevant:

<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 =本地:DABaseControl} ,但没有改变。

For Content I've also tried ... RelativeSource={RelativeSource AncestorType=local:DABaseControl} but no change.

如果在绑定DataTemplate中使用{绑定路径=内容的RelativeSource = {的RelativeSource TemplatedParent},模式=双向}模板不改变设置一次。

If the DataTemplate Binding use "{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" the template doesn't change once set.

还是有更好的方式来做到这一点?

Or is there a better way to do this?

感谢

推荐答案

我刚刚遇到了同样的问题,我想创建一个的DataTemplate 数据类型={X:键入SYS:布尔}。,只是有一个复选框,但也有沿途许多警示牌告诉我这是不是应该做的方式。

I just encountered the same problem, I wanted to create a DataTemplate with DataType="{x:Type sys:Boolean} that just had a checkbox. But there were many warning signs along the way telling me this isn't the way it should be done.

首先,的简单绑定{结合} 会抛出异常的双向绑定需要路径或XPath 的,这是第一个警告信号。我改变了绑定到 {结合} 这工作(即使的这个MSDN文章明确指出,他们是相当)。这一事实,巫术是帮助是第二次警告信号。然后,它显示正确的选中状态是根据布尔值,但点击该复选框(甚至与 UpdateSourceTrigger = PropertyChanged的),就拒绝时更新绑定源,无不管是我的尝试。使用诊断:presentationTraceSources.TraceLevel =高显示,它甚至没有尝试绑定回来(第三次警告标志)

At first, the simple binding of {Binding} would throw an exception "Two-way binding requires path or xpath", which was the first warning sign. I changed the binding to {Binding .} which worked (even though this MSDN article clearly states that they're equivalent). That fact that voodoo was helping was the second warning sign. It then displayed correctly and the checked state was according to the boolean value, but when clicking the checkbox (even with UpdateSourceTrigger=PropertyChanged), it refused to update the binding source, no matter what I tried. Using diagnostics:PresentationTraceSources.TraceLevel=High showed that it didn't even try to bind back (third warning sign).

我继续创建了一个简单的盒子的布尔值 - 与名为 INotifyPropertyChanged的<一个布尔属性的类/ code>的实施。我改变了绑定到 {绑定值} ,现在一切工作,包括双向绑定。

I went ahead and created a simple "box" for the bool value - a class with a single bool property named Value with anINotifyPropertyChanged implementation. I changed the binding to {Binding Value} and now everything worked, including two way binding.

结论:这似乎是一个结合不能更新绑定的对象本身,而只是属性的对象(这就是为什么 {结合} 抛出一个异常,而是更明确的 {结合} 燮presses该异常,根据的 HB的回答)。在任何情况下,建立一个视图模型并创建针对它似乎比仅仅设计准则要多,但是模板的方法的实际技术要求。

Conclusion: It seems a binding can't update the bound object itself, but only properties of that object (which is why {Binding} throws an exception, but the more explicit {Binding .} suppresses that exception, according to H.B.'s answer). In any case, the approach of creating a ViewModel and creating templates that target it appears to be more than a mere design guideline, but an actual technical requirement.

这篇关于在DataTemplate中不ControlTemplate中的绑定更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆