【问题标题】:Silverlight PropertyGrid-like bindingSilverlight 类似 PropertyGrid 的绑定
【发布时间】:2011-10-14 17:32:30
【问题描述】:

我知道有第 3 方 silverlight 属性网格(实际上我的公司拥有一个)所以请不要建议第 3 方控件:我正在尝试通过这个问题了解有关在 xaml 中绑定的更多信息。

我正在编写一个 Silverlight 前端作为启动 SSRS 和基于 php 的报告的标志外观。

我创建了一个包含报表信息的 Report 类,它有一个参数集合,其中包含运行报表需要填写的参数信息。

我的计划是创建一个绑定到报表参数集合的 silverlight 属性网格。

这是一个更简单的类版本:

    public class Report
    {        

        public int ReportId { get; set; }
        public string ReportName { get; set; }
        public string Description { get; set; }

        private List<ReportParameter> _Parameters = new List<ReportParameter>();

        public List<ReportParameter> Parameters
        {
            get { return _Parameters; }
            set { _Parameters = value; }
        } 
}

public class ReportParameter
{
        public int ReportId { get; set; }
        public string ParameterName { get; set; }
        public string DataTemplateName { get; set; }
        public bool IsRequired { get; set; }
}

我希望使用ReportParameterDataTemplateName 属性绑定到数据模板:例如,如果我有一个日期参数,我希望能够设置DataTemplateName="MyDatePicker" 然后DataTemplate={StaticResource {Binding DataTemplateName}}并让该行使用资源中定义的DataTemplate 来编辑参数值。

这是我用来尝试使其工作的一些 XAML:

<UserControl x:Class="ReportLauncherWorkbench.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ReportLauncherWorkbench"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <UserControl.Resources>
        <local:Report x:Key="MyData" 
                      ReportName="Rick Report" 
                      Description="Great report, try it!" 
                      ReportId="0"
        >
            <local:Report.Parameters>
                <local:XReportParameter DataTemplateName="DatePickerTemplate"
                                        ParameterName="StartDate"
                                         IsRequired="True" 
                                         Tooltip="Please enter the start date"
                                        />

                <local:XReportParameter DataTemplateName="CheckBoxTemplate"
                                        ParameterName="AmIHot"
                                        IsRequired="True" 
                                        Tooltip="Please check here if you are hot"
                                        />

            </local:Report.Parameters>
        </local:Report>

        <DataTemplate x:Key="DatePickerTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBox />
                <Button Content="..."/>
            </StackPanel>

        </DataTemplate>

        <DataTemplate x:Key="CheckBoxTemplate">
            <StackPanel Orientation="Horizontal">
                <CheckBox/>
            </StackPanel>
        </DataTemplate>

    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource MyData}">
        <Grid x:Name="Test1" Background="White">
            <StackPanel>
                <TextBlock Text="{Binding ReportName}"/>
                <TextBlock Text="{Binding Description}"/>

                <ListBox  ItemsSource="{Binding Parameters}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="50"></ColumnDefinition>
                                        <ColumnDefinition></ColumnDefinition>
                                        <ColumnDefinition></ColumnDefinition>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Text="{Binding ParameterName}" Grid.Column="1" Margin="5"/>
                                <ListBox ItemsSource="{Binding}" Grid.Column="2" Width="100" ItemTemplate="{StaticResource {Binding DataTemplateName}}">
                                        <!-- I want to somehow bind which DataTemplate is rendered-->

                                </ListBox>

                                </Grid>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>
        </Grid>

    </Grid>
</UserControl>

谢谢!

【问题讨论】:

标签: silverlight data-binding propertygrid


【解决方案1】:

我想通了 - 在 silverlight 论坛上的一个很棒的常见问题解答的帮助下:

http://forums.silverlight.net/p/95440/218611.aspx

Silverlight 没有 WPF 所具有的 DataTemplateSelector 类,这可以解决问题。

在标题为

的部分中

7.1 Silverlight 尚不支持 WPF 的哪些数据绑定功能?有解决办法吗?

DataTemplateSelector 功能有一个简单的解决方法。

以下是我在代码示例中修复它的方法:

将列表框替换为以下内容:

<ListBox  ItemsSource="{Binding Parameters}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <ContentControl Content="{Binding}" Loaded="ContentControl_Loaded"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

然后在后面的代码中填写ContentControl_Loaded事件:

 private void ContentControl_Loaded(object sender, RoutedEventArgs e)
        {
            ContentControl cc = (ContentControl) sender;
            XReportParameter p = (XReportParameter)cc.DataContext;
            cc.ContentTemplate = (DataTemplate)this.Resources[p.DataTemplateName];
        }

效果很好!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多