【发布时间】:2014-02-17 16:33:02
【问题描述】:
我在另一个 xaml 文件中使用/引用了以下用户控件 -
<UserControl x:Class="WpfApplication2.MutuallyExclusiveToggleControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
x:Name="SpecialToggleControl"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ToggleButton Content="{TemplateBinding ContentControl.Content}"
Background="{Binding ElementName=SpecialToggleControl, Path=TileBackground}"
IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}}"
Name="toggleButton"/>
<ControlTemplate.Triggers>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<ListBox x:Name="_listBox"
SelectionChanged="ListBoxSelectionChanged"
SelectedItem="{Binding ElementName=SpecialToggleControl, Path=SelectedItem}"
SelectionMode="Single" ItemsSource="{Binding ElementName=SpecialToggleControl, Path=ItemsSource}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="{Binding ElementName=SpecialToggleControl, Path=ColumnCount}"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
问题:如何从我使用此 UserControl 的位置访问 ToggleButton 的内容(在 ControlTemplate 中)。例如:比如说,根据内容,我想设置背景颜色。我不想从 UserControl 内部执行此操作。我想从我使用这个 UserControl 的地方实现这一点
提前致谢。
【问题讨论】:
标签: wpf xaml datatemplate controltemplate