2010-01-26 70 views
0

在这个问题上的任何帮助将我整天一直在寻找周围围绕这一区域的答案将不胜感激!应用程序样式应用时,WPF DataTrigger不会触发吗?

我已经在合并字典的App.xaml中添加应用了全球风格,我的WPF应用程序。这已经像应用程序那样在应用程序中应用了风格,但是有很多事情我已经完成了,但我并不完全理解。

我可以给你的风格正在申请是否会帮助的代码,但它是相当大,因此尽管它最好不要堵塞这个职位。该样式为每个列表框项目应用背景颜色,并将鼠标悬停在动画和颜色更改上。这种风格并没有在我的应用程序应用到一对夫妇的列表框的,虽然,下面一个代码示例:

<StackPanel Margin="0,15,0,0" Width="auto" HorizontalAlignment="Left"> 
    <StackPanel.Resources> 
     <converter:IntToBoolConverter x:Key="intToBoolConverter" /> 
     <converter:BoolToVisibilityConverter x:Key="boolToVisibilityConverter" /> 
    </StackPanel.Resources> 
    <Label Content="Required Vehicles" HorizontalAlignment="Center" FontWeight="Bold" /> 
    <ListBox x:Name="lstVehicleRequests" ItemsSource="{Binding VehicleRequests}" Width="auto" 
      IsSynchronizedWithCurrentItem="True"> 
     <ListBox.Resources> 
      <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource BaseListBoxItem}"> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding Path=RequestStatus.RequestStatusId}" Value="7"> 
         <Setter Property="Background"> 
          <Setter.Value> 
           <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
            <GradientStop Color="#FFFFFFFB" Offset="0" /> 
            <GradientStop Color="IndianRed" Offset="0.5" /> 
            <GradientStop Color="#FFFFFFFB" Offset="1" /> 
           </LinearGradientBrush> 
          </Setter.Value> 
         </Setter> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </ListBox.Resources> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Grid ShowGridLines="True"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="auto" /> 
         <ColumnDefinition Width="auto" /> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="auto" /> 
         <RowDefinition Height="4" /> 
        </Grid.RowDefinitions> 
        <StackPanel Margin="0,8,0,0"> 
         <TextBlock Margin="0,4,10,0" > 
         <Label Content="Coach Type" Width="120" /> 
         <ComboBox ItemsSource="{Binding DataContext.CoachTypes, 
            RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" 
            SelectedItem="{Binding CoachType}" DisplayMemberPath="Name" Width="100" /> 
         </TextBlock> 
         <TextBlock Margin="0,8,10,0"> 
          <Label Content="No of Passengers" Width="120" /> 
          <TextBox 
            keys:ValidKeys.Numeric="True" 
            Validation.ErrorTemplate="{StaticResource validationTemplate}" 
            Style="{StaticResource textBoxInError}" Width="50"> 
           <TextBox.Text> 
             <Binding Path="Passengers" 
                UpdateSourceTrigger="PropertyChanged"> 
              <Binding.ValidationRules> 
               <val:RegularExpressionRule 
                ErrorDescription="Please Enter a Numeric Size" 
                RegularExpression="^\d*$" /> 
              </Binding.ValidationRules> 
             </Binding> 
            </TextBox.Text> 
          </TextBox> 
         </TextBlock> 
         <TextBlock Margin="0,8,10,0"> 
          <Label Content="No of Drivers" Width="120" /> 
          <TextBox 
            keys:ValidKeys.Numeric="True" 
            Validation.ErrorTemplate="{StaticResource validationTemplate}" 
            Style="{StaticResource textBoxInError}" Width="50"> 
           <TextBox.Text> 
            <Binding Path="Drivers" 
             UpdateSourceTrigger="PropertyChanged"> 
             <Binding.ValidationRules> 
              <val:RegularExpressionRule 
               ErrorDescription="Please Enter a Numeric Size" 
               RegularExpression="^\d*$" /> 
              </Binding.ValidationRules> 
             </Binding> 
            </TextBox.Text> 
          </TextBox> 
         </TextBlock> 
         <TextBlock Margin="0,8,10,0"> 
         <Label Content="Positioning Feeder Drivers" Width="120" /> 
          <TextBox 
            keys:ValidKeys.Numeric="True" 
            Style="{StaticResource textBoxInError}" Width="50" MaxLength="3"> 
           <TextBox.Text> 
            <Binding Path="PositioningFeederDrivers" 
             UpdateSourceTrigger="PropertyChanged"> 
             <Binding.ValidationRules> 
              <val:RegularExpressionRule 
               ErrorDescription="Please Enter a Numeric Size" 
               RegularExpression="^\d*$" /> 
              </Binding.ValidationRules> 
             </Binding> 
            </TextBox.Text> 
          </TextBox> 
         </TextBlock> 
         <TextBlock Margin="0,8,10,0">      
         <Label Content="Wheelchair Access" Width="120" /> 
         <ComboBox Width="100" SelectedIndex="{Binding WheelchairAccess, 
            Converter={StaticResource intToBoolConverter}}"> 
          <ComboBoxItem Content="Not Required" /> 
          <ComboBoxItem Content="Required" /> 
         </ComboBox> 
         </TextBlock> 
         <TextBlock Margin="0,8,10,8"> 
         <Label Content="Trailer" Width="120" /> 
         <ComboBox Width="100" SelectedIndex="{Binding Trailer, 
            Converter={StaticResource intToBoolConverter}}"> 
          <ComboBoxItem Content="Not Required" /> 
          <ComboBoxItem Content="Required" /> 
         </ComboBox> 
         </TextBlock> 
        </StackPanel> 
       </Grid> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</StackPanel> 

