2015-07-11 65 views
1

我需要更改RadGridView的CellTemplate。所有的例子我可以在網上找到靜態定義列在XAML中,然後在該列的標籤,它們定義了CellTemplate:RadGridView CellTemplate

<telerik:RadGridView AutoGenerateColumns="False" ItemsSource="{Binding}" RowStyleSelector="{StaticResource styleSelector}"> 
    <telerik:RadGridView.Columns> 
     <telerik:GridViewDataColumn DataMemberBinding="{Binding ID}" CellTemplateSelector="{StaticResource templateSelector}" /> 
    </telerik:RadGridView.Columns> 
</telerik:RadGridView> 

上面的例子加載CellTemplate僅適用於標題爲「ID列「,併爲那個列中的所有單元做到這一點。

我已經設法加載CellTemplates在後面的代碼中,但這需要我將整個網格的一個實例傳遞給ViewModel,還必須添加一個依賴屬性到GridView,以將網格的列綁定到ViewModel中的一個GridViewColumnCollection。

這是一個非常混亂的解決方法,我敢肯定,如果被錯誤的眼睛看到,我會被解僱。我需要做的就是類似這樣的東西:

<telerik:RadGridView.CellTemplateSelector> 
    <local:MyTemplateSelector> 
     <local:MyTemplateSelector.NormalTemplate> 
      <DataTemplate> 
       ...Some [Normal] template... 
      </DataTemplate> 
     </local:MyTemplateSelector.NormalTemplate 
     <local:MyTemplateSelector.DropdownTemplate> 
      <DataTemplate> 
       ...Some [ComboBox] template... 
      </DataTemplate> 
     </local:MyTemplateSelector.DropdownTemplate> 
    </local:MyTemplateSelector> 
</telerik:RadGridView.CellTemplateSelector> 

老實說,我不知道爲什麼這個RadGridView使得它如此難以改變CellTemplate,因爲這樣可以防止您更改一個共同的屬性,如前景色標籤在Cell本身的ContentTemplate中找到..關於我能做什麼的任何想法?

+1

請更新這個帖子,要麼接受一個答案,或通過評論提供細節或編輯爲什麼它不回答你問題 –

回答

0

所以提前,林不知道有關「正確」的方式做到這一點,因爲WPF似乎如此高度情境化,這確實似乎是什麼適合你的特殊需要最好的情況下。

我自己的方式來這可能issnt最值得推薦的,但我需要集中所有styleselector和cellselector變種,並使用代碼編寫邏輯,而不是在XAML。

我去過的東西類似於telerik.StyleRule方法,但更簡單,就像在代碼隱藏atm中一樣。而不是可能在共享xaml資源庫中定義或在每個視圖中定義,我使用單個文件夾中的類,並簡單地在xaml中使用CLR命名空間和ect來調用它(雖然可以在代碼隱藏中設置)

它使用的列名,很容易使用來定義條件:

Expression<Func<BaseModelForAllTableObjectClasses,bool>> 

實際資源(的DataTemplate和風格),它使用共享XAML資源庫中定義,但如果需要,這些可以說是相當一般。

它有點粗糙,如果有興趣,請留下評論。

MyProject。CellStyleSelector

/// <summary> 
/// Used in CellStyleSelector and its inheriting classes. 
/// 
/// Wouldnt be too hard to modify to allow declarative if highly desired. 
/// 
/// NOTE - tried extending telerik.StyleRule, issues with setting condition value 
/// </summary> 
public class CellStyleRule 
{ 
    /* 
    object _Value; 
    public object Value 
    { 
     get 
     { 
      return this._Value; 
     } 
     set 
     { 
      this._Value = value; 
     } 
    } 
    */ 

    // NOTE - if needing to widen use case, use <T>, but ONLY if needed 
    Expression<Func<BaseDataModel, bool>> _Condition; 
    public Expression<Func<BaseDataModel, bool>> Condition 
    { 
     get 
     { 
      return this._Condition; 
     } 
     set 
     { 
      this._Condition = value; 
     } 
    } 


