【发布时间】: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