无法创建带有按钮和单击处理程序的未绑定列

问题描述 投票:0回答:1

我有一个数据网格,我正在根据从电子表格中读取的列动态构建该数据网格,该电子表格可以具有可变的列数。当我只有文本或枚举值时,一切正常。我遇到的问题是当我想显示一个按钮来调出子窗口时。

这是视图代码后面生成项目 DataTemplate 的代码:

itemCellTemp.Append("<DataTemplate ");
itemCellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
itemCellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' >");
itemCellTemp.Append("<Grid>");
itemCellTemp.Append("<Grid.Resources>");
itemCellTemp.Append("<converters:BlankToEditConverter x:Key='BlankNotesToEditConverter' xmlns:converters='clr-namespace:MyApp.WPF.Converters;assembly=MyApp.WPF'/>");
itemCellTemp.Append("</Grid.Resources>");
itemCellTemp.Append("<Button Content=\"{Binding RowData.Patches[" + index + "].DisplayValue, Converter={StaticResource BlankNotesToEditConverter}, ConverterParameter='" + patch.PropertyName + "'}\" ");
itemCellTemp.Append("Click=\"Button_Click\" ");
itemCellTemp.Append("</Grid>");
itemCellTemp.Append("</DataTemplate>");

我在后面的代码中定义了点击处理程序:

private void Button_Click(object sender, EventArgs e)
{
    // Bring up the child window
}

转换器仅获取从电子表格中读取的值,如果该值为空或空字符串,则返回一些占位符文本,以便用户可以单击。

我正在使用以下代码创建未绑定列:

var newCol = new UnboundColumn
{
    Key = patch.PropertyName,
    HeaderText = patch.DisplayName,
    IsReadOnly = false,
    ItemTemplate = (DataTemplate)XamlReader.Load(XmlReader.Create(new StringReader(itemCellTemp))),
};

我从这段代码中得到的错误是:

ArgumentException:无法绑定到目标方法,因为其签名或安全透明度与委托类型的签名或安全透明度不兼容。

我需要做什么才能让它能够看到 Button_Click 处理程序?

c# wpf datagrid
1个回答
0
投票

XamlReader
不处理事件处理程序,因此您应该从解析的 XAML 字符串中删除事件处理程序。

您可以绑定到命令或使用附加行为来动态连接事件处理程序:https://stackoverflow.com/a/34782692/7252182

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.