    Style _Style; 
    public Style Style 
    { 
     get 
     { 
      return this._Style; 
     } 
     set 
     { 
      this._Style = value; 
     } 
    } 



    public CellStyleRule(Expression<Func<BaseDataModel, bool>> c, string s) 
    { 
     var d = App.Current.FindResource(s); 
     if (d != null) 
      this.Style = d as Style; 
     else 
      throw new Exception("No such style resource as '" + s + "'"); 

     // Value = v; 
     Condition = c; 
    } 



    /* 
    public CellStyleRule(string c, string s) 
    { 
     var d = App.Current.FindResource(s); 
     if (d != null) 
      Style = d as Style; 
     else 
      throw new Exception("No such style resource as '" + s + "'"); 

     // TODO - test - guessing at how to do this based on telerik classes 

     // Value = v; 
     Telerik.Windows.Data.ExpressionTypeConverter converter = new Telerik.Windows.Data.ExpressionTypeConverter(); 
     Condition = (System.Linq.Expressions.Expression) converter.ConvertFromString(c); 

     c.ToString(); 
    } 
    */ 
} 






// NOTE IMPORTANT - use of xaml defined ConditionalStyleSelectors is not a bad alternative approach, though diferent selector 
//     for each column it could all be defined in same resource dictionary 
//    - problem is that issues encountered with Enums, so decided on this approach instead 

/// <summary> 
/// A variation of StyleSelecter not unlike telerik:ConditionalStyleSelector. 
/// 
/// However, rules are defined using a Dictionary<string,CellStyle> to distinct columns and use same 
/// selector, with rules (currently) defined not declarativly (xaml), but in inheriting classes, 
/// typically one for each entity type requiring a RadGridView and conditional styling. 
/// </summary> 
public abstract class CellStyleSelector : StyleSelector 
{ 
    // public Dictionary<String, Dictionary<string, Style>> conditions { get; set; } 


    // Use a Dictionary mapping X columns to individual Lists of rules to check 
    Dictionary<string, List<CellStyleRule>> _Rules; 
    public Dictionary<string, List<CellStyleRule>> Rules 
    { 
     get 
     { 
      if (this._Rules == null) 
      { 
       this._Rules = new Dictionary<string, List<CellStyleRule>>(); 
      } 

      return this._Rules; 
     } 
     set { this._Rules = value; } 
    } 

    public override Style SelectStyle(object item, DependencyObject container) 
    { 
     if (item is BaseDataModel) 
     { 
      GridViewCell cell = container as GridViewCell; 
      var currentColumn = cell.Column as GridViewDataColumn; 
      string key = currentColumn.DataMemberBinding.Path.Path; 


      if (Rules.ContainsKey(key)) 
      { 
       foreach (CellStyleRule rule in Rules[key]) 
       { 
        // string debug = DebugHelper.Debug(rule.Condition); 

        // REVERTED NOTE - if just Func<> 
        // if (rule.Condition((BaseDataModel)item)) 

        // NOTE - if Expression<Func<>>, first compile then pass in param 
        if (rule.Condition.Compile()((BaseDataModel)item)) 
        { 
         return rule.Style; 
        } 
       } 
      } 

     } 
     return null; 
    } 


} 


/// <summary> 
/// Used in CellCellTemplateRuleSelector and its inheriting classes. 
/// 
/// Wouldnt be too hard to modify to allow declarative if highly desired. 
/// </summary> 
public class CellTemplateRule 
{ 
    /* 
    object _Value; 
    public object Value 
    { 
     get 
     { 
      return this._Value; 
     } 
     set 
     { 
      this._Value = value; 
     } 
    } 
    */ 

