【问题标题】:Conditional Formatting of items in a Combox组合框中项目的条件格式
【发布时间】:2013-01-20 13:09:11
【问题描述】:

目前我在一个团队中构建一个包含来自视图模型的数据的数据网格。我们使用的是响应式 UI,数据网格中的每个“行”都由一个继承自“ReactiveObject”的视图模型类表示。

我感兴趣的视图模型中有 2 个属性可以填充每行中的组合框。 PossibleScalesDefaultScale 的集合。 PossibleScales 被加载到组合框中,DefaultScale 用于将选定项设置为 DefaultScaleDefaultScale 始终包含在 DefaultScale 集合中。我想要实现的是组合框中的任何非默认比例都以斜体格式格式化。所以我创建了一个调用 IMultiValueConverter 的样式。所以,我需要给这个 IMultiValueConverter 2 个参数,即当前比例(来自 PossibleScales 和 DefaultScale 的集合。

这是我们必须用可能的刻度列表中的所有项目填充组合框的 XAML(我对 EditingElementStyle 有相同的):

<DataGridComboBoxColumn.ElementStyle>
   <Style TargetType="ComboBox">
       <Setter Property="ItemsSource" Value="{Binding Path=PossibleScales}"  />
       <Setter Property="ItemContainerStyle" Value="{StaticResource Measeurementscale}"></Setter>
   </Style>
</DataGridComboBoxColumn.ElementStyle>

Measeurementscale StaticResource 被定义为控件资源部分的样式。

<Style x:Key="Measeurementscale" TargetType="ComboBoxItem">
    <Setter Property="FontStyle">
       <Setter.Value>
          <MultiBinding Converter="{StaticResource NonDefaultScaleToItalicConverter}">
              <Binding RelativeSource="{RelativeSource Self}" Path="DataContext"/>
          </MultiBinding>
       </Setter.Value>
    </Setter>
</Style>

如您所见,comboboxitem 的当前值被传递给 IMultiValueConverter,这很好。但现在我想知道如何将 DefaultScale(它是 viewmodel 的一个属性并定义为控件的 DataContext)也传递给这个转换器。我无法做到这一点。

任何帮助将不胜感激。

【问题讨论】:

    标签: wpf xaml combobox viewmodel wpfdatagrid


    【解决方案1】:

    您可以使用FindAncestor 获取设置此数据上下文的父控件并绑定到它。例如:

    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ComboBox}"
             Path="DataContext.DefaultScale" />
    

    【讨论】:

    • 我不知道为什么,但是 FontStyle 不起作用。不透明度是,所以我改用它。对我来说最重要的是对非默认比例有一个视觉线索,你的回答很有帮助。所以我已经实现了我的目标:组合框中项目的条件格式
    猜你喜欢
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    相关资源
    最近更新 更多