我如何更改所选wpf组合项的前景色

时间:2014-07-14 14:12:02

标签: c# wpf

我如何更改所选wpf组合项的前景色 我有一个组合项目的数据模板,文本块有前景黑色,其中项目被选中我喜欢前景变为白色

<ComboBox  view:ComboBoxDropdownBehavior.OpenDropDownAutomatically="True"  OverridesDefaultStyle="False"
                  ItemsSource="{Binding TopButtonMenuItems}"   IsEditable="False"  >
            <ComboBox.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">Transparent</SolidColorBrush>
            </ComboBox.Resources>
            <ComboBox.Template>
                <ControlTemplate TargetType="ComboBox">
                    <Grid>
                    <Border FocusVisualStyle="{x:Null}" x:Name="Border" Grid.ColumnSpan="2" CornerRadius="2" BorderThickness="1" BorderBrush="#647887">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CheckStates">
                                <VisualState x:Name="Checked">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <SolidColorBrush>#FFFFFF</SolidColorBrush>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unchecked" />
                                <VisualState x:Name="Indeterminate" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <!--<Border.Background>
                            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                <LinearGradientBrush.GradientStops>
                                    <GradientStopCollection>
                                        <GradientStop Color="#FFFFFF" />
                                        <GradientStop Color="#D3D8DD" Offset="1.0" />
                                    </GradientStopCollection>
                                </LinearGradientBrush.GradientStops>
                            </LinearGradientBrush>
                        </Border.Background>-->
                    </Border>
                    <StackPanel>                       
                        <view:SmartAgentPopup PlacementTarget="{Binding ElementName=ContentSite}" 
                                   x:Name="PART_Popup" Placement="Bottom" StaysOpen="True"
                                   IsOpen="{TemplateBinding IsDropDownOpen}"
                                   AllowsTransparency="True" Focusable="False" PopupAnimation="Slide"
                                   VerticalOffset="-1">
                            <Grid x:Name="DropDown" SnapsToDevicePixels="True" MaxHeight="{TemplateBinding MaxDropDownHeight}">
                                <Border Width="180" x:Name="DropDownBorder" SnapsToDevicePixels="True" BorderThickness="1" 
                                        BorderBrush="White"  Background="#E6E6E6">
                                    <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                                    <!--Margin="4,6,4,6"-->
                                </Border>
                            </Grid>

                    </view:SmartAgentPopup>
                    <Button >
                        <Button.Template>
                            <ControlTemplate>
                                <StackPanel>
                                    <VisualStateManager.VisualStateGroups>
                                        <VisualStateGroup Name ="CommonStates">
                                            <VisualState Name="Normal"/>
                                            <VisualState Name="MouseOver">
                                                <Storyboard>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ActiveImage" Storyboard.TargetProperty="Visibility">
                                                        <DiscreteObjectKeyFrame KeyTime="0" >
                                                            <DiscreteObjectKeyFrame.Value>
                                                                <Visibility>Collapsed</Visibility>
                                                            </DiscreteObjectKeyFrame.Value>
                                                        </DiscreteObjectKeyFrame>
                                                    </ObjectAnimationUsingKeyFrames>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MouseOverImage" Storyboard.TargetProperty="Visibility">
                                                        <DiscreteObjectKeyFrame KeyTime="0" >
                                                            <DiscreteObjectKeyFrame.Value>
                                                                <Visibility>Visible</Visibility>
                                                            </DiscreteObjectKeyFrame.Value>
                                                        </DiscreteObjectKeyFrame>
                                                    </ObjectAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </VisualState>
                                            <VisualState Name="Pressed">
                                                <Storyboard>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ActiveImage" Storyboard.TargetProperty="Visibility">
                                                        <DiscreteObjectKeyFrame KeyTime="0" >
                                                            <DiscreteObjectKeyFrame.Value>
                                                                <Visibility>Collapsed</Visibility>
                                                            </DiscreteObjectKeyFrame.Value>
                                                        </DiscreteObjectKeyFrame>
                                                    </ObjectAnimationUsingKeyFrames>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PressedImage" Storyboard.TargetProperty="Visibility">
                                                        <DiscreteObjectKeyFrame KeyTime="0" >
                                                            <DiscreteObjectKeyFrame.Value>
                                                                <Visibility>Visible</Visibility>
                                                            </DiscreteObjectKeyFrame.Value>
                                                        </DiscreteObjectKeyFrame>
                                                    </ObjectAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </VisualState>
                                            <VisualState Name="Disabled">
                                                <Storyboard>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ActiveImage" Storyboard.TargetProperty="Visibility">
                                                        <DiscreteObjectKeyFrame KeyTime="0" >
                                                            <DiscreteObjectKeyFrame.Value>
                                                                <Visibility>Collapsed</Visibility>
                                                            </DiscreteObjectKeyFrame.Value>
                                                        </DiscreteObjectKeyFrame>
                                                    </ObjectAnimationUsingKeyFrames>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledImage" Storyboard.TargetProperty="Visibility">
                                                        <DiscreteObjectKeyFrame KeyTime="0" >
                                                            <DiscreteObjectKeyFrame.Value>
                                                                <Visibility>Visible</Visibility>
                                                            </DiscreteObjectKeyFrame.Value>
                                                        </DiscreteObjectKeyFrame>
                                                    </ObjectAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </VisualState>
                                        </VisualStateGroup>
                                    </VisualStateManager.VisualStateGroups>
                                    <Image FocusVisualStyle="{x:Null}" Name="ActiveImage" Height="50" Width="16"   Source="/dbMotion.SmartAgent.Client.Viewer;component/Resources/TopButtonMenu.PNG"/>
                                    <Image FocusVisualStyle="{x:Null}" Name="MouseOverImage" Height="50" Width="16" Source="/dbMotion.SmartAgent.Client.Viewer;component/Resources/TopButtonMenuPressed.PNG" Visibility="Collapsed"/>
                                    <Image FocusVisualStyle="{x:Null}" Name="PressedImage" Height="50" Width="16" Source="/dbMotion.SmartAgent.Client.Viewer;component/Resources/TopButtonMenuPressed.PNG" Visibility="Collapsed"/>
                                    <Image FocusVisualStyle="{x:Null}" Name="DisabledImage" Height="50" Width="16" Source="/dbMotion.SmartAgent.Client.Viewer;component/Resources/TopButtonMenu.PNG" Visibility="Collapsed"/>

                                </StackPanel>

                            </ControlTemplate>
                        </Button.Template>
                    </Button>
                </StackPanel>
                </Grid>
            </ControlTemplate>

        </ComboBox.Template>
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <Button  HorizontalAlignment="Stretch">
                    <Button.Template>
                        <ControlTemplate>
                            <Grid  MinWidth="{TemplateBinding ActualWidth}"  Focusable="False" HorizontalAlignment="Stretch">
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup Name="CommonStates">
                                        <VisualState Name="Normal"/>
                                        <VisualState Name="MouseOver">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MouseOverRect" Storyboard.TargetProperty="Visibility">
                                                    <DiscreteObjectKeyFrame KeyTime="0" >
                                                        <DiscreteObjectKeyFrame.Value>
                                                            <Visibility>Visible</Visibility>
                                                        </DiscreteObjectKeyFrame.Value>
                                                    </DiscreteObjectKeyFrame>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <Rectangle Name="MouseOverRect" Visibility="Hidden"  Fill="{StaticResource HighlightBrush}" />
                                <Border Background="Transparent">
                                    <StackPanel Margin="2" Orientation="Vertical">
                                                                                 <view:SmartAgentTextBlock x:Name="Text" Foreground="Black"  
                                                                  Text="{x:Static Viewer:Messages.CollaborateMenuItem}" VerticalAlignment="Center" Margin="4,0,0,0"
                                                                  FontFamily="{Binding Source={x:Static Properties:Settings.Default}, Path=VPOFontFamily, Mode=OneTime}"
                                                                  FontSize="12" 
                                                                  MinWidth="76"/>
                                        <Rectangle Height="1" Margin="2" Fill="#FFCECBCB" Width="178" />

                                    </StackPanel>
                                </Border>
                            </Grid>

                        </ControlTemplate>
                    </Button.Template>
                </Button>

            </DataTemplate>

        </ComboBox.ItemTemplate>


    </ComboBox>

