我有一个表格,它有一个网格。
我自动生成列并根据需要调整它们:
if (e.PropertyName == "id")
{
System.Windows.Style style = new Style(typeof(DataGridCell));
style.Setters.Add(new Setter(DataGridCell.ContentTemplateProperty, CreateBtnTemplate(30)));
e.Column.CellStyle = style;
}
private static DataTemplate CreateBtnTemplate(int width)
{
string str = "<DataTemplate xmlns='http://schemas.microsoft.com/client/2007' >"
//+ "<Button Tag='{Binding id}' Content='Respond'
+ "<Button Tag='{Binding id}' Content='Respond' "
+ "Visibility='{Binding id, Converter={StaticResource myConverter}}'"
+ " />"
+ "</DataTemplate>";
return (DataTemplate)XamlReader.Load(str);
}
在我的页面 xaml 中,我有:
<Grid x:Name="LayoutRoot" Margin="0,0,4,0">
<Grid.Resources>
<my:EnableDisableConverter x:Name="myConverter" x:Key="myConverter"></my:EnableDisableConverter>
</Grid.Resources>
我的课看起来像:
public class EnableDisableConverter : IValueConverter
{
object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Service1failedbackups f = value as Service1failedbackups;
if (f.resolution == null || f.resolution == "")
return System.Windows.Visibility.Visible;
else
return System.Windows.Visibility.Collapsed;
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{ return null;
}
}
简而言之,如果“分辨率”的内容为空白,我想要一个按钮,以便可以通过弹出窗口填写。
现在,一切都编译好了,一切看起来都很好。(我的定义为
xmlns:my="clr-namespace:SilverlightApplication1"
作为页眉的一部分。
我得到的错误是:
Error: Unhandled Error in Silverlight Application
Code: 2272
Category: ParserError
Message: Cannot find a Resource with the Name/Key myConverter
File:
Line: 1
Position: 121
现在,在我将 btnTemplate 的可见性部分放入之前,一切正常。我专门使用了 ID 列,因为我不需要用户查看它。
拜托,谁能告诉我我错过了什么。这让我发疯了。