2010-01-26 78 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>