绑定到应用样式/模板的Control

时间:2012-11-29 11:58:48

标签: wpf xaml binding controltemplate commandparameter

我想(重新)模板控件,例如ComboBox。

XAML:

<ComboBox Style="{StaticResource MyComboBoxStyle}" ... >
    <!-- ... -->
</ComboBox>

在ControlTemplate中我想要一个Button。

的ResourceDictionary:

<Style x:Key="MyComboBoxStyle" TargetType="{x:Type ComboBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBox}">
                <Grid>
                    <!-- ... -->

                    <Button Command="{TemplateBinding Tag}" CommandParameter="{Binding ???}" />

                    <!-- ... -->
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

但我想将CommandParameter设置为应用模板的Control(在此示例中为ComboBox)。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:10)

好的,我找到了正确的解决方案。

我已将Binding设置为Binding RelativeSource={RelativeSource TemplatedParent}

<Style x:Key="MyComboBoxStyle" TargetType="{x:Type ComboBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBox}">
                <Grid>
                    <!-- ... -->

                    <Button Command="{TemplateBinding Tag}" CommandParameter="{Binding RelativeSource={RelativeSource TemplatedParent}}" />

                    <!-- ... -->
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
相关问题