Click Button from Generic.xaml?

前端 未结 2 1468
太阳男子
太阳男子 2021-01-14 09:35

I am writing a custom control in Silverlight and I am having issues getting my Button to click to the Generic.xaml file. This does not work:

    

        
相关标签:
2条回答
  • 2021-01-14 10:27

    You have to override the OnApplyTemplate() on your custom control class (C# File).

    Like this.

        public override void OnApplyTemplate()
        {
            Button btn = GetTemplateChild("ScrollLeft") as Button;
            Debug.Assert(btn != null);
        }
    

    Also, you have to make one more change in the control template in Generic.Xaml like below.

    <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Button Grid.Row="0" Grid.Column="0" x:Name="ScrollLeft">&lt;</Button>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
    

    Note: I removed the Click event handler.

    0 讨论(0)
  • 2021-01-14 10:38

    Although is it possible I recommend using Commands so the designer who replaces the template is free to pick other controls and events to bind the logic to.

    0 讨论(0)
提交回复
热议问题