    // NOTE - if needing to widen use case, use <T>, but ONLY if needed 
    Expression<Func<BaseDataModel, bool>> _Condition; 
    public Expression<Func<BaseDataModel, bool>> Condition 
    { 
     get 
     { 
      return this._Condition; 
     } 
     set 
     { 
      this._Condition = value; 
     } 
    } 


    DataTemplate _Template; 
    public DataTemplate Template 
    { 
     get 
     { 
      return this._Template; 
     } 
     set 
     { 
      this._Template = value; 
     } 
    } 



    public CellTemplateRule(Expression<Func<BaseDataModel, bool>> c, string s) 
    { 
     var d = App.Current.FindResource(s); 
     if (d != null) 
      this.Template = d as DataTemplate; 
     else 
      throw new Exception("No such template resource as '" + s + "'"); 

     Condition = c; 
    } 


} 

// Implementing class, used for any licence table 
public class LicenceTableCellStyleSelector : CellStyleSelector 
{ 
    public LicenceTableCellStyleSelector() 
     : base() 
    { 
     this.Rules = new Dictionary<string, List<CellStyleRule>>() 
     { 
      { "LicenceStatus" , new List<CellStyleRule>() 
       { 
        // Always most specific rules at top 

        // Error catcher, leave in as visual cue just in case 
        { new CellStyleRule(x => ((Licence)x).LicenceExpires < DateTime.Now && ((Licence)x).LicenceStatus != LicenceStatus.Expired, "CellStyle_Licence_ExpiryMismatch") } , 

        { new CellStyleRule(x => ((Licence)x).LicenceStatus == LicenceStatus.Active, "CellStyle_Status_Active") } , 

        // Same as != Active, as only those would through this far 
        { new CellStyleRule(x => x != null, "CellStyle_Status_Inactive") } , 


       } 
      }, 

      { "LicenceType" , new List<CellStyleRule>() 
       {       
        { new CellStyleRule(x => ((Licence)x).LicenceType == LicenceType.Full, "CellStyle_Licence_Full") } , 
        { new CellStyleRule(x => ((Licence)x).LicenceType == LicenceType.Upgrade, "CellStyle_Licence_Upgrade") } , 

        // Timed, uses fallthrough so no need to actually check unless wanting to distinct types of timed 
        { new CellStyleRule(x => x != null, "CellStyle_Licence_Timed") } , 
       } 
      }, 

      { "LicenceExpired" , new List<CellStyleRule>() 
       {       
        { new CellStyleRule(x => ((Licence)x).LicenceExpires < DateTime.Now && ((Licence)x).LicenceStatus != LicenceStatus.Expired, "CellStyle_Licence_ExpiryMismatch") }, 
        { new CellStyleRule(x => ((Licence)x).LicenceExpires < ((Licence)x).LicenceIssued, "CellStyle_Licence_ExpiryMismatch") } , 
       } 
      }, 

      { "LicenceIssued" , new List<CellStyleRule>() 
       { 
        { new CellStyleRule(x => ((Licence)x).LicenceExpires < ((Licence)x).LicenceIssued, "CellStyle_Licence_ExpiryMismatch") } , 
       } 
      } 
     }; 
    } 
} 

CellTemplateSelector

/// <summary> 
/// Used in CellCellTemplateRuleSelector and its inheriting classes. 
/// 
/// Wouldnt be too hard to modify to allow declarative if highly desired. 
/// </summary> 
public class CellTemplateRule 
{ 
    /* 
    object _Value; 
    public object Value 
    { 
     get 
     { 
      return this._Value; 
     } 
     set 
     { 
      this._Value = value; 
     } 
    } 
    */ 

    // NOTE - if needing to widen use case, use <T>, but ONLY if needed 
    Expression<Func<BaseDataModel, bool>> _Condition; 
    public Expression<Func<BaseDataModel, bool>> Condition 
    { 
     get 
     { 
      return this._Condition; 
     } 
     set 
     { 
      this._Condition = value; 
     } 
    } 