由于

1 个答案:

答案 0 :(得分:1)

解决方案很简单。我刚刚在comobo框项目模板中的按钮的控制模板中添加了另一个ObjectAnimationUsingKeyFrames.ObjectAnimationUsingKeyFrames将设置前景白色MouseOver

 <ComboBox.ItemTemplate>
            <DataTemplate>
                <Button Name="btn"  HorizontalAlignment="Stretch">
                    <Button.Template>
                        <ControlTemplate>
                            <Grid  MinWidth="{TemplateBinding ActualWidth}"  Focusable="False" HorizontalAlignment="Stretch">
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup Name="CommonStates">
                                        <VisualState Name="Normal"/>
                                        <VisualState Name="MouseOver">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MouseOverRect" Storyboard.TargetProperty="Visibility">
                                                    <DiscreteObjectKeyFrame KeyTime="0" >
                                                        <DiscreteObjectKeyFrame.Value>
                                                            <Visibility>Visible</Visibility>
                                                        </DiscreteObjectKeyFrame.Value>
                                                    </DiscreteObjectKeyFrame>
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Text" Storyboard.TargetProperty="Foreground">
                                                    <DiscreteObjectKeyFrame KeyTime="0" >
                                                        <DiscreteObjectKeyFrame.Value>
                                                            <Brush>White</Brush>
                                                        </DiscreteObjectKeyFrame.Value>
                                                    </DiscreteObjectKeyFrame>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <Rectangle Name="MouseOverRect" Visibility="Hidden"  Fill="{StaticResource HighlightBrush}" />
                                <Border Background="Transparent">
                                    <StackPanel Margin="2"  Name="MainGrid"  Orientation="Vertical">
                                        <TextBlock Name="Text" Foreground="Black"  
                                                                  Text="{x:Static Viewer:Messages.CollaborateMenuItem}" VerticalAlignment="Center" Margin="4,0,0,0"
                                                                  FontFamily="{Binding Source={x:Static Properties:Settings.Default}, Path=VPOFontFamily, Mode=OneTime}"
                                                                  FontSize="12" 
                                                                  MinWidth="76"/>
                                        <Rectangle Height="1" Margin="2" Fill="#FFCECBCB" Width="178" />

                                    </StackPanel>
                                </Border>
                            </Grid>
                        </ControlTemplate>
                    </Button.Template>
                </Button>
            </DataTemplate>
        </ComboBox.ItemTemplate>