Wpf 在嵌套列表中设置事件引发空引用异常
我正在尝试将文件放入文本框中,以便使用文件的路径(第二个 DataTemplate)设置文本框的内容。 所以我在我的文本框中添加了 DragOver 事件。但我得到一个空引用异常。 我尝试了许多其他事件并得到了同样的错误。在第一个 dataTemplate 中,事件工作正常。
这可能是因为我的文本框位于嵌套列表中。但我不确定。
有什么想法吗?
<ListBox ItemsSource="{Binding Model.Attributes, Mode=TwoWay}" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" Background="{x:Null}" BorderBrush="{x:Null}" Margin="0" BorderThickness="0" VerticalContentAlignment="Stretch" PreviewKeyDown="ListBox_PreviewKeyDown">
<ListBox.Resources>
<DataTemplate DataType="{x:Type local:TransformModel}">
<Expander Header="Transform" VerticalAlignment="Top" d:LayoutOverrides="Width" FontSize="10.667" HorizontalAlignment="Stretch" IsExpanded="{Binding DataContext.Model.TransformIsExpanded, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
<StackPanel HorizontalAlignment="Stretch">
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="14"/>
<ColumnDefinition Width="0.333*"/>
<ColumnDefinition Width="14"/>
<ColumnDefinition Width="0.333*"/>
<ColumnDefinition Width="14"/>
<ColumnDefinition Width="0.333*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.215*"/>
<RowDefinition Height="0.785*"/>
</Grid.RowDefinitions>
<TextBlock Margin="4,0" TextWrapping="Wrap" Text="Position" d:LayoutOverrides="Width, Height" Grid.ColumnSpan="6" FontSize="10.667"/>
<TextBlock TextWrapping="Wrap" Text="X" Grid.Row="1" VerticalAlignment="Top" Margin="4,0,0,0" d:LayoutOverrides="Width"/>
<TextBox Grid.Column="1" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Position.X, Mode=TwoWay}" VerticalAlignment="Top" FontSize="8" Margin="0,0,-0.001,0" d:LayoutOverrides="Width" GotFocus="TextGotFocus" PreviewMouseLeftButtonDown="SelectivelyIgnoreMouseButton" />
<TextBlock TextWrapping="Wrap" Text="Y" Grid.Row="1" VerticalAlignment="Top" Margin="3.998,0,4.562,0" Grid.Column="2" d:LayoutOverrides="Width"/>
<TextBox Grid.Column="3" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Position.Y, Mode=TwoWay}" VerticalAlignment="Top" FontSize="8" Margin="0" GotFocus="TextGotFocus" PreviewMouseLeftButtonDown="SelectivelyIgnoreMouseButton"/>
<TextBlock TextWrapping="Wrap" Text="Z" Grid.Row="1" VerticalAlignment="Top" Margin="3.999,0,4.344,0" Grid.Column="4" d:LayoutOverrides="Width"/>
<TextBox Grid.Column="5" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Position.Z, Mode=TwoWay}" VerticalAlignment="Top" FontSize="8" Margin="0,0,4,0" GotFocus="TextGotFocus" PreviewMouseLeftButtonDown="SelectivelyIgnoreMouseButton"/>
</Grid>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="14"/>
<ColumnDefinition Width="0.333*"/>
<ColumnDefinition Width="14"/>
<ColumnDefinition Width="0.333*"/>
<ColumnDefinition Width="14"/>
<ColumnDefinition Width="0.333*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.215*"/>
<RowDefinition Height="0.785*"/>
</Grid.RowDefinitions>
<TextBlock Margin="4,0" TextWrapping="Wrap" Text="Size" d:LayoutOverrides="Width, Height" Grid.ColumnSpan="6" FontSize="10.667"/>
<TextBlock TextWrapping="Wrap" Text="X" Grid.Row="1" VerticalAlignment="Top" Margin="4,0,0,0" d:LayoutOverrides="Width"/>
<TextBox Grid.Column="1" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Size.X, Mode=TwoWay}" VerticalAlignment="Top" FontSize="8" Margin="0,0,-0.001,0" d:LayoutOverrides="Width" GotFocus="TextGotFocus" PreviewMouseLeftButtonDown="SelectivelyIgnoreMouseButton"/>
<TextBlock TextWrapping="Wrap" Text="Y" Grid.Row="1" VerticalAlignment="Top" Margin="3.998,0,4.562,0" Grid.Column="2" d:LayoutOverrides="Width"/>
<TextBox Grid.Column="3" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Size.Y, Mode=TwoWay}" VerticalAlignment="Top" FontSize="8" Margin="0" GotFocus="TextGotFocus" PreviewMouseLeftButtonDown="SelectivelyIgnoreMouseButton"/>
<TextBlock TextWrapping="Wrap" Text="Z" Grid.Row="1" VerticalAlignment="Top" Margin="3.999,0,4.344,0" Grid.Column="4" d:LayoutOverrides="Width"/>
<TextBox Grid.Column="5" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Size.Z, Mode=TwoWay}" VerticalAlignment="Top" FontSize="8" Margin="0,0,4,0" GotFocus="TextGotFocus" PreviewMouseLeftButtonDown="SelectivelyIgnoreMouseButton"/>
</Grid>
<Separator Height="1" Margin="5,2,5,0"/>
</StackPanel>
</Expander>
</DataTemplate>
<DataTemplate DataType="{x:Type local:UnknownModel}">
<Expander Header="Miscellaneous" VerticalAlignment="Top" d:LayoutOverrides="Width" FontSize="10.667" HorizontalAlignment="Stretch" IsExpanded="{Binding DataContext.Model.TransformIsExpanded, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
<StackPanel HorizontalAlignment="Stretch">
<ListBox Grid.Row="1" VerticalAlignment="Top" ItemsSource="{Binding Attributs}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Background="{x:Null}" BorderBrush="{x:Null}" Margin="0" BorderThickness="0">
<ListBox.Resources>
<DataTemplate DataType="{x:Type local:PropertiesAssetsModel}">
<Grid d:DesignWidth="682.4" Margin="0,0,0,4" d:DesignHeight="144">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.163*"/>
<ColumnDefinition Width="0.837*"/>
</Grid.ColumnDefinitions>
<TextBlock TextWrapping="Wrap" VerticalAlignment="Center" d:LayoutOverrides="Width" Text="Assets" Margin="0,0,-0.003,0"/>
<TextBox Grid.Column="1" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Value, Mode=TwoWay}" VerticalAlignment="Top" FontSize="8" Margin="0,0,-0.001,0" d:LayoutOverrides="Width" AllowDrop="True" DragOver="AssetPathDragOvers"/>
</Grid>
</DataTemplate>
</ListBox.Resources>
</ListBox>
<Separator Height="1" Margin="5,2,5,0"/>
</StackPanel>
</Expander>
</DataTemplate>
</ListBox.Resources>
</ListBox>
I'm trying to drop a file into a textbox in order to set the content of the textbox with the path of the file (second DataTemplate).
So I added the event DragOver on my textBox. But I get an Null reference exception.
I tried many other events and got the same error. In the first dataTemplate, events are just working fine.
This may be because my textbox is in a nested list. But i am not sure.
Any idea ?
<ListBox ItemsSource="{Binding Model.Attributes, Mode=TwoWay}" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" Background="{x:Null}" BorderBrush="{x:Null}" Margin="0" BorderThickness="0" VerticalContentAlignment="Stretch" PreviewKeyDown="ListBox_PreviewKeyDown">
<ListBox.Resources>
<DataTemplate DataType="{x:Type local:TransformModel}">
<Expander Header="Transform" VerticalAlignment="Top" d:LayoutOverrides="Width" FontSize="10.667" HorizontalAlignment="Stretch" IsExpanded="{Binding DataContext.Model.TransformIsExpanded, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
<StackPanel HorizontalAlignment="Stretch">
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="14"/>
<ColumnDefinition Width="0.333*"/>
<ColumnDefinition Width="14"/>
<ColumnDefinition Width="0.333*"/>
<ColumnDefinition Width="14"/>
<ColumnDefinition Width="0.333*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.215*"/>
<RowDefinition Height="0.785*"/>
</Grid.RowDefinitions>
<TextBlock Margin="4,0" TextWrapping="Wrap" Text="Position" d:LayoutOverrides="Width, Height" Grid.ColumnSpan="6" FontSize="10.667"/>
<TextBlock TextWrapping="Wrap" Text="X" Grid.Row="1" VerticalAlignment="Top" Margin="4,0,0,0" d:LayoutOverrides="Width"/>
<TextBox Grid.Column="1" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Position.X, Mode=TwoWay}" VerticalAlignment="Top" FontSize="8" Margin="0,0,-0.001,0" d:LayoutOverrides="Width" GotFocus="TextGotFocus" PreviewMouseLeftButtonDown="SelectivelyIgnoreMouseButton" />
<TextBlock TextWrapping="Wrap" Text="Y" Grid.Row="1" VerticalAlignment="Top" Margin="3.998,0,4.562,0" Grid.Column="2" d:LayoutOverrides="Width"/>
<TextBox Grid.Column="3" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Position.Y, Mode=TwoWay}" VerticalAlignment="Top" FontSize="8" Margin="0" GotFocus="TextGotFocus" PreviewMouseLeftButtonDown="SelectivelyIgnoreMouseButton"/>
<TextBlock TextWrapping="Wrap" Text="Z" Grid.Row="1" VerticalAlignment="Top" Margin="3.999,0,4.344,0" Grid.Column="4" d:LayoutOverrides="Width"/>
<TextBox Grid.Column="5" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Position.Z, Mode=TwoWay}" VerticalAlignment="Top" FontSize="8" Margin="0,0,4,0" GotFocus="TextGotFocus" PreviewMouseLeftButtonDown="SelectivelyIgnoreMouseButton"/>
</Grid>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="14"/>
<ColumnDefinition Width="0.333*"/>
<ColumnDefinition Width="14"/>
<ColumnDefinition Width="0.333*"/>
<ColumnDefinition Width="14"/>
<ColumnDefinition Width="0.333*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.215*"/>
<RowDefinition Height="0.785*"/>
</Grid.RowDefinitions>
<TextBlock Margin="4,0" TextWrapping="Wrap" Text="Size" d:LayoutOverrides="Width, Height" Grid.ColumnSpan="6" FontSize="10.667"/>
<TextBlock TextWrapping="Wrap" Text="X" Grid.Row="1" VerticalAlignment="Top" Margin="4,0,0,0" d:LayoutOverrides="Width"/>
<TextBox Grid.Column="1" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Size.X, Mode=TwoWay}" VerticalAlignment="Top" FontSize="8" Margin="0,0,-0.001,0" d:LayoutOverrides="Width" GotFocus="TextGotFocus" PreviewMouseLeftButtonDown="SelectivelyIgnoreMouseButton"/>
<TextBlock TextWrapping="Wrap" Text="Y" Grid.Row="1" VerticalAlignment="Top" Margin="3.998,0,4.562,0" Grid.Column="2" d:LayoutOverrides="Width"/>
<TextBox Grid.Column="3" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Size.Y, Mode=TwoWay}" VerticalAlignment="Top" FontSize="8" Margin="0" GotFocus="TextGotFocus" PreviewMouseLeftButtonDown="SelectivelyIgnoreMouseButton"/>
<TextBlock TextWrapping="Wrap" Text="Z" Grid.Row="1" VerticalAlignment="Top" Margin="3.999,0,4.344,0" Grid.Column="4" d:LayoutOverrides="Width"/>
<TextBox Grid.Column="5" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Size.Z, Mode=TwoWay}" VerticalAlignment="Top" FontSize="8" Margin="0,0,4,0" GotFocus="TextGotFocus" PreviewMouseLeftButtonDown="SelectivelyIgnoreMouseButton"/>
</Grid>
<Separator Height="1" Margin="5,2,5,0"/>
</StackPanel>
</Expander>
</DataTemplate>
<DataTemplate DataType="{x:Type local:UnknownModel}">
<Expander Header="Miscellaneous" VerticalAlignment="Top" d:LayoutOverrides="Width" FontSize="10.667" HorizontalAlignment="Stretch" IsExpanded="{Binding DataContext.Model.TransformIsExpanded, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
<StackPanel HorizontalAlignment="Stretch">
<ListBox Grid.Row="1" VerticalAlignment="Top" ItemsSource="{Binding Attributs}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Background="{x:Null}" BorderBrush="{x:Null}" Margin="0" BorderThickness="0">
<ListBox.Resources>
<DataTemplate DataType="{x:Type local:PropertiesAssetsModel}">
<Grid d:DesignWidth="682.4" Margin="0,0,0,4" d:DesignHeight="144">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.163*"/>
<ColumnDefinition Width="0.837*"/>
</Grid.ColumnDefinitions>
<TextBlock TextWrapping="Wrap" VerticalAlignment="Center" d:LayoutOverrides="Width" Text="Assets" Margin="0,0,-0.003,0"/>
<TextBox Grid.Column="1" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Value, Mode=TwoWay}" VerticalAlignment="Top" FontSize="8" Margin="0,0,-0.001,0" d:LayoutOverrides="Width" AllowDrop="True" DragOver="AssetPathDragOvers"/>
</Grid>
</DataTemplate>
</ListBox.Resources>
</ListBox>
<Separator Height="1" Margin="5,2,5,0"/>
</StackPanel>
</Expander>
</DataTemplate>
</ListBox.Resources>
</ListBox>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚遇到了同样的问题(我的问题是使用
comboBox
和LostFocus 事件
)。我可以通过将事件处理
位移动到视图的ResourceDictionary
中的样式来解决这个问题。尝试这样的事情:
I just encountered the same sort of problem (mine was with a
comboBox
and theLostFocus event
). I'm able to work around it by moving theEvent handling
bit to a style in my view'sResourceDictionary
.Try something like this:
谢谢,
嵌套数据模板无法与 .NET 4.0 正常工作
您可以阅读有关此错误的更多信息:
官方错误报告
我的解决方法:
创建我自己的数据模板选择器:
模板选择器xaml:
以及我如何使用它:
Thanks,
Nested data templates are not working properly with .NET 4.0
You can read more about this bug :
Official bug report
My workaround :
Create my own data template selector :
Template selector in xaml :
and how I use it :
有一个修补程序可修复此错误。
There's a hotfix available to fix this bug.