我正在尝试将文件拖放到文本框中,以便使用文件路径(第二个DataTemplate)设置文本框的内容。 所以我在我的textBox上添加了事件DragOver。但我得到一个Null引用异常。 我尝试了很多其他事件并得到了同样的错误。在第一个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>
答案 0 :(得分:1)
我刚遇到同样的问题(我的问题是comboBox
和LostFocus event
)。我可以通过将Event handling
位移动到我视图ResourceDictionary
中的样式来解决它。
尝试这样的事情:
<ResourceDictionary>
<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
<EventSetter Event="DragOver" Handler="AssetPathDragOvers"/>
</Style>
</ResourceDictionary>
<TextBox Style="{StaticResource ComboBoxStyle}" ...
答案 1 :(得分:1)
谢谢,
嵌套数据模板在.NET 4.0中无法正常运行
您可以阅读有关此错误的更多信息: Official bug report
我的解决方法:
创建我自己的数据模板选择器:
public class TemplateSelector : DataTemplateSelector
{
public DataTemplate UnknownTemplate { get; set; }
public override DataTemplate SelectTemplate(object item,
DependencyObject container)
{
if (item is PropertiesUnkownModel)
{
return UnknownTemplate;
}
return UnknownTemplate;
}
}
xaml中的模板选择器:
<view:TemplateSelector x:Key="templateSelector" UnknownTemplate="{StaticResource PropertiesUnkownModel}"/>
以及我如何使用它:
<ListBox Grid.Row="1" VerticalAlignment="Top" ItemsSource="{Binding Attributs}" ItemTemplateSelector="{DynamicResource templateSelector}"/>
答案 2 :(得分:0)
有hotfix可用来修复此错误。