    DataTemplate _Template; 
    public DataTemplate Template 
    { 
     get 
     { 
      return this._Template; 
     } 
     set 
     { 
      this._Template = value; 
     } 
    } 



    public CellTemplateRule(Expression<Func<BaseDataModel, bool>> c, string s) 
    { 
     var d = App.Current.FindResource(s); 
     if (d != null) 
      this.Template = d as DataTemplate; 
     else 
      throw new Exception("No such template resource as '" + s + "'"); 

     Condition = c; 
    } 


} 

// NOTE IMPORTANT - use of xaml defined ConditionalTemplateSelectors is not a bad alternative approach, though diferent selector 
//     for each column it could all be defined in same resource dictionary 
//    - problem is that issues encountered with Enums, so decided on this approach instead 

/// <summary> 
/// See CellStyleSelector, this is a variant used with the CellTemplateSelector property 
/// </summary> 
public abstract class CellTemplateSelector : DataTemplateSelector 
{ 
    // public Dictionary<String, Dictionary<string, Template>> conditions { get; set; } 


    // Use a Dictionary mapping X columns to individual Lists of rules to check 
    Dictionary<string, List<CellTemplateRule>> _Rules; 
    public Dictionary<string, List<CellTemplateRule>> Rules 
    { 
     get 
     { 
      if (this._Rules == null) 
      { 
       this._Rules = new Dictionary<string, List<CellTemplateRule>>(); 
      } 

      return this._Rules; 
     } 
     set { this._Rules = value; } 
    } 

    public override DataTemplate SelectTemplate(object item, DependencyObject container) 
    { 
     if (item is BaseDataModel) 
     { 
      GridViewCell cell = container as GridViewCell; 
      var currentColumn = cell.Column as GridViewDataColumn; 
      string key = currentColumn.DataMemberBinding.Path.Path; 


      if (Rules.ContainsKey(key)) 
      { 
       foreach (CellTemplateRule rule in Rules[key]) 
       { 
        // string debug = DebugHelper.Debug(rule.Condition); 

        // REVERTED NOTE - if just Func<> 
        // if (rule.Condition((BaseDataModel)item)) 

        // NOTE - if Expression<Func<>>, first compile then pass in param 
        if (rule.Condition.Compile()((BaseDataModel)item)) 
        { 
         return rule.Template; 
        } 
       } 
      } 

     } 
     return null; 
    } 


} 


// Implementing class for a different table, though RadGridView can use both CellStyleSelector and CellTemplateSelector at same time, i just didnt need yet 
public class OrderDongleWrapTableCellTemplateSelector : CellTemplateSelector 
{ 
    public OrderDongleWrapTableCellTemplateSelector() 
     : base() 
    { 
     this.Rules = new Dictionary<string, List<CellTemplateRule>>() 
     { 
      { "ReplacedDongle.DongleID" , new List<CellTemplateRule>() 
       { 
        // Always most specific rules at top 


        { new CellTemplateRule(x => ((OrderDongleWrap)x).IsReplacementDongle == false , "CellTemplate_OrderDongleWrap_NotReplacementDongler_NAField") }, 


       } 
      }, 
      { "ReplacedDongleStatus" , new List<CellTemplateRule>() 
       { 
        // Always most specific rules at top 


        { new CellTemplateRule(x => ((OrderDongleWrap)x).IsReplacementDongle == false , "CellTemplate_OrderDongleWrap_NotReplacementDongler_NAField") }, 


       } 
      }, 
      /* 
      * // REVERTED - better to just set to UNKNOWN CUSTOMER explicitly before shown in table, rather than overwrite datatemplate 
      { "Customer.CustomerName" , new List<CellTemplateRule>() 
       { 
        // Always most specific rules at top 


        { new CellTemplateRule(x => ((OrderDongleWrap)x).Customer == null || ((OrderDongleWrap)x).Customer.CustomerID == 1 , "CellTemplate_OrderDongleWrap_UnknownCustomerField") }, 


       } 
      }, 
      */ 
     }; 
    } 
} 

