从XAML中的Usercontrol访问ControlTemplate(Content)

时间:2014-02-17 16:33:02

标签: wpf xaml datatemplate controltemplate

我有以下Usercontrol,我在另一个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的地方实现这一目标

提前致谢。

2 个答案:

答案 0 :(得分:0)

您可以从要使用UserControl的位置为要访问的属性创建依赖项属性。有了它,您可以将值从外部绑定到ControlTemplate中的特定属性。

从外部设置Button内容的代码如下所示:

依赖属性(在UserControl的代码隐藏中):

public string ButtonContent
{
    get { return (string)GetValue(ButtonContentProperty); }
    set { SetValue(ButtonContentProperty, value); }
}

public static readonly DependencyProperty ButtonContentProperty =
    DependencyProperty.Register("ButtonContent", typeof(string), typeof(TestButton), new PropertyMetadata("Test"));

public string ButtonContent { get { return (string)GetValue(ButtonContentProperty); } set { SetValue(ButtonContentProperty, value); } } public static readonly DependencyProperty ButtonContentProperty = DependencyProperty.Register("ButtonContent", typeof(string), typeof(TestButton), new PropertyMetadata("Test"));

在将其绑定到Usercontrol中的Property之后,您可以像这样创建Usercontrol并从ViewModel获取所需的内容(假设您使用的是MVVM):

<wpfApplication2:TestButton ButtonContent="{Binding TestContentFromViewModel}"></wpfApplication2:TestButton>

您可以为所需的UserControl中的每个属性执行此操作。

有关依赖项属性的其他信息,请参阅here

答案 1 :(得分:0)

好的,我会回答的。事情不是 ControlTemplate ,我们应该修改ListBox的 ItemTemplate 并将相同的ControlTemplate应用为 DataTemplate

<ListBox x:Name="_listBox"
    SelectedItem="{Binding ElementName=SpecialToggleControl, Path=SelectedItem}"
    SelectionMode="Single" 
    ItemsSource="{Binding ElementName=SpecialToggleControl, Path=ItemsSource}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="{Binding ElementName=SpecialToggleControl, Path=ColumnCount}" Background="Beige"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <ToggleButton Content="{TemplateBinding ContentControl.Content}" 
                          IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                            AncestorType={x:Type ListBoxItem}},Path=IsSelected}" 
                          Name="toggleButton"/>
        </DataTemplate>
    </ListBox.ItemTemplate>

    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="VerticalContentAlignment" Value="Stretch" />
        </Style>
    </ListBox.ItemContainerStyle>

</ListBox>

MainWindow.xaml(我使用此控件的地方): (转换器“cc”现在将有条件地应用颜色)

                <WpfApplication2:MutuallyExclusiveToggleControl.Resources>
                <Style TargetType="{x:Type ToggleButton}">
                    <Style.Setters>
                        <Setter Property="Background" Value="{Binding Converter={StaticResource cc}}"></Setter>
                    </Style.Setters>                    
                </Style>
            </WpfApplication2:MutuallyExclusiveToggleControl.Resources>

转换器代码:

public class ColourConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if((string)value == "Buy")
        {
            return Brushes.Blue;
        }

        if ((string)value == "Sell")
        {
            return Brushes.Red;
        }

        return Brushes.White;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}