【问题标题】:How to change the Combo box Selected Item color through MVVM?如何通过 MVVM 更改 Combo box Selected Item 颜色?
【发布时间】:2015-01-24 21:12:59
【问题描述】:

谁能给我一些关于如何通过 MVVM 更改组合框选定项目颜色的示例。 我使用 WPF 作为 UI。

【问题讨论】:

  • 查看样式触发器、数据触发器

标签: wpf mvvm


【解决方案1】:

在您的 ViewModel 中获取所选项目(如果您使用 ICollectionView,则使用 CurrentItem)并在 View 中使用 DataTrigger 更改所选项目的样式。

【讨论】:

    【解决方案2】:

    这应该可以帮助您: https://stackoverflow.com/a/9906687/1468507

    无论您是在谈论选择还是突出显示。

    编辑(将所选文本设置为红色): 这应该可以解决问题。

    <ComboBox>
      <ComboBox.Resources>
        <Style TargetType="{x:Type TextBlock}">
          <Style.Triggers>
            <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ComboBoxItem}}" Value="True">
              <Setter Property="Foreground" Value="Red" />
            </DataTrigger>
          </Style.Triggers>
        </Style>
      </ComboBox.Resources>
    ...
    </ComboBox>
    

    【讨论】:

    • 我需要将组合框中选中项的文本颜色改为红色,而不是组合框选中项的背景色。
    猜你喜欢
    • 2017-01-28
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 2013-05-15
    相关资源
    最近更新 更多