从Generic.xaml点击按钮? [英] Click Button from Generic.xaml?

查看:123
本文介绍了从Generic.xaml点击按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在Silverlight自定义控件,我有让我按钮的问题点击到Generic.xaml文件。这不起作用:

 < setter属性=模板> 
< Setter.Value>
<的ControlTemplate的TargetType =本地:ScrollableTabControl>
<按钮Grid.Row =0Grid.Column =0X:NAME =ScrollLeft点击=scrollLeft>&放大器; LT;< /按钮>
< /控件模板>
< /Setter.Value>



ScrollLeft是我的C#文件。



不过这并不工作之一:

 变种b = this.GetTemplateChild(ScrollLeft); 
Debug.Assert的(B!= NULL);



因为不管我做什么,B总是回来空。我觉得应该有点击的方法分配给该按钮一个简单的方法,所以我在做什么错



编辑:这是我的C#文件的方法

 公共无效scrollLeft(对象发件人,RoutedEventArgs E)
{
// VAR滚动=应用.Current.Resources [TabScroller]作为ScrollViewer中;
//scroller.LineLeft();
}


解决方案

您必须重写< STRONG> OnApplyTemplate()上的自定义控制类(C#文件)



喜欢这一点。

 公共覆盖无效OnApplyTemplate()
{
键BTN = GetTemplateChild(ScrollLeft)作为按钮;
Debug.Assert的(BTN!= NULL);
}



另外,你必须让在通用控件模板一次机会。 。XAML像下面

 < setter属性=模板> 
< Setter.Value>
<的ControlTemplate的TargetType ={X:类型本地:CustomControl1}>
<边框背景={TemplateBinding背景}
BorderBrush ={TemplateBinding BorderBrush}
了borderThickness ={TemplateBinding了borderThickness}>
将按钮Grid.Row =0Grid.Column =0×:名称=ScrollLeft>&放大器;所述;&下; /按钮>
< /边框>
< /控件模板>
< /Setter.Value>





注意:我删除Click事件处理程序。


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:

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:ScrollableTabControl">
                    <Button Grid.Row="0" Grid.Column="0" x:Name="ScrollLeft" Click="scrollLeft">&lt;</Button>
            </ControlTemplate>
        </Setter.Value>

"ScrollLeft" is in my C# file.

However this does not work either:

        var b = this.GetTemplateChild("ScrollLeft");
        Debug.Assert(b != null);

Because no matter what I do, b always comes back null. I feel like there should be an easy way to assign the click method to this button, so what am I doing wrong?

EDIT: This is the method from my C# file:

    public void scrollLeft(object sender, RoutedEventArgs e)
    {
        //var scroller = Application.Current.Resources["TabScroller"] as ScrollViewer;
        //scroller.LineLeft();
    }

解决方案

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.

这篇关于从Generic.xaml点击按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