Wpf 在嵌套列表中设置事件引发空引用异常

发布于 2024-12-12 05:39:57 字数 7886 浏览 0 评论 0原文

我正在尝试将文件放入文本框中,以便使用文件的路径(第二个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-残月青衣踏尘吟 2024-12-19 05:39:57

我刚刚遇到了同样的问题(我的问题是使用 comboBoxLostFocus 事件)。我可以通过将事件处理位移动到视图的ResourceDictionary中的样式来解决这个问题。

尝试这样的事情:

    <ResourceDictionary>
        <Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
            <EventSetter Event="DragOver" Handler="AssetPathDragOvers"/>
        </Style>
    </ResourceDictionary>

    <TextBox Style="{StaticResource ComboBoxStyle}" ...

I just encountered the same sort of problem (mine was with a comboBox and the LostFocus event). I'm able to work around it by moving the Event handling bit to a style in my view's ResourceDictionary.

Try something like this:

    <ResourceDictionary>
        <Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
            <EventSetter Event="DragOver" Handler="AssetPathDragOvers"/>
        </Style>
    </ResourceDictionary>

    <TextBox Style="{StaticResource ComboBoxStyle}" ...
离鸿 2024-12-19 05:39:57

谢谢,

嵌套数据模板无法与 .NET 4.0 正常工作

您可以阅读有关此错误的更多信息:
官方错误报告

我的解决方法:

创建我自己的数据模板选择器:

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}"/>

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 :

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;
    }
}

Template selector in xaml :

<view:TemplateSelector x:Key="templateSelector" UnknownTemplate="{StaticResource PropertiesUnkownModel}"/>

and how I use it :

<ListBox Grid.Row="1" VerticalAlignment="Top" ItemsSource="{Binding Attributs}" ItemTemplateSelector="{DynamicResource templateSelector}"/>
疯了 2024-12-19 05:39:57

有一个修补程序可修复此错误。

There's a hotfix available to fix this bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文