EDN Admin
Well-known member
wpf开发环境VS2010,.Net4.0 wpf前台代码
<Window x:Class="wpfTech.MainWindow"<br/>
xmlns=" http://schemas.microsoft.com/winfx/2006/xaml/presentation http://schemas.microsoft.com/winfx/2006/xaml/presentation "<br/>
xmlns:x=" http://schemas.microsoft.com/winfx/2006/xaml http://schemas.microsoft.com/winfx/2006/xaml "<br/>
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded <br/>
<Grid><br/>
<DataGrid Name="dataGrid1" AutoGenerateColumns="False" Height="235" HorizontalAlignment="Left"<br/>
Margin="42,30,0,0" VerticalAlignment="Top" Width="387"<br/>
ItemsSource="{Binding}" CanUserAddRows="False"<br/>
DataContext="{Binding} <br/>
<DataGrid.Columns><br/>
<DataGridHyperlinkColumn Header="附件名称" Width="197" Binding="{Binding Path=_name, Mode=TwoWay}" /><br/>
<DataGridTextColumn Header="修改日期" Width="80" Binding="{Binding Path=_time,Mode=TwoWay}"/>
<br/>
</DataGrid.Columns><br/>
<DataGrid.RowDetailsTemplate><br/>
<DataTemplate ><br/>
<DataGrid Name="dataGridDetail" ItemsSource="{Binding _child}" AutoGenerateColumns="False <br/>
<DataGrid.Columns><br/>
<DataGridTextColumn Header="there" Binding="{Binding sdf}"/><br/>
<DataGridTemplateColumn><br/>
<DataGridTemplateColumn.CellTemplate><br/>
<DataTemplate ><br/>
<Button Name="buttonTest"
Content="{Binding abc}" Click="Button_Click" /><br/>
</DataTemplate><br/>
</DataGridTemplateColumn.CellTemplate><br/>
</DataGridTemplateColumn>
<br/>
</DataGrid.Columns><br/>
</DataGrid><br/>
</DataTemplate><br/>
</DataGrid.RowDetailsTemplate><br/>
</DataGrid><br/>
<br/>
</Grid><br/>
</Window><br/>
后台代码:
using System;<br/>
using System.Collections.Generic;<br/>
using System.Linq;<br/>
using System.Text;<br/>
using System.Windows;<br/>
using System.Windows.Controls;<br/>
using System.Windows.Data;<br/>
using System.Windows.Documents;<br/>
using System.Windows.Input;<br/>
using System.Windows.Media;<br/>
using System.Windows.Media.Imaging;<br/>
using System.Windows.Navigation;<br/>
using System.Windows.Shapes;<br/>
using System.IO;
namespace wpfTech<br/>
{<br/>
/// <summary><br/>
/// MainWindow.xaml 的交互逻辑<br/>
/// </summary><br/>
public partial class MainWindow : Window<br/>
{<br/>
static public List<data> listdata = new List<data>();
<br/>
public MainWindow()<br/>
{<br/>
InitializeComponent();
listdata.Add(new data() { _name = "abc", _time = "abctime", _child = new List<file> { new file() { abc = "1", sdf = "4" }, new file() { abc = "2", sdf = "3" } } });<br/>
<br/>
listdata.Add(new data() { _name = "def", _time = "deftime", _child = new List<file> { new file() { abc = "79", sdf = "0-" } } });<br/>
<br/>
listdata.Add(new data() { _name = "ghi", _time = "ghitime", _child = new List<file> { new file() { abc = "qwe", sdf = "rt5y" } } });<br/>
dataGrid1.ItemsSource = listdata;<br/>
}
private void Window_Loaded(object sender, RoutedEventArgs e)<br/>
{<br/>
}<br/>
private void showit(string id)<br/>
{<br/>
MessageBox.Show(id);<br/>
}<br/>
private void Button_Click(object sender, RoutedEventArgs e)<br/>
{
}
}<br/>
public class data<br/>
{ <br/>
public string _name{get;set;}<br/>
public string _time {get;set;}<br/>
public List<file> _child { get; set; }<br/>
}<br/>
public class file<br/>
{<br/>
public string abc { get; set; }<br/>
public string sdf { get; set; }<br/>
}<br/>
}<br/>
编译运行时,当点击DataGrid的某行,意愿是显示详细信息DataGrid。但由于详细信息的DataGrid中buttonTest控件上有Click方法,导致出现â未将对象引用设置到对象的实例。â错误,没有调用堆栈信息。
事实上,在详细信息DataGrid中的行上,添加事件处理,都会导致上述错误。
但我必须实现显示 详细信息DataGrid的行,并且在行的cell上注册事件。
请问为什么会出现这样的错误,如何解决?
View the full article
<Window x:Class="wpfTech.MainWindow"<br/>
xmlns=" http://schemas.microsoft.com/winfx/2006/xaml/presentation http://schemas.microsoft.com/winfx/2006/xaml/presentation "<br/>
xmlns:x=" http://schemas.microsoft.com/winfx/2006/xaml http://schemas.microsoft.com/winfx/2006/xaml "<br/>
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded <br/>
<Grid><br/>
<DataGrid Name="dataGrid1" AutoGenerateColumns="False" Height="235" HorizontalAlignment="Left"<br/>
Margin="42,30,0,0" VerticalAlignment="Top" Width="387"<br/>
ItemsSource="{Binding}" CanUserAddRows="False"<br/>
DataContext="{Binding} <br/>
<DataGrid.Columns><br/>
<DataGridHyperlinkColumn Header="附件名称" Width="197" Binding="{Binding Path=_name, Mode=TwoWay}" /><br/>
<DataGridTextColumn Header="修改日期" Width="80" Binding="{Binding Path=_time,Mode=TwoWay}"/>
<br/>
</DataGrid.Columns><br/>
<DataGrid.RowDetailsTemplate><br/>
<DataTemplate ><br/>
<DataGrid Name="dataGridDetail" ItemsSource="{Binding _child}" AutoGenerateColumns="False <br/>
<DataGrid.Columns><br/>
<DataGridTextColumn Header="there" Binding="{Binding sdf}"/><br/>
<DataGridTemplateColumn><br/>
<DataGridTemplateColumn.CellTemplate><br/>
<DataTemplate ><br/>
<Button Name="buttonTest"
Content="{Binding abc}" Click="Button_Click" /><br/>
</DataTemplate><br/>
</DataGridTemplateColumn.CellTemplate><br/>
</DataGridTemplateColumn>
<br/>
</DataGrid.Columns><br/>
</DataGrid><br/>
</DataTemplate><br/>
</DataGrid.RowDetailsTemplate><br/>
</DataGrid><br/>
<br/>
</Grid><br/>
</Window><br/>
后台代码:
using System;<br/>
using System.Collections.Generic;<br/>
using System.Linq;<br/>
using System.Text;<br/>
using System.Windows;<br/>
using System.Windows.Controls;<br/>
using System.Windows.Data;<br/>
using System.Windows.Documents;<br/>
using System.Windows.Input;<br/>
using System.Windows.Media;<br/>
using System.Windows.Media.Imaging;<br/>
using System.Windows.Navigation;<br/>
using System.Windows.Shapes;<br/>
using System.IO;
namespace wpfTech<br/>
{<br/>
/// <summary><br/>
/// MainWindow.xaml 的交互逻辑<br/>
/// </summary><br/>
public partial class MainWindow : Window<br/>
{<br/>
static public List<data> listdata = new List<data>();
<br/>
public MainWindow()<br/>
{<br/>
InitializeComponent();
listdata.Add(new data() { _name = "abc", _time = "abctime", _child = new List<file> { new file() { abc = "1", sdf = "4" }, new file() { abc = "2", sdf = "3" } } });<br/>
<br/>
listdata.Add(new data() { _name = "def", _time = "deftime", _child = new List<file> { new file() { abc = "79", sdf = "0-" } } });<br/>
<br/>
listdata.Add(new data() { _name = "ghi", _time = "ghitime", _child = new List<file> { new file() { abc = "qwe", sdf = "rt5y" } } });<br/>
dataGrid1.ItemsSource = listdata;<br/>
}
private void Window_Loaded(object sender, RoutedEventArgs e)<br/>
{<br/>
}<br/>
private void showit(string id)<br/>
{<br/>
MessageBox.Show(id);<br/>
}<br/>
private void Button_Click(object sender, RoutedEventArgs e)<br/>
{
}
}<br/>
public class data<br/>
{ <br/>
public string _name{get;set;}<br/>
public string _time {get;set;}<br/>
public List<file> _child { get; set; }<br/>
}<br/>
public class file<br/>
{<br/>
public string abc { get; set; }<br/>
public string sdf { get; set; }<br/>
}<br/>
}<br/>
编译运行时,当点击DataGrid的某行,意愿是显示详细信息DataGrid。但由于详细信息的DataGrid中buttonTest控件上有Click方法,导致出现â未将对象引用设置到对象的实例。â错误,没有调用堆栈信息。
事实上,在详细信息DataGrid中的行上,添加事件处理,都会导致上述错误。
但我必须实现显示 详细信息DataGrid的行,并且在行的cell上注册事件。
请问为什么会出现这样的错误,如何解决?
View the full article