XAML資源

<!-- 
    The following are styles applied to cells or rows in RadGridViews based on StyleSelectors 

    First is generic colours, which are inherited by semi-generic flag styles, which optionally 
    can be further inherited by table-specific styles (which can include tooltips and other properties) 

    As a general rule, no colour style is applied directly, instead using semi-generic or table-specific 
    styles so as to make changes easier to manage. Which style actually matches which condition is 
    entirely the responsibility of the StyleSelector. 

    https://en.wikipedia.org/wiki/Web_colors 

    http://www.flounder.com/csharp_color_table.htm 

    <Style x:Key="CellStyle_" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}"> 
     <Setter Property="Foreground" Value=""/> 
    </Style> 
--> 
<!-- 
    NOTE: to get cell text underlining to work, see http://www.telerik.com/forums/underline-cell-contents 
--> 

<Style x:Key="CellStyle_Green" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}"> 
    <Setter Property="Foreground" Value="Green"/> 
</Style> 
<Style x:Key="CellStyle_DarkGreen" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}"> 
    <Setter Property="Foreground" Value="DarkGreen"/> 
</Style> 
<Style x:Key="CellStyle_ExtraDarkGreen" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}"> 
    <Setter Property="Foreground" Value="#004000"/> 
</Style> 
<Style x:Key="CellStyle_MediumBlue" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}"> 
    <Setter Property="Foreground" Value="MediumBlue"/> 
</Style> 
<Style x:Key="CellStyle_DarkBlue" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}"> 
    <Setter Property="Foreground" Value="DarkBlue"/> 
</Style> 
<Style x:Key="CellStyle_Purple" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}"> 
    <Setter Property="Foreground" Value="Purple"/> 
</Style> 
<Style x:Key="CellStyle_Red" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}"> 
    <Setter Property="Foreground" Value="Red"/> 
</Style> 
<Style x:Key="CellStyle_Crimson" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}"> 
    <Setter Property="Foreground" Value="Crimson"/> 
</Style> 
<Style x:Key="CellStyle_DarkOrange" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}"> 
    <Setter Property="Foreground" Value="DarkOrange"/> 
</Style> 
<Style x:Key="CellStyle_Silver" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}"> 
    <Setter Property="Foreground" Value="Silver"/> 
</Style> 
<Style x:Key="CellStyle_DarkGray" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}"> 
    <Setter Property="Foreground" Value="DarkGray"/> 
</Style> 
<Style x:Key="CellStyle_Gray" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}"> 
    <Setter Property="Foreground" Value="Gray"/> 
</Style> 

<Style x:Key="CellStyle_RedBG_WhiteFG" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}"> 
    <Setter Property="Background" Value="Red"/> 
    <Setter Property="Foreground" Value="White"/> 
</Style> 



<Style x:Key="CellStyle_Muted" TargetType="telerik:GridViewCell" BasedOn="{StaticResource CellStyle_Gray}" /> 
<Style x:Key="CellStyle_Status_Active" TargetType="telerik:GridViewCell" BasedOn="{StaticResource CellStyle_ExtraDarkGreen}" /> 
<Style x:Key="CellStyle_Status_Inactive" TargetType="telerik:GridViewCell" BasedOn="{StaticResource CellStyle_Gray}" /> 
<Style x:Key="CellStyle_Status_Warning" TargetType="telerik:GridViewCell" BasedOn="{StaticResource CellStyle_Crimson}" /> 
<Style x:Key="CellStyle_Status_Error" TargetType="telerik:GridViewCell" BasedOn="{StaticResource CellStyle_Red}" /> 
<Style x:Key="CellStyle_Status_ErrorBG" TargetType="telerik:GridViewCell" BasedOn="{StaticResource CellStyle_RedBG_WhiteFG}" /> 

