Pergunta

I want to (re-)template a Control, for example a ComboBox.

XAML:

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

In the ControlTemplate I want to have a 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>

But I want to set the CommandParameter to the Control which applied the Template (in this example the ComboBox).

How can I solve this?

Foi útil?

Solução

Ok, i found the right solution.

I have set the Binding to 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>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top