任何有关此问题的帮助将不胜感激,因为我整天都在四处寻找有关该区域的答案!
通过将合并字典添加到 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>
然后我添加了如上所示的 BasedOn 属性,为什么我必须这样做才能将样式应用于此列表框?其他列表框和不同的控件自动选择了这个?
无论如何,您会看到我有一个针对此列表框的数据触发器,当请求状态 id = 7 时,它应该更改背景颜色。没有基于属性的行成功将颜色更改为红色。应用样式时,它永远不会改变颜色,并且始终应用模板中的橙色。
帮助?????
非常感激,
标记