【问题标题】:WPF, cannot access a control inside a ControlTemplateWPF,无法访问 ControlTemplate 中的控件
【发布时间】:2017-02-22 12:42:51
【问题描述】:

我有以下 ControlTemplate:

<UserControl x:Class="WpfSinergoHMIControls.Controlli.ControlButton"
         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" 
         xmlns:local="clr-namespace:WpfSinergoHMIControls.Controlli"
         mc:Ignorable="d">

<UserControl.Template>
    <ControlTemplate TargetType="UserControl" x:Name="ControlButtonTemplate">
        <Grid Background="Black" Name="ControlButtonGrid">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="2*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="1*" />
                <RowDefinition Height="2*" />
            </Grid.RowDefinitions>

            <Label Content="{TemplateBinding Content}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Background="White" Margin="1,1,1,1" Grid.Row="0"></Label>
            <Viewbox Grid.Row="1" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center">
                <Ellipse Width="100" Height="100"  Fill="White" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </Viewbox>
            <Viewbox Grid.Row="1" Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center">
                <Ellipse Width="100" Height="100" Margin="0" Name="InnerEllipse" Fill="Black" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Viewbox>
        </Grid>
    </ControlTemplate>
</UserControl.Template>

我想以编程方式访问名为“InnerEllipse”的对象。

我尝试使用以下代码行:

Ellipse InnerEllipse = (Ellipse) this.Template.FindName("InnerEllipse", this);

在“ControlButton”类中名为“Color”的属性中调用:

public Color Color
{
    set
    {
        Ellipse InnerEllipse = (Ellipse)this.Template.FindName("InnerEllipse", this);
    }
}

当我使用“用户控件”时,属性“颜色”会被初始化

<Controlli:ControlButton Height="169" Width="119" Color="DarkGreen"/>

问题是“FindName”函数返回“null”。我无法弄清楚缺少什么。非常感谢!

【问题讨论】:

  • 你试过this.FindName("InnerEllipse", this.Template)吗?

标签: c# wpf controltemplate


【解决方案1】:

问题在于Color 属性是在应用模板之前设置的。

Color 应该是一个依赖属性。然后,您可以指定一个默认值以及连接一个 PropertyChangedCallback,只要它被更改就会被调用:

public partial class ControlButton : UserControl
{
    public ControlButton()
    {
        InitializeComponent();
    }

    Ellipse InnerEllipse;
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        InnerEllipse = (Ellipse)this.Template.FindName("InnerEllipse", this);
    }

    public static readonly DependencyProperty ColorProperty =
         DependencyProperty.Register("Color", typeof(Color),
         typeof(ControlButton), new FrameworkPropertyMetadata(Colors.DarkGreen, new PropertyChangedCallback(OnColorChanged)));

    public Color Color
    {
        get { return (Color)GetValue(ColorProperty); }
        set { SetValue(ColorProperty, value); }
    }

    private static void OnColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ControlButton ctrl = d as ControlButton;
        if (ctrl.InnerEllipse != null)
            ctrl.InnerEllipse.Fill = new SolidColorBrush() { Color = ctrl.Color };
    }
}

【讨论】:

  • 我使用“OnApplyTemplate()”获取“InnerEllipse”对象并在此函数中执行我需要执行的任何操作。谢谢!
猜你喜欢
  • 1970-01-01
  • 2010-10-23
  • 2013-10-07
  • 1970-01-01
  • 1970-01-01
  • 2013-11-03
  • 2010-12-06
  • 1970-01-01
相关资源
最近更新 更多