【问题标题】:Access ControlTemplate (Content) from the Usercontrol in XAML从 XAML 中的用户控件访问 ControlTemplate(内容)
【发布时间】: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


    【解决方案1】:

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

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

    依赖属性(在您的 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"));
    

    将它绑定到用户控件中的属性后,您可以像这样创建用户控件并从 ViewModel 中获取所需的内容(假设您使用的是 MVVM):

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

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

    有关依赖属性的更多信息,请参阅here

    【讨论】:

    • 但是,Content 是基于 ListBox 的 ItemsSource 设置的(如果您仔细查看 xaml)。我不想单独设置内容。
    【解决方案2】:

    好的,我会回答的。我们应该修改 ListBox 的 ItemTemplate 并应用与 DataTemplate 相同的 ControlTemplate,而不是 ControlTemplate

    <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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-07
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多