将列表框控件中选定的字体颜色设置为与未选定的字体颜色不同

发布于 2024-07-26 09:53:46 字数 190 浏览 3 评论 0原文

我发现字体颜色来自列表框或组合框的内容(即控件模板之外)。 当未选择项目时,我希望在白色背景上显示黑色文本,而当选择项目时,我希望在白色背景上显示黑色文本。 不幸的是我无法弄清楚如何更改文本颜色。 我正在努力寻找在选定和未选定的背景颜色之间形成鲜明对比的颜色。

检查了 Silverlight 3,但您似乎也无法在那里执行此操作。

I've found that the font colour comes from the content (ie outside of the controltemplate) of a listbox or combobox. I'd like to have black text on white background when the items are unselected, and when selected would like black background with white text.
unfortunately I've not been able to figure out how to change the text colour. I'm struggling to find colours that contrast well between the selected and unselected background colour.

checked in Silverlight 3 and you dont seem able to do it there either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒猫 2024-08-02 09:53:46

我设法在 Silverlight 4 中为在 AutoCompleteBox 下弹出的 ListBox 做类似的事情,但它是大量的 Binging 和 XAML 强奸。

基本上,您必须创建一个 ListBoxItem 样式并在其下强制执行一些控件模板 hacking。 然后您可以将其应用到您的列表框上,它应该可以工作。

我刚刚从我正在处理的项目中复制了相关代码,因此您必须对其进行一些调整,但它确实会更改鼠标悬停时的字体和选择矩形颜色,因此应该是一个很好的开始。

    <Style x:Key="MyListBoxItemStyle" TargetType="ListBoxItem">
        <Setter Property="Foreground" Value="#FF4C4C4C" />
        <Setter Property="FontStyle" Value="Normal" />
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="ListBoxItem">
                <Grid Background="{TemplateBinding Background}">
                  <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualStateGroup.Transitions>
                            <VisualTransition GeneratedDuration="0:0:0.2" To="MouseOver">
                                <VisualTransition.GeneratedEasingFunction>
                                    <CubicEase EasingMode="EaseOut"/>
                                </VisualTransition.GeneratedEasingFunction>
                            </VisualTransition>
                            <VisualTransition From="MouseOver" GeneratedDuration="0:0:0.1">
                                <VisualTransition.GeneratedEasingFunction>
                                    <CubicEase EasingMode="EaseOut"/>
                                </VisualTransition.GeneratedEasingFunction>
                            </VisualTransition>
                        </VisualStateGroup.Transitions>
                      <VisualState x:Name="Normal"/>
                      <VisualState x:Name="MouseOver">
                        <Storyboard>
                          <DoubleAnimationUsingKeyFrames Storyboard.TargetName="fillColor" Storyboard.TargetProperty="Opacity">
                            <SplineDoubleKeyFrame KeyTime="0" Value="1"/>
                          </DoubleAnimationUsingKeyFrames>
                          <ColorAnimation Duration="0" To="#FFFFFFFF" Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" d:IsOptimized="True"/>
                        </Storyboard>
                      </VisualState>
                    </VisualStateGroup>
                    <VisualStateGroup x:Name="SelectionStates">
                      <VisualState x:Name="Unselected"/>
                      <VisualState x:Name="Selected">
                        <Storyboard>
                          <DoubleAnimationUsingKeyFrames Storyboard.TargetName="fillColor2" Storyboard.TargetProperty="Opacity">
                            <SplineDoubleKeyFrame KeyTime="0" Value="1"/>
                          </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                      </VisualState>
                    </VisualStateGroup>
                  </VisualStateManager.VisualStateGroups>
                  <Rectangle x:Name="fillColor" IsHitTestVisible="False" Opacity="0" RadiusX="1" RadiusY="1" Fill="#FF1f6cae"/>
                  <Rectangle x:Name="fillColor2" IsHitTestVisible="False" Opacity="0" Fill="#FF000000" RadiusX="1" RadiusY="1"/>
                  <ContentControl HorizontalAlignment="Left" Margin="{TemplateBinding Padding}" x:Name="contentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Foreground="#FF4c4c4c"/>
                </Grid>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        <Setter Property="FontSize" Value="14"/>
    </Style>

它应该直接位于 UserControl.Resources 下,您就可以绑定它了。

如果您成功或需要更多解释,请告诉我!

I managed to do a similar thing in Silverlight 4 for a ListBox popping under an AutoCompleteBox, but it was copious amounts of Binging and XAML rape.

Basically you have to create a ListBoxItem style and force some control template hackery under it. This then you can apply on your ListBox and it should work.

I just copied the relevant code from the project I'm working on so you'll have to tweak it a bit, but it does change both the font and the selection rectangle colour on mouse over, so should be a great start.

    <Style x:Key="MyListBoxItemStyle" TargetType="ListBoxItem">
        <Setter Property="Foreground" Value="#FF4C4C4C" />
        <Setter Property="FontStyle" Value="Normal" />
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="ListBoxItem">
                <Grid Background="{TemplateBinding Background}">
                  <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualStateGroup.Transitions>
                            <VisualTransition GeneratedDuration="0:0:0.2" To="MouseOver">
                                <VisualTransition.GeneratedEasingFunction>
                                    <CubicEase EasingMode="EaseOut"/>
                                </VisualTransition.GeneratedEasingFunction>
                            </VisualTransition>
                            <VisualTransition From="MouseOver" GeneratedDuration="0:0:0.1">
                                <VisualTransition.GeneratedEasingFunction>
                                    <CubicEase EasingMode="EaseOut"/>
                                </VisualTransition.GeneratedEasingFunction>
                            </VisualTransition>
                        </VisualStateGroup.Transitions>
                      <VisualState x:Name="Normal"/>
                      <VisualState x:Name="MouseOver">
                        <Storyboard>
                          <DoubleAnimationUsingKeyFrames Storyboard.TargetName="fillColor" Storyboard.TargetProperty="Opacity">
                            <SplineDoubleKeyFrame KeyTime="0" Value="1"/>
                          </DoubleAnimationUsingKeyFrames>
                          <ColorAnimation Duration="0" To="#FFFFFFFF" Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" d:IsOptimized="True"/>
                        </Storyboard>
                      </VisualState>
                    </VisualStateGroup>
                    <VisualStateGroup x:Name="SelectionStates">
                      <VisualState x:Name="Unselected"/>
                      <VisualState x:Name="Selected">
                        <Storyboard>
                          <DoubleAnimationUsingKeyFrames Storyboard.TargetName="fillColor2" Storyboard.TargetProperty="Opacity">
                            <SplineDoubleKeyFrame KeyTime="0" Value="1"/>
                          </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                      </VisualState>
                    </VisualStateGroup>
                  </VisualStateManager.VisualStateGroups>
                  <Rectangle x:Name="fillColor" IsHitTestVisible="False" Opacity="0" RadiusX="1" RadiusY="1" Fill="#FF1f6cae"/>
                  <Rectangle x:Name="fillColor2" IsHitTestVisible="False" Opacity="0" Fill="#FF000000" RadiusX="1" RadiusY="1"/>
                  <ContentControl HorizontalAlignment="Left" Margin="{TemplateBinding Padding}" x:Name="contentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Foreground="#FF4c4c4c"/>
                </Grid>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        <Setter Property="FontSize" Value="14"/>
    </Style>

It should go straight under UserControl.Resources and you'll be ready to bind it.

Let me know if you managed or need more explanation!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文