加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

WPF笔刷之VisualBrush(1)

(2012-02-20 21:14:05)
标签:

杂谈

分类: wpf

http://s13/middle/633d0e17hb9651706717c&690

<Grid><!--VisualBrush-->
        <Grid.Background>
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                <GradientStop Color="Black" Offset="0"></GradientStop>
                <GradientStop Color="White" Offset="1"></GradientStop>
            </LinearGradientBrush>
        </Grid.Background>
        <TextBox Height="72" HorizontalAlignment="Left" Margin="64,98,0,0" Name="textBox1" VerticalAlignment="Top" Width="391" FontSize="40" />
        <Rectangle Height="54" HorizontalAlignment="Left" Margin="64,182,0,0" Name="rectangle1" VerticalAlignment="Top" Width="391" StrokeThickness="0">
            <Rectangle.Fill>
                <VisualBrush Visual="{Binding ElementName=textBox1}">
                    <VisualBrush.RelativeTransform>
                        <TransformGroup>
                            <!--使用 ScaleTransform 物件可以水平或垂直延伸或壓縮物件。ScaleX 屬性指定物件沿著 X 軸要延伸或縮小的量,而 ScaleY 屬性則指定物件沿著 Y 軸要延伸或縮小的量。-->
                            <ScaleTransform ScaleX="1" ScaleY="-1"></ScaleTransform>
                            <!--TranslateTransform 會沿著 X 和 Y 軸定義與軸對齊的平移。下圖顯示平移位移為 (dx, dy) 的轉換矩陣。-->
                            <TranslateTransform Y="1"></TranslateTransform>
                        </TransformGroup>
                    </VisualBrush.RelativeTransform>
                </VisualBrush>
            </Rectangle.Fill>
        </Rectangle>

    </Grid>

 

 

通过代码实现

private VisualBrush vbsh;
        private Rectangle rect;
        private TextBox textBox;
        public VisualText()
        {
            InitializeComponent();
            createTextBox();
            createRect();
            createVisual();
            rect.Fill = vbsh;
        }
        #region //创建文本框
        private void createTextBox()
        {
            textBox = new TextBox();
            textBox.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            textBox.VerticalAlignment = System.Windows.VerticalAlignment.Top;

            this.RegisterName("text1", textBox);//
            textBox.Width = 300;
            textBox.Height = 50;
            textBox.BorderThickness = new Thickness(2, 4, 6, 8);
            textBox.Margin = new Thickness(40, 1, 0, 0);
            grid.Children.Add(textBox);
        }
        #endregion
        #region //创建矩形
        public void createRect()
        {
            rect = new Rectangle();
            rect.Margin = new Thickness(10, 10, 0, 0);
            rect.Width = 200;
            rect.Height = 30;
            rect.Stroke = new SolidColorBrush(Colors.Yellow);
            rect.StrokeThickness = 5;
            grid.Children.Add(rect);
        }
        #endregion
        #region //VisualBrush
        private void createVisual()
        {
            vbsh = new VisualBrush();
            vbsh.AutoLayoutContent = true;
            Binding bd = new Binding();
            bd.ElementName = "text1";//
            BindingOperations.SetBinding(vbsh, VisualBrush.VisualProperty, bd);
        }
        #endregion
    }

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有