开发者

wpf style trigger on dynamic path

In the following style, is there a way to make the Binding Path generic so that this style can be used by multiple consumers, each supplying a different binding path?

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


You can derive from Style to create a shorthand notation that looks like this:

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

and an implementation for this example might be:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