2011-10-14 25 views
1

我知道有第三方silverlight屬性網格(事實上我的公司擁有一個)所以請不要建議第三方控件:我想了解更多關於綁定xaml與這個問題。Silverlight PropertyGrid般的綁定

我正在寫一個Silverlight前端作爲標誌外觀來啓動SSRS和基於php的報告。

我已經創建了一個報告類,其中包含有關報告的信息,並且它有一個參數集合,其中包含有關需要填寫以運行報告的參數的信息。

我的計劃是創建綁定到報表的Parameters集合的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> 

謝謝!

+0

在http://forums.silverlight.net/p/95440/218611.aspx找到解決方案。查看DataTemplateSelector的解決方法部分。我會在7小時內發佈答案(因爲我沒有100點聲望點) –

回答

1

我想通了 - 一個偉大的FAQ對Silverlight的論壇上求助:

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

Silverlight不具備DataTemplateSelector類WPF的確,這已經解決了這個問題。

在標題爲

7.1結合WPF的功能尚未在Silverlight支持哪些數據?有沒有解決方法?

對於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]; 
     } 

很好用!