开发者

Binding the background colour of a control using a trigger in WPF/XAML

Okay, first off I have no experience of WPF whatsoever so please bear with me and apologies if my terminology is a little wayward... ;)

The following code snippet is part of a WPF application that I have inherited. The trigger governs whether mandatory fields on a particular form are highlighted or not. The code works but the highlighting seems to apply to the control and the border (??) which contains it.

<ItemsControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:cal="clr-namespace:Caliburn.PresentationFramework.ApplicationModel;assembly=Caliburn.PresentationFramework"
              x:Class="company.product.Jobs.JobParametersEditor"
              IsTabStop="False">

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <DockPanel MinHeight="30">
                <TextBlock Text="{Binding DisplayName, Mode=OneWay}"
                           DockPanel.Dock="Left"
                           VerticalAlignment="Center"
                           MinWidth="120"
                           Margin="6,0" />
                <Border>
                    <Border.Style>
                        <Style TargetType="{x:Type Border}">
                            <Setter Property="Background"
                                    Value="{x:Null}" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsValid}"
                                             Value="False">
                                    <Setter Property="Background"
                                        Value="Red" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>

                    <ContentControl cal:View.Model="{Binding ValueEditor}"
                                    ToolTip="{Binding ToolTip}"
                                    IsTabStop="False"
                                 开发者_Python百科   MinHeight="19"
                                    VerticalAlignment="Center"
                                    HorizontalAlignment="Stretch" />
                </Border>
            </DockPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

The result is a bit clunky so I would like to restrict the highlighting to the control only but I can't figure out how to do it. I've tried moving the trigger so that it applies to the ContentControl instead of the Border but that didn't work and fiddling about with border margins, padding and thickness hasn't had any effect either.

Could anybody enlighten me as to how to accomplish this?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