边框背景DataTrigger没有改变

时间:2014-09-30 15:25:09

标签: c# wpf xaml

想要根据枚举值更改边框的属性:

可见性和不透明度之类的东西似乎有所改变,但不是背景或边界......

<Border Background="{StaticResource Gradient3}" 
        BorderBrush="Green" BorderThickness="1"  
        Height="25" Width="25" CornerRadius="25">            
    <Border.Style>
        <Style TargetType="{x:Type Border}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Operation}" Value="0"> <!-- Idle -->
                    <Setter Property="Visibility" Value="Hidden"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding Operation}" Value="1"> <!-- Failure -->
                    <Setter Property="BorderBrush" Value="Red"></Setter>                            
                </DataTrigger>
                <DataTrigger Binding="{Binding Operation}" Value="2"> <!-- Success-->
                    <Setter Property="BorderBrush" Value="Purple"></Setter>
                    <Setter Property="Background" Value="Purple"></Setter>                            
                </DataTrigger>
                <DataTrigger Binding="{Binding Operation}" Value="3"> <!-- Running -->
                    <Setter Property="BorderBrush" Value="Purple"></Setter>
                    <DataTrigger.EnterActions>
                        <BeginStoryboard Name="abc">
                            <Storyboard>
                                <DoubleAnimation                                       
                                    Storyboard.TargetProperty="Opacity"
                                    From="0" To="1" Duration="0:0:1" 
                                    RepeatBehavior="Forever"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                    <DataTrigger.ExitActions>
                        <RemoveStoryboard BeginStoryboardName="abc"/>
                    </DataTrigger.ExitActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
    <TextBlock FontWeight="ExtraBold" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center">?</TextBlock>
</Border>

此代码触发它:

private void ExecuteTestConnectionCommand(ConnectionString connection)
{
    ThreadPool.QueueUserWorkItem(
         o =>
         {
             Operation = OperationStatus.Running;
             try
             {
                 _connectionTester.Connect(connection);
                 IsBusy = true;
                 Operation = OperationStatus.Success;
             }
             catch (Exception e)
             {
                 MessageBox.Show(e.Message);
                 Operation = OperationStatus.Failure;
             }
             // Do something
             DispatcherHelper.CheckBeginInvokeOnUI(
               () =>
               {
                   // Dispatch back to the main thread
                   IsBusy = false;
                   // without this the button stays disabled until a ui interaction occurs
                   System.Windows.Input.CommandManager.InvalidateRequerySuggested();
               });
         });
}

1 个答案:

答案 0 :(得分:1)

您正在Background上直接设置Border。这称为本地值,本地值优先于任何其他源设置的值,包括样式/模板触发器和设置器。尝试从Background="{StaticResource Gradient3}"声明中删除Border,向Setter添加Style以设置默认背景:

<Border.Style>
  <Style TargetType="{x:Type Border}">
    <Setter Property="Background" Value="{StaticResource Gradient3}" />
    ...

您需要对要使用触发器更新的任何其他值执行相同的操作,例如BorderBrush