簡體   English   中英

WPF,無法訪問ControlTemplate內的控件

[英]WPF, cannot access a control inside a ControlTemplate

我有以下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);
    }
}

當我使用'UserControl'時,屬性'Color'會被初始化

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

問題是函數“FindName”返回'null'。 我無法弄清楚遺漏了什么。 非常感謝你!

問題是在應用模板之前設置了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 };
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM