Silverlight、数据网格、修改列上自动生成的列更改内容的单元格可见性

发布于 2024-11-09 02:48:18 字数 2320 浏览 4 评论 0原文

我有一个表格,它有一个网格。

我自动生成列并根据需要调整它们:

    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 列,因为我不需要用户看到请

有人告诉我我错过了什么,这让我发疯。

I have a form, it has a grid.

I autogenerate the columns and tweak them as required:

    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);
    }

In my pages xaml, I have:

<Grid x:Name="LayoutRoot" Margin="0,0,4,0">
    <Grid.Resources>
        <my:EnableDisableConverter x:Name="myConverter" x:Key="myConverter"></my:EnableDisableConverter>
    </Grid.Resources>

My class looks like:

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;
    }
}

In short, if the content of "resolution" is blank, I want a button so I it can be filled in, via a popup window.

Now, it all compiles, it all looks good.
(my is defined as

xmlns:my="clr-namespace:SilverlightApplication1"

as part of the page header.

The error I get is:

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     

Now, its all OK until I put the visibility part of my btnTemplate in. I've used the ID column specifically because, I dont need users to see it.

Please, can someone tell me what I missed. This is driving me nuts.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娜些时光,永不杰束 2024-11-16 02:48:18

我修复了它,

方法如下。

在我的 xaml 文件中添加

xmlns:converter="clr-namespace:SilverlightApplication1"

,然后在网格下添加,

    <converter:EnableDisableConverter x:Name="myConverter" x:Key="myConverter" />
    <DataTemplate x:Key="DataTemplate" >
        <Button Visibility='{Binding Converter={StaticResource myConverter}}' Content='Respond' Click="btnRespond_Click" />
    </DataTemplate>
    <DataTemplate x:Key="err" >
        <TextBlock Text='{Binding err}' FontSize='8'  TextWrapping='Wrap'/>
    </DataTemplate>
    <DataTemplate x:Key="resolution" >
        <TextBlock Text='{Binding resolution}' FontSize='8'  TextWrapping='Wrap'/>
    </DataTemplate>
</Grid.Resources>

然后放弃所有列的自动生成并设置

    <sdk:DataGrid.Columns>
        <sdk:DataGridTemplateColumn  CellTemplate="{StaticResource DataTemplate}" Header="Action" Width="Auto" />
        <sdk:DataGridTextColumn Binding="{Binding server}"  Header="server" Width="Auto" />
        <sdk:DataGridTextColumn Binding="{Binding software}"  Header="software" Width="Auto" />
        <sdk:DataGridTextColumn Binding="{Binding backupgroup}"  Header="backupgroup" Width="Auto" />
        <sdk:DataGridTemplateColumn  CellTemplate="{StaticResource err}" Header="Action" Width="250" />
        <sdk:DataGridTemplateColumn  CellTemplate="{StaticResource resolution}" Header="Action" Width="150" />
        <sdk:DataGridTextColumn Binding="{Binding resolver}"  Header="resolver" Width="Auto" />
    </sdk:DataGrid.Columns>

现在它可以工作。现在它翻转工作了..
欢呼

I fixed it

Heres how.

in my xaml file I added

xmlns:converter="clr-namespace:SilverlightApplication1"

and then under grid I added

    <converter:EnableDisableConverter x:Name="myConverter" x:Key="myConverter" />
    <DataTemplate x:Key="DataTemplate" >
        <Button Visibility='{Binding Converter={StaticResource myConverter}}' Content='Respond' Click="btnRespond_Click" />
    </DataTemplate>
    <DataTemplate x:Key="err" >
        <TextBlock Text='{Binding err}' FontSize='8'  TextWrapping='Wrap'/>
    </DataTemplate>
    <DataTemplate x:Key="resolution" >
        <TextBlock Text='{Binding resolution}' FontSize='8'  TextWrapping='Wrap'/>
    </DataTemplate>
</Grid.Resources>

I then ditched all the auto generating of my columns and set

    <sdk:DataGrid.Columns>
        <sdk:DataGridTemplateColumn  CellTemplate="{StaticResource DataTemplate}" Header="Action" Width="Auto" />
        <sdk:DataGridTextColumn Binding="{Binding server}"  Header="server" Width="Auto" />
        <sdk:DataGridTextColumn Binding="{Binding software}"  Header="software" Width="Auto" />
        <sdk:DataGridTextColumn Binding="{Binding backupgroup}"  Header="backupgroup" Width="Auto" />
        <sdk:DataGridTemplateColumn  CellTemplate="{StaticResource err}" Header="Action" Width="250" />
        <sdk:DataGridTemplateColumn  CellTemplate="{StaticResource resolution}" Header="Action" Width="150" />
        <sdk:DataGridTextColumn Binding="{Binding resolver}"  Header="resolver" Width="Auto" />
    </sdk:DataGrid.Columns>

NOW it works. NOW it flipping works..
hurrah

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文