0

有了围绕控件的所有样式信息,以及嵌套的内容、控件模板和触发器,我试图弄清楚以下内容。

采用组合框控件。它有一个用于切换按钮组件的控制模板,该模板以标准显示(非下拉模式)显示正常显示,显示显示值和切换按钮以激活下拉菜单。

<ControlTemplate TargetType="ToggleButton" x:Key="baseComboBoxToggleButton" >
   <!-- overall border covering left side display value and the actual toggle button -->
   <Border x:Name="Border" Grid.ColumnSpan="2" />

   <!-- area in left side (column=0) that shows the DISPLAY value -->
   <Border x:Name="ShowDisplayValueArea" Grid.Column="0" />

   <!-- second column using a path to draw the glyph down arrow -->
   <Path Grid.Column="1" />

   <Triggers for the toggle button ... />
</ControlTemplate>

然后,您拥有使用上面切换按钮模板的主组合框控件

<ControlTemplate TargetType="ComboBox" x:Key="ComboBoxGridControlTemplate" >
   <Grid>
      <ToggleButton Name="ToggleButton" 
                    Template="{StaticResource baseComboBoxToggleButton}" 
                    ... />
   </Grid>
</ControlTemplate>

因此,我试图根据 MultiBinding 转换器的结果最终更改“ShowDisplayValueArea”的背景颜色。如果我将多绑定转换器放在 toggleButton 控件模板区域中,例如..

<MultiBinding Converter="{StaticResource myMultiParmConverter}">
   <Binding Path="." RelativeSource="{RelativeSource Self}" />
</MultiBinding>

值对象数组中的第一个“值”正确地传递了切换按钮控件模板的实例。整个对象引用,而不仅仅是名称。

public object Convert(object[] values, 
                      Type targetType, 
                      object parameter, 
                      CultureInfo culture)

所以,我如何告诉 Binding 参数传递切换按钮来自的实际组合框(即:切换按钮的父级),所以我得到了作为参数传递的实际整个组合框控件。

4

1 回答 1

0

找到了……完全是偶然的……

在我的转换器类的调试器中,我正在查看调试器窗口中的对象引用。然后,由于它是一个对象,我点击了“值”列右侧的放大镜。这样做,调出了“WPF Visualizer”(以前从未使用过/见过)。所以,从这个角度来看,它的“TemplatedParent”实际上是我想要的对象。显然,我可以考虑在其他时间使用其他属性,但 TemplatedParent 是我想要的。

所以,我改变了绑定

<Binding Path="." RelativeSource="{RelativeSource Self}" />

<Binding Path=".TemplatedParent" RelativeSource="{RelativeSource Self}" />

并正确地将实际的 Combobox 控件作为参数传递给我的转换器。现在,我可以直接在转换器中使用组合框的任何/所有属性,而无需单独显式地传递它们。

于 2012-03-23T16:53:40.170 回答