【问题标题】:Binding mode for item in listbox列表框中项目的绑定模式
【发布时间】:2011-05-18 13:13:27
【问题描述】:

当对 ListBox 项使用 DataTemplate 时,我的应用程序冻结并在输出窗口中引发异常。详情如下。

编辑模板:

<DataTemplate x:Key="EditOnlyTemplate">
    <Border BorderBrush="Blue" Margin="3" Padding="3" BorderThickness="2" CornerRadius="5" Background="Beige">
        <StackPanel Orientation="Horizontal">
            <StackPanel Orientation="Vertical" VerticalAlignment="Center">
                <TextBox Width="300" FontSize="25" Foreground="Goldenrod" Text="{Binding XPath=/doc/dob/text, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged, diag:PresentationTraceSources.TraceLevel=High}" />
                <TextBox Width="300" FontSize="25" Foreground="Blue" Text="{Binding XPath=/doc/dob/group, UpdateSourceTrigger=PropertyChanged}" />
                <TextBox Width="300" FontSize="25" Foreground="Green" Text="{Binding XPath=/doc/dob/filter, UpdateSourceTrigger=PropertyChanged}" />
                <Button Content="Save" Click="btnSave_Click" Width="40" HorizontalAlignment="Left" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentControl}}}"/>
            </StackPanel>
        </StackPanel>
    </Border>
</DataTemplate>

我正在使用后面的代码来处理按钮单击并将我的 ListBox 项上的 DataTemplate 从仅显示绑定数据(即“详细信息”模板)的一种切换到我可以修改绑定数据的一种(即, “编辑”模板)。

private void btnEdit_Click(object sender, RoutedEventArgs e)
{
  Button btn = (Button)sender;
  //command contains the list item
  ContentControl itm = (ContentControl)btn.CommandParameter;
  itm.ContentTemplate = this.FindResource("EditTemplate") as DataTemplate;

}

关于我的 EditTemplate 需要注意的是第一个 TextBox 的绑定模式 - 它设置为 OneWayToSource。这就是导致问题的原因,当我单击在此模板中交换的“编辑”按钮时。如果我将模式更改为“TwoWay”(文本框的默认模式),则没有问题。此外,如果我在页面的另一个位置使用此模板的副本,在设计期间放置 - 不是通过代码 - 并且不是在 ListBox 项中,而是在 ContentControl 中,OneWayToSource 模式按预期工作。

我的破场景中抛出的异常如下:

System.Windows.Data Warning: 104 : BindingExpression (hash=48690759):   At level 0 - for XmlElement.InnerText found accessor ReflectPropertyDescriptor(InnerText)
    System.Windows.Data Warning: 100 : BindingExpression (hash=48690759): Replace item at level 0 with XmlElement (hash=21889970), using accessor ReflectPropertyDescriptor(InnerText)
    System.Windows.Data Warning: 86 : BindingExpression (hash=48690759): Update - got raw value ''
    System.Windows.Data Warning: 90 : BindingExpression (hash=48690759): Update - using final value ''
    System.Wi

ndows.Data Warning: 98 : BindingExpression (hash=48690759): SetValue at level 0 to XmlElement (hash=21889970) using ReflectPropertyDescriptor(InnerText): ''
A first chance exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll
System.Windows.Data Error: 8 : Cannot save value from target back to source. BindingExpression:Path=/InnerText; DataItem='XmlDocument' (HashCode=33583636); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String') InvalidOperationException:'System.InvalidOperationException: Dispatcher processing has been suspended, but messages are still being processed.
   at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value)
   at MS.Internal.Data.PropertyPathWorker.SetValue(Object item, Object value)
   at MS.Internal.Data.ClrBindingWorker.UpdateValue(Object value)
   at System.Windows.Data.BindingExpression.UpdateSource(Object value)'

我怀疑 ListBox 是某种原因,但不明白为什么,除了可能的错误。我在 Windows XP 上使用带有所有当前更新的 .Net Framework 3.5。有没有人认为我正在尝试做的事情有什么明显的错误?

【问题讨论】:

    标签: wpf binding listbox wpf-controls binding-mode


    【解决方案1】:

    忽略异常并帮助您克服当前的实现,您应该使用DataTemplateSelector。这是发生在ListBox 上的sample。

    【讨论】:

    • 嗨亚伦,感谢您的提示。将来我肯定会使用 DataTemplateSelector,但我不清楚它将如何影响我看到的这个绑定问题。在我看来,DataTemplateSelector 的预期用途是在 ListBox 中显示不同类型的项目,而我想做的是为绑定数据实现不同的使用“模式”。不过我会试一试的。
    【解决方案2】:

    OneWayToSource 尝试更新您的绑定数据,这可能是不可能的。 单向绑定应该可以正常工作。

    【讨论】:

    • 您好 Grozz,您是对的,出于某种原因,以这种方式切换 DataTemplate 确实会产生更新源的问题。但是,如果我尝试在我的 TextBox 目标上使用 TwoWay 而不是 OneWayToSource,它可以正常工作。
    猜你喜欢
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    相关资源
    最近更新 更多