将Setter的值设置为附加属性

时间:2010-02-22 22:40:24

标签: c# wpf data-binding

我需要能够将setter的Value设置为附加属性,但是我很难提出语法。这就是我的触发器:

<DataTrigger Binding="{Binding Path=IsDisabled}" Value="True">
    <Setter TargetName="LayoutRoot" Property="Opacity" Value="0.3" />
    <Setter TargetName="LayoutRoot" Property="Background" Value="Interaction:Behaviors.OriginalBgBrush" />
</DataTrigger>

其中Interaction:Behaviors.OriginalBgBrush是在LayoutRoot上设置的附加属性,而LayoutRoot是边框。我必须这样做,因为在此之上的标准触发器设置背景,但是我无法同时检查模板和数据触发器的值。下面是整个模板,其中MultiTrigger是设置我不希望它的背景的模板。此外,背景颜色在单独的样式中定义,该样式使用此ControlTemplate作为其“模板属性”。

<ControlTemplate x:Key="StandardRowStyle" TargetType="tk:DataGridRow">
    <Border x:Name="LayoutRoot" Margin="0,0,0,-1" MinHeight="23" Interaction:Behaviors.OriginalBgBrush="{TemplateBinding Background}" PreviewMouseRightButtonDown="DataGridRow_SimpleMouseDown" PreviewMouseLeftButtonDown="DataGridRow_SimpleMouseDown" Background="{TemplateBinding Background}" BorderBrush="{StaticResource ElementBorderBrush}" BorderThickness="1" CornerRadius="0" SnapsToDevicePixels="True">
        <toolkit:SelectiveScrollingGrid x:Name="DGR_SelectiveScrollingGrid">
            <toolkit:SelectiveScrollingGrid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </toolkit:SelectiveScrollingGrid.ColumnDefinitions>

            <toolkit:SelectiveScrollingGrid.RowDefinitions>
                <RowDefinition Height="*" MinHeight="23"/>
                <RowDefinition Height="Auto"/>
            </toolkit:SelectiveScrollingGrid.RowDefinitions>

            <toolkit:DataGridCellsPresenter Grid.Column="1" Grid.Row="0" x:Name="DGR_CellsPresenter" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
            <toolkit:DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" Visibility="{TemplateBinding DetailsVisibility}" toolkit:SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding Path=AreRowDetailsFrozen, Converter={x:Static tk:DataGrid.RowDetailsScrollingConverter}, ConverterParameter={x:Static tk:SelectiveScrollingOrientation.Vertical}, RelativeSource={RelativeSource AncestorType={x:Type tk:DataGrid}}}" />
            <!--<toolkit:DataGridRowHeader Grid.Column="2" Grid.RowSpan="2" toolkit:SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding Path=HeadersVisibility, Converter={x:Static tk:DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static tk:DataGridHeadersVisibility.Row}, RelativeSource={RelativeSource AncestorType={x:Type tk:DataGrid}}}"/>-->
        </toolkit:SelectiveScrollingGrid>
    </Border>
    <ControlTemplate.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsMouseOver" Value="True" />
                <Condition Property="IsSelected" Value="False" />
                <Condition Property="DetailsVisibility" Value="Collapsed" />     
            </MultiTrigger.Conditions>
            <Setter TargetName="LayoutRoot" Property="Background" Value="{StaticResource InactiveSelectedBackgroundBrush}" />
            <Setter TargetName="LayoutRoot" Property="BorderBrush" Value="{StaticResource InactiveOuterBorderBrush}" />
            <Setter TargetName="LayoutRoot" Property="CornerRadius" Value="5" />
        </MultiTrigger>            
        <DataTrigger Binding="{Binding Path=IsDisabled}" Value="True">
            <Setter TargetName="LayoutRoot" Property="Opacity" Value="0.3" />
            <!-- Background needs to be changed back to default here -->
        </DataTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

1 个答案:

答案 0 :(得分:1)

只需使用ElementName绑定:

<Setter TargetName="LayoutRoot"
        Property="Background"
        Value="{Binding Path=(Interaction:Behaviors.OriginalBgBrush), ElementName=LayoutRoot}" />