<Style x:Key="CellStyle_Dongle_InactiveWithActiveLicences" TargetType="telerik:GridViewCell" BasedOn="{StaticResource CellStyle_Status_Error}"> 
    <Setter Property="ToolTip" Value="This Dongle is not Active, but has Licences which are"/> 
</Style> 

<Style x:Key="CellStyle_Licence_Full" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}" /> 
<Style x:Key="CellStyle_Licence_Upgrade" TargetType="telerik:GridViewCell" BasedOn="{StaticResource CellStyle_DarkBlue}" /> 
<Style x:Key="CellStyle_Licence_Timed" TargetType="telerik:GridViewCell" BasedOn="{StaticResource CellStyle_Purple}" /> 
<Style x:Key="CellStyle_Licence_ExpiryMismatch" TargetType="telerik:GridViewCell" BasedOn="{StaticResource CellStyle_Status_ErrorBG}"> 
    <Setter Property="ToolTip" Value="Expiry doesnt match Status or Issued Date"/> 
</Style> 


<Style x:Key="CellStyle_OrderDongleWrap_NotReplacementDongler_NAField" TargetType="telerik:GridViewCell" BasedOn="{StaticResource CellStyle_Gray}"> 
    <Setter Property="Content" Value="N/A"/> 
</Style> 
<Style x:Key="CellStyle_OrderDongleWrap_UnknownCustomerField" TargetType="telerik:GridViewCell" BasedOn="{StaticResource CellStyle_Gray}"> 
    <Setter Property="Content" Value="N/A"/> 
</Style> 






<DataTemplate x:Key="CellTemplate_OrderDongleWrap_NotReplacementDongler_NAField"> 
    <TextBlock>N/A</TextBlock> 
</DataTemplate> 

我mplementing的XAML CellStyleSelector

<telerik:RadGridView.Resources>        
    <gridviewstyleselectors:LicenceTableCellStyleSelector x:Key="LicenceTableCellStyleSelector" /> 
</telerik:RadGridView.Resources> 

<telerik:RadGridView.Columns> 
    <telerik:GridViewDataColumn DataMemberBinding="{Binding DongleID}" Header="DongleID" IsReadOnly="True" /> 
    <telerik:GridViewDataColumn DataMemberBinding="{Binding LicenceSerial}" Header="Serial" IsReadOnly="True" /> 
    <telerik:GridViewDataColumn DataMemberBinding="{Binding LicenceStatus}" Header="Status" IsReadOnly="True" 
           CellStyleSelector="{StaticResource LicenceTableCellStyleSelector}"/> 
    <telerik:GridViewDataColumn DataMemberBinding="{Binding Product.ProductName}" Header="Product" IsReadOnly="True" /> 
    <telerik:GridViewDataColumn DataMemberBinding="{Binding LicenceType}" Header="Licence Type" 
           CellStyleSelector="{StaticResource LicenceTableCellStyleSelector}" IsReadOnly="True" /> 
    <telerik:GridViewDataColumn DataMemberBinding="{Binding LicenceIssued}" Header="Issued" 
           CellStyleSelector="{StaticResource LicenceTableCellStyleSelector}" IsReadOnly="True" /> 
    <telerik:GridViewDataColumn DataMemberBinding="{Binding LicenceExpires}" Header="Expiry" 
           CellStyleSelector="{StaticResource LicenceTableCellStyleSelector}" IsReadOnly="True" /> 
</telerik:RadGridView.Columns> 

伊夫只有在一種情況下CellTemplateSelector,到目前爲止,那就是:

實施的XAML CellTemplateSelector

