我定义了一个Custom Control:<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Test">
    <Style TargetType="{x:Type local:CC1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CC1}">
                    <Viewbox x:Name="MView">
                        <Grid Width="50" Height="50" x:Name="MGrid" Background="Red">
                            <Grid.RenderTransform>
                                <TranslateTransform X="0" Y="0"/>
                            </Grid.RenderTransform>
                        </Grid>
                    </Viewbox>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
1. 我想在CC1的.cs文件中对MGrid的进行变形如实现移动到某一位置:
    public class CC1 : Control
    {
        static CC1()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CC1), new FrameworkPropertyMetadata(typeof(CC1)));
        }        public void MoveTo(double x, double y)
        {
            // 控制MGrid的RenderTransform的TranslateTransform,请问如何控制?
        }
    }2. 我想在调用CC1的程序中(也就是外部)对CC1的MGrid的RenderTransform进行控制,请问应该如何暴露到外边?是通过上述方法的形式么?不能像UserControl那样直接将MGrid起一个名字就暴露到外边么?请指教下实现方法(大家正常的做法呢?),非常感激!

解决方案 »

  1.   

    请大家不吝赐教啊。
    我尝试使用FindName寻找:CC1 cc1 = new CC1();
    Grid grid = cc1.FindName("MGrid") as Grid;一样是找不到"MGrid",(即使是在CC1类内部也不知道如何访问)
    请大家指点一下,多谢了。
      

  2.   

    这个是不是涉及如何从控件访问其ControlTemplate内的元素的问题?我看书上写,使用Template的FindName方法,我在CC1中加入方法:        public Grid GetGrid()
            {
                return  this.Template.FindName("MGrid", this) as Grid;
            }使用:            CC1 cc1 = new CC1();
                Grid g = cc1.GetGrid();
    仍然行不通,还弹出了异常。实在不知道怎么办啊,弄了一天了,大家不要因为简单不回答啊,您一句话也帮了我大忙啊,我现在就剩一次回答机会了,请大家指教!谢谢了!
      

  3.   

    VisualTreeHelper.GetChild(ElementName,Index)
    一层层找
      

  4.   

    添加TemplateVisualState,创建状态动画,对外公布修改状态方法或者属性