2011-01-21 84 views
1

在下列樣式中,是否有辦法使綁定路徑具有通用性,以便這種樣式可以被多個使用者使用,每個使用者都提供不同的綁定路徑?動態路徑上的wpf樣式觸發器

<Style x:Key="OptionalBackground" 
     TargetType="{x:Type DataPresenter:CellValuePresenter}" 
     BasedOn="{StaticResource OptionalFieldCellPresenter}"> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, 
             Path=Record.DataItem.IsEditAllowed}" 
        Value="False"> 
      <Setter Property="Background" Value="{StaticResource ReadOnlyField}" /> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 
+0

簡短的回答,沒有這個可能不行。儘管取決於你的情況,你有幾種選擇。從理論上講,你想怎麼做?語法是什麼樣子? – 2011-01-22 01:47:36

回答

0

您可以從Style派生創造一個速記符號,看起來像這樣:

<local:BackgroundStyle 
    x:Key="OptionalBackground" 
    TargetType="{x:Type DataPresenter:CellValuePresenter}" 
    BasedOn="{StaticResource OptionalFieldCellPresenter}" 
    Path="Record.DataItem.IsEditAllowed" 
    Value="{StaticResource ReadOnlyField}"/> 

,並在這個例子中的實現可能是:

public class BackgroundStyle : Style, ISupportInitialize 
{ 
    public string Path { get; set; } 
    public object Value { get; set; } 

    public void BeginInit() { } 

    public void EndInit() 
    { 
     var trigger = new DataTrigger 
     { 
      Binding = new Binding 
      { 
       Path = new PropertyPath(Path), 
       RelativeSource = new RelativeSource(RelativeSourceMode.Self) 
      }, 
     }; 
     trigger.Setters.Add(new Setter(Control.BackgroundProperty, Value)); 
     Triggers.Add(trigger); 
    } 
}