<telerik:RadGridView.Columns> 
    <telerik:GridViewDataColumn DataMemberBinding="{Binding Dongle.DongleID}" Header="DongleID"> 
     <telerik:GridViewDataColumn.AggregateFunctions> 
      <telerik:CountFunction ResultFormatString="{}Dongles: {0}" /> 
     </telerik:GridViewDataColumn.AggregateFunctions> 
    </telerik:GridViewDataColumn> 
    <telerik:GridViewDataColumn DataMemberBinding="{Binding OrderRows.Count}" Header="Licences" 
           CellStyleSelector="{StaticResource OrderDongleWrapTableCellStyleSelector}" 
           CellTemplateSelector="{StaticResource OrderDongleWrapTableCellTemplateSelector}" /> 
    <telerik:GridViewDataColumn DataMemberBinding="{Binding Customer.CustomerName}" Header="Customer" 
           CellStyleSelector="{StaticResource OrderDongleWrapTableCellStyleSelector}" 
           CellTemplateSelector="{StaticResource OrderDongleWrapTableCellTemplateSelector}" /> 
    <telerik:GridViewDataColumn DataMemberBinding="{Binding IsReplacementDongle}" Header="Replacement?" 
           IsVisible="False" 
           CellStyleSelector="{StaticResource OrderDongleWrapTableCellStyleSelector}" 
           CellTemplateSelector="{StaticResource OrderDongleWrapTableCellTemplateSelector}" /> 
    <telerik:GridViewDataColumn DataMemberBinding="{Binding IsEducationalDongle}" Header="Educational?" 
           CellStyleSelector="{StaticResource OrderDongleWrapTableCellStyleSelector}" 
           CellTemplateSelector="{StaticResource OrderDongleWrapTableCellTemplateSelector}" /> 
    <telerik:GridViewDataColumn DataMemberBinding="{Binding ReplacedDongle.DongleID}" Header="Replaced" 
           CellStyleSelector="{StaticResource OrderDongleWrapTableCellStyleSelector}" 
           CellTemplateSelector="{StaticResource OrderDongleWrapTableCellTemplateSelector}" /> 
    <telerik:GridViewDataColumn DataMemberBinding="{Binding ReplacedDongleStatus}" Header="Replacement Status" 
           CellStyleSelector="{StaticResource OrderDongleWrapTableCellStyleSelector}" 
           CellTemplateSelector="{StaticResource OrderDongleWrapTableCellTemplateSelector}"/> 
    <telerik:GridViewDataColumn DataMemberBinding="{Binding OrderRows[0].OrderRowCosts[0].CostValue }" Header="Sub-Total (AUD)"> 
    </telerik:GridViewDataColumn> 

</telerik:RadGridView.Columns> 

所以這是我自己的方法,但它集中了條件邏輯和樣式變爲每個目標類(GridView ItemsSource對象)只有1個cs類,而所有GridView只有1個資源表。

你可以很容易地重構爲聲明如果你想,我看到它是可能的但沒有需要或想要這樣做,所以我沒有。

你也可以重構所以規則爲單個cs類級聯所有表,甚至是在動態聲明樣式..? (由新的風格和添加屬性?)

在任何情況下,我希望這可以幫助別人這個問題

+0

嗯,這是一個有趣的方法..我仍然需要動態地添加單元格,我對單元格的樣式也沒有太大興趣,因爲我在內容 –

+0

這些螞蟻依賴於所有聲明式列,如果事實上這種方法部分地被創建以允許更多地關注代碼隱藏,但是我們仍然使用大多數表的聲明列,但是使用動態列來進行成本計算,因爲我們有一個帶有ProductCosts和其他表的貨幣DB表允許動態數量的成本fi現場和擴展欄。如果在代碼隱藏中添加這些屬性,這些選擇器的工作方式將完全相同,並且CellTemplate選擇器會根據數據訪問列表綁定 –

+0

更改DataTemplate我假設您從OP中設置了生成實際列的設置,所以它只是CellTemplateSelectors等需要更有活力?如果是這樣,那麼只需在代碼隱藏中添加我在xaml中定義的屬性即可。如果你願意的話,我也可以分享我用來添加我的動態費用列的代碼,儘管這裏提供了更好的例子。 –