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

Dav*_*vid 3 wpf xaml binding controltemplate commandparameter

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

XAML:

<ComboBox Style="{StaticResource MyComboBoxStyle}" ... >
    <!-- ... -->
</ComboBox>
Run Code Online (Sandbox Code Playgroud)

在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>
Run Code Online (Sandbox Code Playgroud)

但我想设置CommandParameter应用模板的Control(在本例中为ComboBox).

我怎么解决这个问题?

Dav*_*vid 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>
Run Code Online (Sandbox Code Playgroud)