我遇到了异常:
“{0}”不是 Visual 或 Visual3D。
我发现类似的唯一问题:WPF: System.ArgumentException => {"'{0}' is not a Visual or Visual3D."}
我只是在构建“漂亮”的网格。无需处理双击。当用户错误地双击时,这只是副作用 - 抛出此异常。
XAML 看起来像这样:
<DataGrid
ItemsSource="{Binding Source={StaticResource TrucksSource}}"
CanUserReorderColumns="False"
CanUserResizeColumns="True"
CanUserResizeRows="False"
AutoGenerateColumns="False"
BorderThickness="0"
CanUserAddRows="False"
RowBackground="{StaticResource GrayBackgroundGradientBrush}"
RowHeight="20" Focusable="False" RowHeaderWidth="0">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Select" Width="40" CanUserSort="True" SortMemberPath="IsSelected">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox
IsChecked="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Team" Width="42" CanUserSort="True" SortMemberPath="TeamDispatcherCaptionShort">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Border
Margin="-2,-1">
<TextBlock ToolTip="{Binding TeamDispatcherCaptionLong}"
Foreground="#414141" FontFamily="Arial" FontSize="12"
Text="{Binding TeamDispatcherCaptionShort}"
HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
每当用户双击时,我都会遇到异常。第一列是复选框。当它被单击时,它工作正常。当我单击复选框周围的任何区域时 - 异常。
我如何解决它?后面没有代码,是MVVM项目
编辑:
好的,我继续尝试在小项目上重现这个。我已经想到了问题,但想知道你对此的看法。我需要奖励这个赏金:)
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<CollectionViewSource x:Key="WidgetsSource" Source="{Binding Widgets}" />
</ResourceDictionary>
</Window.Resources>
<Grid>
<DataGrid
ItemsSource="{Binding Source={StaticResource WidgetsSource}}"
CanUserReorderColumns="False"
CanUserResizeColumns="True"
CanUserResizeRows="False"
AutoGenerateColumns="False"
BorderThickness="0"
CanUserAddRows="False"
VerticalGridLinesBrush="#00000000"
HorizontalGridLinesBrush="Gray"
RowBackground="LightGray"
RowHeight="20" Focusable="False" RowHeaderWidth="0" SelectionUnit="Cell">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Prop1" Width="50" CanUserSort="True" SortMemberPath="Prop1">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Border>
<TextBlock>
<Run Text="{Binding NestWidg.Prop1}" />
</TextBlock>
</Border>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Prop2" Width="50" CanUserSort="True" SortMemberPath="Prop1">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Border>
<TextBlock Text="{Binding Prop1}" />
</Border>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
虚拟机:
namespace WpfApplication1
{
using System.Collections.ObjectModel;
public class MainWindowVM
{
public ObservableCollection<Widget> Widgets { get; set; }
public MainWindowVM()
{
this.Widgets = new ObservableCollection<Widget>();
this.Widgets.Clear();
this.Widgets.Add(new Widget("a", "b") { NestWidg = new NestWidget { Prop1 = "Nest" } });
}
}
public class Widget
{
public Widget(string p1, string p2)
{
Prop1 = p1;
Prop2 = p2;
}
public string Prop1 { get; private set; }
public string Prop2 { get; private set; }
public NestWidget NestWidg { get; set; }
}
public class NestWidget
{
public string Prop1 { get; set; }
}
}
后面的代码:
namespace WpfApplication1
{
using System.Windows;
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
this.DataContext = new MainWindowVM();
}
}
}