我加入了支持算法FMP属性,你可以在上面看到,为什么我这样做是为了得到应用于此列表框的样式?其他列表框和不同的控件自动采摘了它?

无论如何,你会看到我有一个datatrigger针对这个列表框,当请求状态id = 7时它应该改变背景颜色。没有基于属性的行成功地将颜色改变为红色。应用样式时,不会更改颜色,并始终应用模板中的橙色。

帮助?????

大加赞赏,

马克

+0

如果颜色变为红色,而不支持算法FMP属性,那么你的风格被应用到你的列表框正确,否则它不会改变。模板中的橙色在哪里?您使用的不同类型的控件与此Style定义具有不同的行为? – Dave 2010-01-26 17:01:22

+0

将橙色颜色应用于从App.Xaml作为合并字典导入的baseListBox样式的此列表框。我们在整个应用程序,列表框,组合框,标签,文本框等中使用大部分控件......所有这些都从App.xaml中获取了样式,而无需添加BasedOn属性。我认为这可能与我们改变列表框的数据模板有关? 的主要问题是,与被包含的支持算法FMP财产,为什么会在后台不与datatrigger改变? 我已经包括以下 感谢基地stlye! – Schnoor 2010-01-27 10:56:59

回答

0
<Style x:Key="BaseListBoxItem" d:IsControlPart="True" TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="SnapsToDevicePixels" Value="true" /> 
     <Setter Property="OverridesDefaultStyle" Value="true" /> 
     <Setter Property="Padding" Value="3" /> 
     <Setter Property="Foreground" Value="{StaticResource OutsideFontColor}" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
        <ControlTemplate.Resources> 
         <Storyboard x:Key="HoverOn"> 
          <DoubleAnimation Duration="00:00:00.1000000" Storyboard.TargetName="BackgroundGradientOver" 
              Storyboard.TargetProperty="Opacity" To="0.73" /> 
         </Storyboard> 
         <Storyboard x:Key="HoverOff"> 
          <DoubleAnimation Duration="00:00:00.4000000" Storyboard.TargetName="BackgroundGradientOver" 
              Storyboard.TargetProperty="Opacity" To="0" /> 
         </Storyboard> 
         <Storyboard x:Key="SelectedOn"> 
          <DoubleAnimation Duration="00:00:00.1000000" Storyboard.TargetName="BackgroundGradientSelected" 
              Storyboard.TargetProperty="Opacity" To="0.84" /> 
          <DoubleAnimation Duration="00:00:00.1000000" Storyboard.TargetName="BackgroundGradientSelectedDisabled" 
              Storyboard.TargetProperty="Opacity" To="0.55" /> 
         </Storyboard> 
         <Storyboard x:Key="SelectedOff"> 
          <DoubleAnimation Duration="00:00:00.4000000" Storyboard.TargetName="BackgroundGradientSelected" 
              Storyboard.TargetProperty="Opacity" To="0" /> 
          <DoubleAnimation Duration="00:00:00.4000000" Storyboard.TargetName="BackgroundGradientSelectedDisabled" 
              Storyboard.TargetProperty="Opacity" To="0" /> 
         </Storyboard> 
        </ControlTemplate.Resources> 
        <Grid SnapsToDevicePixels="true"> 
         <Rectangle x:Name="BackgroundGradientOver" RadiusX="1" RadiusY="1" Stroke="{DynamicResource MouseOverBorderBrush}" 
            Opacity="0" Fill="{DynamicResource MouseOverBrush}"/> 
         <Rectangle x:Name="BackgroundGradientSelectedDisabled" RadiusX="1" RadiusY="1" Opacity="0" Fill="{DynamicResource 
          ListItemSelectedBrush}" Stroke="{DynamicResource ListItemSelectedBorderBrush}"/> 
         <Rectangle x:Name="BackgroundGradientSelected" Stroke="{DynamicResource PressedBorderBrush}" StrokeThickness="1" 
            RadiusX="1" RadiusY="1" Opacity="0" Fill="{DynamicResource PressedBrush}"> 

         </Rectangle> 
         <ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}" Margin="{TemplateBinding Padding}" 
              ContentTemplate="{TemplateBinding ContentTemplate}" 
              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" /> 
        </Grid> 
        <ControlTemplate.Triggers> 

         <Trigger Property="IsSelected" Value="true"> 
          <Trigger.ExitActions> 
           <BeginStoryboard Storyboard="{StaticResource SelectedOff}" x:Name="SelectedOff_BeginStoryboard" /> 
          </Trigger.ExitActions> 
          <Trigger.EnterActions> 
           <BeginStoryboard Storyboard="{StaticResource SelectedOn}" x:Name="SelectedOn_BeginStoryboard" /> 
          </Trigger.EnterActions> 

         </Trigger> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Trigger.ExitActions> 
           <BeginStoryboard Storyboard="{StaticResource HoverOff}" x:Name="HoverOff_BeginStoryboard" /> 
          </Trigger.ExitActions> 
          <Trigger.EnterActions> 
           <BeginStoryboard Storyboard="{StaticResource HoverOn}" /> 
          </Trigger.EnterActions> 
         </Trigger> 

         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}" /> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
相关问题