C# 访问控件模板中的控件
我为一个控件定义了一个样式,这样它的内容就是不同控件的组合C# 访问控件模板中的控件,c#,wpf,xaml,C#,Wpf,Xaml,我为一个控件定义了一个样式,这样它的内容就是不同控件的组合 <Style x:Key="TagSetting" TargetType="CheckBox"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type CheckBox}"> <StackPanel
<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>
如何访问这些复选框的组合框,以便在C#code(而不是XAML)中设置所选索引?您使用样式而不是仅使用
,有什么特别的原因吗?与其使用样式,我将使用您的ControlTemplate并将其转换为数据模板。创建包含所有需要处理的数据的相册模型,然后将复选框
内容绑定到模型。DataTemplate的TextBlock可以绑定到Album.Name属性,然后您可以访问所需的所有附加元数据,因为它是模型的一部分。@JohnathonSullinger,我对XAML和WPF非常陌生,所以我不太熟悉什么时候应该和不应该使用不同的东西。你所解释的听起来是个好主意,我会努力想办法的。
<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... --->