【问题标题】:Unhandled Exception on button click event按钮单击事件上的未处理异常
【发布时间】:2016-12-27 07:15:22
【问题描述】:

我已将一个数据表(在运行时填充)绑定到我的数据网格,并且有一列列出了文件路径。我正在自定义该列以用图像按钮替换文件路径以在单击时打开文件。我收到以下错误,但无法解决。任何方向将不胜感激!

错误: System.Core.dll 中出现“Microsoft.CSharp.RuntimeBinder.RuntimeBinderException”类型的未处理异常 附加信息:“System.Data.DataRowView”不包含“Master”的定义

我的按钮事件出现错误...“d.Master”。

private void ButtonClick(object sender, RoutedEventArgs e)
{
    Button button2 = (Button)e.OriginalSource;
    dynamic d = button2.DataContext;
    string filepath = d.Master;
    Process.Start(filepath); 
}

数据网格:

<DataGrid x:Name="DataGrid1" HorizontalAlignment="Stretch" 
          Margin="650,197,449,0" VerticalAlignment="Stretch" 
          AutoGenerateColumns="True" AutoGeneratingColumn="DataGrid_AugoGeneratingColumn" ItemsSource="{Binding fileTable}">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Master" CellTemplate="{StaticResource DataTemplate2}"/>
    </DataGrid.Columns>
</DataGrid>

数据模板

<DataTemplate x:Key="DataTemplate2">
<Button Name="fileButton" Click="ButtonClick" Width="30" Height="30" BorderBrush="#FF707070" BorderThickness="1,1,0,1">
    <Button.Background>
        <ImageBrush ImageSource="C:\Images\PDFicon.png" Stretch="Uniform"/>
    </Button.Background>
</Button>
</DataTemplate>

AutoGeneratingColumn 方法:

private void DataGrid_AugoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.Column.Header.ToString() == "Master")
    {
        DataGridTemplateColumn templateColumn = new DataGridTemplateColumn    //create new template column
        CellTemplate = (DataTemplate) Resources["DataTemplate2"]
        e.Column = templateColumn; // Replace the auto-generated column with the templateColumn.
    }
    else
    {
        e.Column.Header = "Expired";
    }
}

【问题讨论】:

  • 错误似乎很清楚。您的代码假定该对象具有 Master 属性,但事实并非如此。但是您没有提供一个很好的 minimal reproducible example 来清楚地显示此代码的上下文以及您可能的意思,而不是尝试获取 Master 属性。也许您可以按照建议添加 Master 属性,也许不是。没有更好的代码示例是不可能知道的。

标签: c# wpf xaml datagrid datatemplate


【解决方案1】:

您为什么要从按钮的DataContext 中获取未定义的Master?即为什么会出现错误。

由于fileTable 是您的数据网格的来源,您需要在fileTable 数据类型中有一个项目Master。由于还没有定义,所以会报错。

最好的解决方案是在您的 ItemsSource 的数据类中拥有一个 Master 属性,并相应地填充该属性,以便在单击按钮时,您可以像现在一样通过 DataContext 访问它。

【讨论】:

    【解决方案2】:

    谢谢大家,这为我指明了正确的方向。我认为 DataContext 将我指向数据表的“主”列本身,它是在运行时生成的,而不是真正的属性。我通过识别选定的单元格来解决我的问题,而不是。

            private void ButtonClick(object sender, RoutedEventArgs e)
        {
            //Get column Index of selected cell & set as variable
            int colIndex = BoundPivotGrid.CurrentCell.Column.DisplayIndex;
           DataRowView drv = (DataRowView)BoundPivotGrid.SelectedItem;
            String valueOfItem = drv[colIndex].ToString();
    
            if (valueOfItem == "-")
            {
                MessageBox.Show("No file");
            }
            else
            {
                Process.Start(valueOfItem);
            }
    

    【讨论】:

      【解决方案3】:

      问题是“字符串文件路径 = d.Master;”。 d 是 DataRowView 类型,它没有名为 Master 的属性。将 dynamic 更改为 var ,它将在编译时而不是运行时失败

      【讨论】:

      • 这对我来说似乎不是一个答案。将异常更改为编译时错误并不能解决问题。它只是移动它。 (无论如何,该建议也没有多大帮助;DataContext 的类型为 object,所以肯定var 会在编译时失败,但不会以某种方式告诉你任何事情。即使dynamic 没有。)
      • 答案是第二句话。 d 是 DataRowView 类型,它没有名为 Master 的属性
      • 所做的只是重复错误消息。它说了很多。这如何解决问题作者的问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多