dimanche 28 juin 2015

Accessing controls in control template

I have defined a style for a control so that it's content is a combonation of different controls.

<Style x:Key="TagSetting" TargetType="CheckBox">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CheckBox}">
                <StackPanel Orientation="Horizontal" Margin="0,0,0,5">
                    <CheckBox x:Name="chkTag" Focusable="False"/>
                    <ComboBox x:Name="cbbTagAction" Width="65" Margin="0,0,5,0">
                        <ComboBoxItem Content="Clear"/>
                        <ComboBoxItem Content="Tag"/>
                    </ComboBox>
                    <TextBlock x:Name="lblTag" Text="{TemplateBinding Content}" VerticalAlignment="Center"/>
                </StackPanel>
                <ControlTemplate.Triggers>
                    <Trigger SourceName="chkTag" Property="IsChecked" Value="True">
                        <Setter TargetName="cbbTagAction" Property="IsEnabled" Value="True"/>
                        <Setter TargetName="cbbTagAction" Property="SelectedIndex" Value="1"/>
                        <Setter TargetName="lblTag" Property="IsEnabled" Value="True"/>
                    </Trigger>
                    <Trigger SourceName="chkTag" Property="IsChecked" Value="False">
                        <Setter TargetName="cbbTagAction" Property="IsEnabled" Value="False"/>
                        <Setter TargetName="cbbTagAction" Property="SelectedIndex" Value="-1"/>
                        <Setter TargetName="lblTag" Property="IsEnabled" Value="False"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And I need to access the ComboBox cbbTagAction in code so I can set the selected index. I have many of these CheckBoxes.

<CheckBox x:Name="chkAlbum" Style="{StaticResource TagSetting}" Content="album"/>
<CheckBox x:Name="chkAlbumArtists" Style="{StaticResource TagSetting}" Content="album artists"/>
<CheckBox x:Name="chkArtists" Style="{StaticResource TagSetting}" Content="artists"/>
<CheckBox x:Name="chkArtwork" Style="{StaticResource TagSetting}" Content="artwork"/>
<!-- It goes on... --->

How can I access the ComboBox for each of these CheckBoxes so I can set the selected index in C# code (not XAML)?

Aucun commentaire:

Enregistrer un commentaire